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