plugins/include/float.inc
author Phong Dang <teame06@gmail.com>
Sun Jul 29 00:26:18 2007 +0000 (2007-07-29)
changeset 1200 623603b49096
parent 851 66d23844a8aa
child 1268 9360f5ea2289
permissions -rw-r--r--
Fixed AskPluginLoad would still allowed plugin to load even if it returned false.
     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  * Converts an integer into a floating point value.
    23  *
    24  * @param value			Integer to convert.
    25  * @return				Floating point value.
    26  */
    27 native Float:float(value);
    28 
    29 /**
    30  * Multiplies two floats together.
    31  *
    32  * @param oper1			First value.
    33  * @param oper2			Second value.
    34  * @return				oper1*oper2.
    35  */
    36 native Float:FloatMul(Float:oper1, Float:oper2);
    37 
    38 /**
    39  * Divides the dividend by the divisor.
    40  *
    41  * @param dividend		First value.
    42  * @param divisor		Second value.
    43  * @return				dividend/divisor.
    44  */
    45 native Float:FloatDiv(Float:dividend, Float:divisor);
    46 
    47 /**
    48  * Adds two floats together.
    49  *
    50  * @param oper1			First value.
    51  * @param oper2			Second value.
    52  * @return				oper1+oper2.
    53  */
    54 native Float:FloatAdd(Float:oper1, Float:oper2);
    55 
    56 /**
    57  * Subtracts oper2 from oper1.
    58  *
    59  * @param oper1			First value.
    60  * @param oper2			Second value.
    61  * @return				oper1-oper2.
    62  */
    63 native Float:FloatSub(Float:oper1, Float:oper2);
    64 
    65 /**
    66  * Returns the decimal part of a float.
    67  *
    68  * @param value			Input value.
    69  * @return				Decimal part.
    70  */
    71 native Float:FloatFraction(Float:value);
    72 
    73 /**
    74  * Rounds a float to the closest integer to zero.
    75  *
    76  * @param value			Input value to be rounded.
    77  * @return				Rounded value.
    78  */
    79 native RoundToZero(Float:value);
    80 
    81 /**
    82  * Rounds a float to the next highest integer value.
    83  *
    84  * @param value			Input value to be rounded.
    85  * @return				Rounded value.
    86  */
    87 native RoundToCeil(Float:value);
    88 
    89 /**
    90  * Rounds a float to the next lowest integer value.
    91  *
    92  * @param value			Input value to be rounded.
    93  * @return				Rounded value.
    94  */
    95 native RoundToFloor(Float:value);
    96 
    97 /**
    98  * Standard IEEE rounding.
    99  *
   100  * @param value			Input value to be rounded.
   101  * @return				Rounded value.
   102  */
   103 native RoundToNearest(Float:value);
   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 an absolute value.
   176  *
   177  * @param value			Input value.
   178  * @return				Absolute value of the input.
   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  * Rounds a floating point number using the "round to nearest" algorithm.
   217  *
   218  * @param value			Floating point value to round.
   219  * @return				The value rounded to the nearest integer.
   220  */
   221 stock RoundFloat(Float:value)
   222 {
   223 	return RoundToNearest(value);
   224 }
   225 
   226 /**
   227  * Backwards compatibility stock.  Do not use.
   228  *
   229  * @deprecated
   230  */
   231 stock FloatRound(Float:value)
   232 {
   233 	return RoundToNearest(value);
   234 }
   235 
   236 /**
   237  * User defined operators.
   238  *
   239  */
   240 #pragma rational Float
   241 
   242 native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
   243 native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
   244 native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
   245 native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
   246 
   247 stock Float:operator++(Float:oper)
   248 {
   249 	return oper+1.0;
   250 }
   251 
   252 stock Float:operator--(Float:oper)
   253 {
   254 	return oper-1.0;
   255 }
   256 
   257 stock Float:operator-(Float:oper)
   258 {
   259 	return oper^Float:((-1)^((-1)/2));				/* IEEE values are sign/magnitude */
   260 }
   261 
   262 stock Float:operator*(Float:oper1, oper2)
   263 {
   264 	return FloatMul(oper1, float(oper2));			/* "*" is commutative */
   265 }
   266 
   267 stock Float:operator/(Float:oper1, oper2)
   268 {
   269 	return FloatDiv(oper1, float(oper2));
   270 }
   271 
   272 stock Float:operator/(oper1, Float:oper2)
   273 {
   274 	return FloatDiv(float(oper1), oper2);
   275 }
   276 
   277 stock Float:operator+(Float:oper1, oper2)
   278 {
   279 	return FloatAdd(oper1, float(oper2));			/* "+" is commutative */
   280 }
   281 
   282 stock Float:operator-(Float:oper1, oper2)
   283 {
   284 	return FloatSub(oper1, float(oper2));
   285 }
   286 
   287 stock Float:operator-(oper1, Float:oper2)
   288 {
   289 	return FloatSub(float(oper1), oper2);
   290 }
   291 
   292 stock bool:operator==(Float:oper1, Float:oper2)
   293 {
   294 	return FloatCompare(oper1, oper2) == 0;
   295 }
   296 
   297 stock bool:operator==(Float:oper1, oper2)
   298 {
   299 	return FloatCompare(oper1, float(oper2)) == 0; 	/* "==" is commutative */
   300 }
   301 
   302 stock bool:operator!=(Float:oper1, Float:oper2)
   303 {
   304 	return FloatCompare(oper1, oper2) != 0;
   305 }
   306 
   307 stock bool:operator!=(Float:oper1, oper2)
   308 {
   309 	return FloatCompare(oper1, float(oper2)) != 0; 	/* "==" is commutative */
   310 }
   311 
   312 stock bool:operator>(Float:oper1, Float:oper2)
   313 {
   314 	return FloatCompare(oper1, oper2) > 0;
   315 }
   316 
   317 stock bool:operator>(Float:oper1, oper2)
   318 {
   319 	return FloatCompare(oper1, float(oper2)) > 0;
   320 }
   321 
   322 stock bool:operator>(oper1, Float:oper2)
   323 {
   324 	return FloatCompare(float(oper1), oper2) > 0;
   325 }
   326 
   327 stock bool:operator>=(Float:oper1, Float:oper2)
   328 {
   329 	return FloatCompare(oper1, oper2) >= 0;
   330 }
   331 
   332 stock bool:operator>=(Float:oper1, oper2)
   333 {
   334 	return FloatCompare(oper1, float(oper2)) >= 0;
   335 }
   336 
   337 stock bool:operator>=(oper1, Float:oper2)
   338 {
   339 	return FloatCompare(float(oper1), oper2) >= 0;
   340 }
   341 
   342 stock bool:operator<(Float:oper1, Float:oper2)
   343 {
   344 	return FloatCompare(oper1, oper2) < 0;
   345 }
   346 
   347 stock bool:operator<(Float:oper1, oper2)
   348 {
   349 	return FloatCompare(oper1, float(oper2)) < 0;
   350 }
   351 
   352 stock bool:operator<(oper1, Float:oper2)
   353 {
   354 	return FloatCompare(float(oper1), oper2) < 0;
   355 }
   356 
   357 stock bool:operator<=(Float:oper1, Float:oper2)
   358 {
   359 	return FloatCompare(oper1, oper2) <= 0;
   360 }
   361 
   362 stock bool:operator<=(Float:oper1, oper2)
   363 {
   364 	return FloatCompare(oper1, float(oper2)) <= 0;
   365 }
   366 
   367 stock bool:operator<=(oper1, Float:oper2)
   368 {
   369 	return FloatCompare(float(oper1), oper2) <= 0;
   370 }
   371 
   372 stock bool:operator!(Float:oper)
   373 {
   374 	return (_:oper & ((-1)/2)) == 0;		/* -1 = all bits to 1; /2 = remove most significant bit (sign)
   375 											works on both 32bit and 64bit systems; no constant required */
   376 }
   377 
   378 /**
   379  * Forbidden operators.
   380  *
   381  */
   382 forward operator%(Float:oper1, Float:oper2);
   383 forward operator%(Float:oper1, oper2);
   384 forward operator%(oper1, Float:oper2);
   385 
   386 #define FLOAT_PI 3.1415926535897932384626433832795
   387 
   388 /**
   389  * Converts degrees to radians.
   390  *
   391  * @param angle		Degrees.
   392  * @return			Radians.
   393  */
   394 stock Float:DegToRad(Float:angle)
   395 {
   396 	return (angle*FLOAT_PI)/180;
   397 }
   398 
   399 /**
   400  * Converts degrees to radians.
   401  *
   402  * @param angle		Radians.
   403  * @return			Degrees.
   404  */
   405 stock Float:RadToDeg(Float:angle)
   406 {
   407 	return (angle*180)/FLOAT_PI;
   408 }