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