plugins/include/float.inc
author Scott Ehlert <ds@alliedmods.net>
Sun May 13 07:48:36 2007 +0000 (2007-05-13)
changeset 784 0ff19a8bd981
parent 619 6934bf39171e
child 815 8946ca39d52b
permissions -rw-r--r--
amb283 - Renamed IsPlayerInGame() to IsClientInGame()
     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  * Different methods of rounding.
    23  */
    24 enum floatround_method
    25 {
    26 	floatround_round = 0,		/**< Standard IEEE rounding */
    27 	floatround_floor,			/**< Next lowest integer value. */
    28 	floatround_ceil,			/**< Next highest integer value. */
    29 	floatround_tozero			/** Closest integer to zero. */
    30 };
    31 
    32 /**
    33  * Converts an integer into a floating point value.
    34  *
    35  * @param value			Integer to convert.
    36  * @return				Floating point value.
    37  */
    38 native Float:float(value);
    39 
    40 /**
    41  * Multiplies two floats together.
    42  *
    43  * @param oper1			First value.
    44  * @param oper2			Second value.
    45  * @return				oper1*oper2.
    46  */
    47 native Float:FloatMul(Float:oper1, Float:oper2);
    48 
    49 /**
    50  * Divides the dividend by the divisor.
    51  *
    52  * @param dividend		First value.
    53  * @param divisor		Second value.
    54  * @return				dividend/divisor.
    55  */
    56 native Float:FloatDiv(Float:dividend, Float:divisor);
    57 
    58 /**
    59  * Adds two floats together.
    60  *
    61  * @param oper1			First value.
    62  * @param oper2			Second value.
    63  * @return				oper1+oper2.
    64  */
    65 native Float:FloatAdd(Float:oper1, Float:oper2);
    66 
    67 /**
    68  * Subtracts oper2 from oper1.
    69  *
    70  * @param oper1			First value.
    71  * @param oper2			Second value.
    72  * @return				oper1-oper2.
    73  */
    74 native Float:FloatSub(Float:oper1, Float:oper2);
    75 
    76 /**
    77  * Returns the decimal part of a float.
    78  *
    79  * @param value			Input value.
    80  * @return				Decimal part.
    81  */
    82 native Float:FloatFraction(Float:value);
    83 
    84 /**
    85  * Rounds a float into an integer number.
    86  *
    87  * @param value			Input value to be rounded.
    88  * @param method		Rounding method to use.
    89  * @return				Rounded value.
    90  */
    91 native FloatRound(Float:value, floatround_method:method=floatround_round);
    92 
    93 /**
    94  * Compares two floats.
    95  *
    96  * @param fOne			First value.
    97  * @param fTwo			Second value.
    98  * @return				Returns 1 if the first argument is greater than the second argument.
    99  *                      Returns -1 if the first argument is smaller than the second argument.
   100  *                      Returns 0 if both arguments are equal.
   101  */
   102 native FloatCompare(Float:fOne, Float:fTwo);
   103 
   104 /**
   105  * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
   106  *
   107  * @param value			Input value.
   108  * @return				Square root of the value.
   109  */
   110 native Float:SquareRoot(Float:value);
   111 
   112 /**
   113  * Returns the value raised to the power of the exponent.
   114  *
   115  * @param value			Value to be raised.
   116  * @param exponent		Value to raise the base.
   117  * @return				value^exponent.
   118  */
   119 native Float:Pow(Float:value, Float:exponent);
   120 
   121 /**
   122  * Returns the value of raising the input by e.
   123  *
   124  * @param value			Input value.
   125  * @return				exp(value).
   126  */
   127 native Float:Exponential(Float:value);
   128 
   129 /**
   130  * Returns the logarithm of any base specified.
   131  *
   132  * @param value			Input value.
   133  * @param base			Logarithm base to use, default is 10.
   134  * @return				log(value)/log(base).
   135  */
   136 native Float:Logarithm(Float:value, Float:base=10.0);
   137 
   138 /**
   139  * Returns the sine of the argument.
   140  *
   141  * @param value			Input value in radians.
   142  * @return				sin(value).
   143  */
   144 native Float:Sine(Float:value);
   145 
   146 /**
   147  * Returns the cosine of the argument.
   148  *
   149  * @param value			Input value in radians.
   150  * @return				cos(value).
   151  */
   152 native Float:Cosine(Float:value);
   153 
   154 /**
   155  * Returns the tangent of the argument.
   156  *
   157  * @param value			Input value in radians.
   158  * @return				tan(value).
   159  */
   160 native Float:Tangent(Float:value);
   161 
   162 /**
   163  * Returns an absolute value.
   164  *
   165  * @param value			Input value.
   166  * @return				Absolute value of the input.
   167  */
   168 native Float:FloatAbs(Float:value);
   169 
   170 /**
   171  * Returns the arctangent of the input value.
   172  *
   173  * @param angle			Input value.
   174  * @return				atan(value) in radians.
   175  */
   176 native Float:ArcTangent(Float:angle);
   177 
   178 /**
   179  * Returns the arccosine of the input value.
   180  *
   181  * @param angle			Input value.
   182  * @return				acos(value) in radians.
   183  */
   184 native Float:ArcCosine(Float:angle);
   185 
   186 /**
   187  * Returns the arcsine of the input value.
   188  *
   189  * @param angle			Input value.
   190  * @return				asin(value) in radians.
   191  */
   192 native Float:ArcSine(Float:angle);
   193 
   194 /**
   195  * Returns the arctangent2 of the input values.
   196  *
   197  * @param x				Horizontal value.
   198  * @param y				Vertical value.
   199  * @return				atan2(value) in radians.
   200  */
   201 native Float:ArcTangent2(Float:x, Float:y);
   202 
   203 /**
   204  * User defined operators.
   205  *
   206  */
   207 #pragma rational Float
   208 
   209 native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
   210 native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
   211 native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
   212 native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
   213 
   214 stock Float:operator++(Float:oper)
   215 	return oper+1.0;
   216 
   217 stock Float:operator--(Float:oper)
   218 	return oper-1.0;
   219 
   220 stock Float:operator-(Float:oper)
   221 	return oper^Float:((-1)^((-1)/2));				/* IEEE values are sign/magnitude */
   222 
   223 stock Float:operator*(Float:oper1, oper2)
   224 	return FloatMul(oper1, float(oper2));			/* "*" is commutative */
   225 
   226 stock Float:operator/(Float:oper1, oper2)
   227 	return FloatDiv(oper1, float(oper2));
   228 
   229 stock Float:operator/(oper1, Float:oper2)
   230 	return FloatDiv(float(oper1), oper2);
   231 
   232 stock Float:operator+(Float:oper1, oper2)
   233 	return FloatAdd(oper1, float(oper2));			/* "+" is commutative */
   234 
   235 stock Float:operator-(Float:oper1, oper2)
   236 	return FloatSub(oper1, float(oper2));
   237 
   238 stock Float:operator-(oper1, Float:oper2)
   239 	return FloatSub(float(oper1), oper2);
   240 
   241 stock bool:operator==(Float:oper1, Float:oper2)
   242 	return FloatCompare(oper1, oper2) == 0;
   243 
   244 stock bool:operator==(Float:oper1, oper2)
   245 	return FloatCompare(oper1, float(oper2)) == 0; 	/* "==" is commutative */
   246 
   247 stock bool:operator!=(Float:oper1, Float:oper2)
   248 	return FloatCompare(oper1, oper2) != 0;
   249 
   250 stock bool:operator!=(Float:oper1, oper2)
   251 	return FloatCompare(oper1, float(oper2)) != 0; 	/* "==" is commutative */
   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;
   258 
   259 stock bool:operator>(oper1, Float:oper2)
   260 	return FloatCompare(float(oper1), oper2) > 0;
   261 
   262 stock bool:operator>=(Float:oper1, Float:oper2)
   263 	return FloatCompare(oper1, oper2) >= 0;
   264 
   265 stock bool:operator>=(Float:oper1, oper2)
   266 	return FloatCompare(oper1, float(oper2)) >= 0;
   267 
   268 stock bool:operator>=(oper1, Float:oper2)
   269 	return FloatCompare(float(oper1), oper2) >= 0;
   270 
   271 stock bool:operator<(Float:oper1, Float:oper2)
   272 	return FloatCompare(oper1, oper2) < 0;
   273 
   274 stock bool:operator<(Float:oper1, oper2)
   275 	return FloatCompare(oper1, float(oper2)) < 0;
   276 
   277 stock bool:operator<(oper1, Float:oper2)
   278 	return FloatCompare(float(oper1), oper2) < 0;
   279 
   280 stock bool:operator<=(Float:oper1, Float:oper2)
   281 	return FloatCompare(oper1, oper2) <= 0;
   282 
   283 stock bool:operator<=(Float:oper1, oper2)
   284 	return FloatCompare(oper1, float(oper2)) <= 0;
   285 
   286 stock bool:operator<=(oper1, Float:oper2)
   287 	return FloatCompare(float(oper1), oper2) <= 0;
   288 
   289 stock bool:operator!(Float:oper)
   290 	return (_:oper & ((-1)/2)) == 0;		/* -1 = all bits to 1; /2 = remove most significant bit (sign)
   291 											works on both 32bit and 64bit systems; no constant required */
   292 
   293 /**
   294  * Forbidden operators.
   295  *
   296  */
   297 forward operator%(Float:oper1, Float:oper2);
   298 forward operator%(Float:oper1, oper2);
   299 forward operator%(oper1, Float:oper2);
   300 
   301 #define FLOAT_PI 3.1415926535897932384626433832795
   302 
   303 /**
   304  * Converts degrees to radians.
   305  *
   306  * @param angle		Degrees.
   307  * @return			Radians.
   308  */
   309 stock Float:DegToRad(Float:angle)
   310 {
   311 	return (angle*FLOAT_PI)/180;
   312 }
   313 
   314 /**
   315  * Converts degrees to radians.
   316  *
   317  * @param angle		Radians.
   318  * @return			Degrees.
   319  */
   320 stock Float:RadToDeg(Float:angle)
   321 {
   322 	return (angle*180)/FLOAT_PI;
   323 }