plugins/include/float.inc
author Scott Ehlert <ds@alliedmods.net>
Mon Feb 12 09:00:55 2007 +0000 (2007-02-12)
changeset 475 bb11d7eaae0f
parent 374 39ab08027d9e
child 537 3fc99cb190ce
permissions -rw-r--r--
Fixed tabs, hopefully...
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _float_included
    17  #endinput
    18 #endif
    19 #define _float_included
    20 
    21 /**
    22  * @GLOBAL@
    23  *   All the trigonometric functions take in or return its values in RADIANS.
    24  *   Use DegToRad or RadToDeg stocks to convert the radix units.
    25  */
    26 
    27 /* Different methods of rounding */
    28 enum floatround_method
    29 {
    30 	floatround_round = 0,
    31 	floatround_floor,
    32 	floatround_ceil,
    33 	floatround_tozero
    34 };
    35 
    36 /**
    37  * Converts an integer into a floating point value.
    38  *
    39  * @param value			Integer to convert.
    40  * @return				Floating point value.
    41  */
    42 native Float:float(value);
    43 
    44 /**
    45  * Converts a string into a floating point value.
    46  *
    47  * @param str			String to convert.
    48  * @return				Converted floating point number.
    49  */
    50 native Float:FloatStr(const String:str[]);
    51 
    52 /**
    53  * Multiplies two floats together.
    54  *
    55  * @param oper1			First value.
    56  * @param oper2			Second value.
    57  * @return				oper1*oper2.
    58  */
    59 native Float:FloatMul(Float:oper1, Float:oper2);
    60 
    61 /**
    62  * Divides the dividend by the divisor.
    63  *
    64  * @param dividend		First value.
    65  * @param divisor		Second value.
    66  * @return				dividend/divisor.
    67  */
    68 native Float:FloatDiv(Float:dividend, Float:divisor);
    69 
    70 /**
    71  * Adds two floats together.
    72  *
    73  * @param oper1			First value.
    74  * @param oper2			Second value.
    75  * @return				oper1+oper2.
    76  */
    77 native Float:FloatAdd(Float:oper1, Float:oper2);
    78 
    79 /**
    80  * Subtracts oper2 from oper1.
    81  *
    82  * @param oper1			First value.
    83  * @param oper2			Second value.
    84  * @return				oper1-oper2.
    85  */
    86 native Float:FloatSub(Float:oper1, Float:oper2);
    87 
    88 /**
    89  * Returns the decimal part of a float.
    90  *
    91  * @param value			Input value.
    92  * @return				Decimal part.
    93  */
    94 native Float:FloatFraction(Float:value);
    95 
    96 /**
    97  * Rounds a float into an integer number.
    98  *
    99  * @param value			Input value to be rounded.
   100  * @param method		Rounding method to use.
   101  * @return				Rounded value.
   102  */
   103 native FloatRound(Float:value, floatround_method:method=floatround_round);
   104 
   105 /**
   106  * Compares two floats.
   107  *
   108  * @param fOne			First value.
   109  * @param fTwo			Second value.
   110  * @return				Returns 1 if the first argument is greater than the second argument.
   111  *                      Returns -1 if the first argument is smaller than the second argument.
   112  *                      Returns 0 if both arguments are equal.
   113  */
   114 native FloatCompare(Float:fOne, Float:fTwo);
   115 
   116 /**
   117  * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
   118  *
   119  * @param value			Input value.
   120  * @return				Square root of the value.
   121  */
   122 native Float:SquareRoot(Float:value);
   123 
   124 /**
   125  * Returns the value raised to the power of the exponent.
   126  *
   127  * @param value			Value to be raised.
   128  * @param exponent		Value to raise the base.
   129  * @return				value^exponent.
   130  */
   131 native Float:Pow(Float:value, Float:exponent);
   132 
   133 /**
   134  * Returns the value of raising the input by e.
   135  *
   136  * @param value			Input value.
   137  * @return				exp(value).
   138  */
   139 native Float:Exponential(Float:value);
   140 
   141 /**
   142  * Returns the logarithm of any base specified.
   143  *
   144  * @param value			Input value.
   145  * @param base			Logarithm base to use, default is 10.
   146  * @return				log(value)/log(base).
   147  */
   148 native Float:Logarithm(Float:value, Float:base=10.0);
   149 
   150 /**
   151  * Returns the sine of the argument.
   152  *
   153  * @param value			Input value in radians.
   154  * @return				sin(value).
   155  */
   156 native Float:Sine(Float:value);
   157 
   158 /**
   159  * Returns the cosine of the argument.
   160  *
   161  * @param value			Input value in radians.
   162  * @return				cos(value).
   163  */
   164 native Float:Cosine(Float:value);
   165 
   166 /**
   167  * Returns the tangent of the argument.
   168  *
   169  * @param value			Input value in radians.
   170  * @return				tan(value).
   171  */
   172 native Float:Tangent(Float:value);
   173 
   174 /**
   175  * Returns the absolute value.
   176  *
   177  * @param value			Input value.
   178  * @return				abs(value).
   179  */
   180 native Float:FloatAbs(Float:value);
   181 
   182 /**
   183  * Returns the arctangent of the input value.
   184  *
   185  * @param angle			Input value.
   186  * @return				atan(value) in radians.
   187  */
   188 native Float:ArcTangent(Float:angle);
   189 
   190 /**
   191  * Returns the arccosine of the input value.
   192  *
   193  * @param angle			Input value.
   194  * @return				acos(value) in radians.
   195  */
   196 native Float:ArcCosine(Float:angle);
   197 
   198 /**
   199  * Returns the arcsine of the input value.
   200  *
   201  * @param angle			Input value.
   202  * @return				asin(value) in radians.
   203  */
   204 native Float:ArcSine(Float:angle);
   205 
   206 /**
   207  * Returns the arctangent2 of the input values.
   208  *
   209  * @param x				Horizontal value.
   210  * @param y				Vertical value.
   211  * @return				atan2(value) in radians.
   212  */
   213 native Float:ArcTangent2(Float:x, Float:y);
   214 
   215 /**
   216  * User defined operators.
   217  *
   218  */
   219 #pragma rational Float
   220 
   221 native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
   222 native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
   223 native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
   224 native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
   225 
   226 stock Float:operator++(Float:oper)
   227 	return oper+1.0;
   228 
   229 stock Float:operator--(Float:oper)
   230 	return oper-1.0;
   231 
   232 stock Float:operator-(Float:oper)
   233 	return oper^Float:((-1)^((-1)/2));				/* IEEE values are sign/magnitude */
   234 
   235 stock Float:operator*(Float:oper1, oper2)
   236 	return FloatMul(oper1, float(oper2));			/* "*" is commutative */
   237 
   238 stock Float:operator/(Float:oper1, oper2)
   239 	return FloatDiv(oper1, float(oper2));
   240 
   241 stock Float:operator/(oper1, Float:oper2)
   242 	return FloatDiv(float(oper1), oper2);
   243 
   244 stock Float:operator+(Float:oper1, oper2)
   245 	return FloatAdd(oper1, float(oper2));			/* "+" is commutative */
   246 
   247 stock Float:operator-(Float:oper1, oper2)
   248 	return FloatSub(oper1, float(oper2));
   249 
   250 stock Float:operator-(oper1, Float:oper2)
   251 	return FloatSub(float(oper1), oper2);
   252 
   253 stock bool:operator==(Float:oper1, Float:oper2)
   254 	return FloatCompare(oper1, oper2) == 0;
   255 
   256 stock bool:operator==(Float:oper1, oper2)
   257 	return FloatCompare(oper1, float(oper2)) == 0; 	/* "==" is commutative */
   258 
   259 stock bool:operator!=(Float:oper1, Float:oper2)
   260 	return FloatCompare(oper1, oper2) != 0;
   261 
   262 stock bool:operator!=(Float:oper1, oper2)
   263 	return FloatCompare(oper1, float(oper2)) != 0; 	/* "==" is commutative */
   264 
   265 stock bool:operator>(Float:oper1, Float:oper2)
   266 	return FloatCompare(oper1, oper2) > 0;
   267 
   268 stock bool:operator>(Float:oper1, oper2)
   269 	return FloatCompare(oper1, float(oper2)) > 0;
   270 
   271 stock bool:operator>(oper1, Float:oper2)
   272 	return FloatCompare(float(oper1), oper2) > 0;
   273 
   274 stock bool:operator>=(Float:oper1, Float:oper2)
   275 	return FloatCompare(oper1, oper2) >= 0;
   276 
   277 stock bool:operator>=(Float:oper1, oper2)
   278 	return FloatCompare(oper1, float(oper2)) >= 0;
   279 
   280 stock bool:operator>=(oper1, Float:oper2)
   281 	return FloatCompare(float(oper1), oper2) >= 0;
   282 
   283 stock bool:operator<(Float:oper1, Float:oper2)
   284 	return FloatCompare(oper1, oper2) < 0;
   285 
   286 stock bool:operator<(Float:oper1, oper2)
   287 	return FloatCompare(oper1, float(oper2)) < 0;
   288 
   289 stock bool:operator<(oper1, Float:oper2)
   290 	return FloatCompare(float(oper1), oper2) < 0;
   291 
   292 stock bool:operator<=(Float:oper1, Float:oper2)
   293 	return FloatCompare(oper1, oper2) <= 0;
   294 
   295 stock bool:operator<=(Float:oper1, oper2)
   296 	return FloatCompare(oper1, float(oper2)) <= 0;
   297 
   298 stock bool:operator<=(oper1, Float:oper2)
   299 	return FloatCompare(float(oper1), oper2) <= 0;
   300 
   301 stock bool:operator!(Float:oper)
   302 	return (_:oper & ((-1)/2)) == 0;		/* -1 = all bits to 1; /2 = remove most significant bit (sign)
   303 											works on both 32bit and 64bit systems; no constant required */
   304 
   305 /**
   306  * Forbidden operators.
   307  *
   308  */
   309 forward operator%(Float:oper1, Float:oper2);
   310 forward operator%(Float:oper1, oper2);
   311 forward operator%(oper1, Float:oper2);
   312 
   313 #define FLOAT_PI 3.1415926535897932384626433832795
   314 
   315 stock Float:DegToRad(Float:angle)
   316 {
   317 	return (angle*FLOAT_PI)/180;
   318 }
   319 
   320 stock Float:RadToDeg(Float:angle)
   321 {
   322 	return (angle*180)/FLOAT_PI;
   323 }