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