plugins/include/float.inc
author David Anderson <dvander@alliedmods.net>
Tue Oct 02 14:48:44 2007 +0000 (2007-10-02)
changeset 1525 bca151238f5e
parent 1330 ab94316bb52b
child 1936 89d6e5038933
permissions -rw-r--r--
- removed incorrect information about DrawOption, renamed to DisplayOption
- renamed DrawTitle to DisplayTitle
     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.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32  
    33 #if defined _float_included
    34  #endinput
    35 #endif
    36 #define _float_included
    37 
    38 /**
    39  * Converts an integer into a floating point value.
    40  *
    41  * @param value			Integer to convert.
    42  * @return				Floating point value.
    43  */
    44 native Float:float(value);
    45 
    46 /**
    47  * Multiplies two floats together.
    48  *
    49  * @param oper1			First value.
    50  * @param oper2			Second value.
    51  * @return				oper1*oper2.
    52  */
    53 native Float:FloatMul(Float:oper1, Float:oper2);
    54 
    55 /**
    56  * Divides the dividend by the divisor.
    57  *
    58  * @param dividend		First value.
    59  * @param divisor		Second value.
    60  * @return				dividend/divisor.
    61  */
    62 native Float:FloatDiv(Float:dividend, Float:divisor);
    63 
    64 /**
    65  * Adds two floats together.
    66  *
    67  * @param oper1			First value.
    68  * @param oper2			Second value.
    69  * @return				oper1+oper2.
    70  */
    71 native Float:FloatAdd(Float:oper1, Float:oper2);
    72 
    73 /**
    74  * Subtracts oper2 from oper1.
    75  *
    76  * @param oper1			First value.
    77  * @param oper2			Second value.
    78  * @return				oper1-oper2.
    79  */
    80 native Float:FloatSub(Float:oper1, Float:oper2);
    81 
    82 /**
    83  * Returns the decimal part of a float.
    84  *
    85  * @param value			Input value.
    86  * @return				Decimal part.
    87  */
    88 native Float:FloatFraction(Float:value);
    89 
    90 /**
    91  * Rounds a float to the closest integer to zero.
    92  *
    93  * @param value			Input value to be rounded.
    94  * @return				Rounded value.
    95  */
    96 native RoundToZero(Float:value);
    97 
    98 /**
    99  * Rounds a float to the next highest integer value.
   100  *
   101  * @param value			Input value to be rounded.
   102  * @return				Rounded value.
   103  */
   104 native RoundToCeil(Float:value);
   105 
   106 /**
   107  * Rounds a float to the next lowest integer value.
   108  *
   109  * @param value			Input value to be rounded.
   110  * @return				Rounded value.
   111  */
   112 native RoundToFloor(Float:value);
   113 
   114 /**
   115  * Standard IEEE rounding.
   116  *
   117  * @param value			Input value to be rounded.
   118  * @return				Rounded value.
   119  */
   120 native RoundToNearest(Float:value);
   121 
   122 /**
   123  * Compares two floats.
   124  *
   125  * @param fOne			First value.
   126  * @param fTwo			Second value.
   127  * @return				Returns 1 if the first argument is greater than the second argument.
   128  *                      Returns -1 if the first argument is smaller than the second argument.
   129  *                      Returns 0 if both arguments are equal.
   130  */
   131 native FloatCompare(Float:fOne, Float:fTwo);
   132 
   133 /**
   134  * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
   135  *
   136  * @param value			Input value.
   137  * @return				Square root of the value.
   138  */
   139 native Float:SquareRoot(Float:value);
   140 
   141 /**
   142  * Returns the value raised to the power of the exponent.
   143  *
   144  * @param value			Value to be raised.
   145  * @param exponent		Value to raise the base.
   146  * @return				value^exponent.
   147  */
   148 native Float:Pow(Float:value, Float:exponent);
   149 
   150 /**
   151  * Returns the value of raising the input by e.
   152  *
   153  * @param value			Input value.
   154  * @return				exp(value).
   155  */
   156 native Float:Exponential(Float:value);
   157 
   158 /**
   159  * Returns the logarithm of any base specified.
   160  *
   161  * @param value			Input value.
   162  * @param base			Logarithm base to use, default is 10.
   163  * @return				log(value)/log(base).
   164  */
   165 native Float:Logarithm(Float:value, Float:base=10.0);
   166 
   167 /**
   168  * Returns the sine of the argument.
   169  *
   170  * @param value			Input value in radians.
   171  * @return				sin(value).
   172  */
   173 native Float:Sine(Float:value);
   174 
   175 /**
   176  * Returns the cosine of the argument.
   177  *
   178  * @param value			Input value in radians.
   179  * @return				cos(value).
   180  */
   181 native Float:Cosine(Float:value);
   182 
   183 /**
   184  * Returns the tangent of the argument.
   185  *
   186  * @param value			Input value in radians.
   187  * @return				tan(value).
   188  */
   189 native Float:Tangent(Float:value);
   190 
   191 /**
   192  * Returns an absolute value.
   193  *
   194  * @param value			Input value.
   195  * @return				Absolute value of the input.
   196  */
   197 native Float:FloatAbs(Float:value);
   198 
   199 /**
   200  * Returns the arctangent of the input value.
   201  *
   202  * @param angle			Input value.
   203  * @return				atan(value) in radians.
   204  */
   205 native Float:ArcTangent(Float:angle);
   206 
   207 /**
   208  * Returns the arccosine of the input value.
   209  *
   210  * @param angle			Input value.
   211  * @return				acos(value) in radians.
   212  */
   213 native Float:ArcCosine(Float:angle);
   214 
   215 /**
   216  * Returns the arcsine of the input value.
   217  *
   218  * @param angle			Input value.
   219  * @return				asin(value) in radians.
   220  */
   221 native Float:ArcSine(Float:angle);
   222 
   223 /**
   224  * Returns the arctangent2 of the input values.
   225  *
   226  * @param x				Horizontal value.
   227  * @param y				Vertical value.
   228  * @return				atan2(value) in radians.
   229  */
   230 native Float:ArcTangent2(Float:x, Float:y);
   231 
   232 /**
   233  * Rounds a floating point number using the "round to nearest" algorithm.
   234  *
   235  * @param value			Floating point value to round.
   236  * @return				The value rounded to the nearest integer.
   237  */
   238 stock RoundFloat(Float:value)
   239 {
   240 	return RoundToNearest(value);
   241 }
   242 
   243 /**
   244  * Backwards compatibility stock.  Do not use.
   245  *
   246  * @deprecated
   247  */
   248 #pragma deprecated Use RoundToNearest() or RoundFloat() instead
   249 stock FloatRound(Float:value)
   250 {
   251 	return RoundToNearest(value);
   252 }
   253 
   254 /**
   255  * User defined operators.
   256  *
   257  */
   258 #pragma rational Float
   259 
   260 native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
   261 native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
   262 native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
   263 native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
   264 
   265 stock Float:operator++(Float:oper)
   266 {
   267 	return oper+1.0;
   268 }
   269 
   270 stock Float:operator--(Float:oper)
   271 {
   272 	return oper-1.0;
   273 }
   274 
   275 stock Float:operator-(Float:oper)
   276 {
   277 	return oper^Float:((-1)^((-1)/2));				/* IEEE values are sign/magnitude */
   278 }
   279 
   280 stock Float:operator*(Float:oper1, oper2)
   281 {
   282 	return FloatMul(oper1, float(oper2));			/* "*" is commutative */
   283 }
   284 
   285 stock Float:operator/(Float:oper1, oper2)
   286 {
   287 	return FloatDiv(oper1, float(oper2));
   288 }
   289 
   290 stock Float:operator/(oper1, Float:oper2)
   291 {
   292 	return FloatDiv(float(oper1), oper2);
   293 }
   294 
   295 stock Float:operator+(Float:oper1, oper2)
   296 {
   297 	return FloatAdd(oper1, float(oper2));			/* "+" is commutative */
   298 }
   299 
   300 stock Float:operator-(Float:oper1, oper2)
   301 {
   302 	return FloatSub(oper1, float(oper2));
   303 }
   304 
   305 stock Float:operator-(oper1, Float:oper2)
   306 {
   307 	return FloatSub(float(oper1), oper2);
   308 }
   309 
   310 stock bool:operator==(Float:oper1, Float:oper2)
   311 {
   312 	return FloatCompare(oper1, oper2) == 0;
   313 }
   314 
   315 stock bool:operator==(Float:oper1, oper2)
   316 {
   317 	return FloatCompare(oper1, float(oper2)) == 0; 	/* "==" is commutative */
   318 }
   319 
   320 stock bool:operator!=(Float:oper1, Float:oper2)
   321 {
   322 	return FloatCompare(oper1, oper2) != 0;
   323 }
   324 
   325 stock bool:operator!=(Float:oper1, oper2)
   326 {
   327 	return FloatCompare(oper1, float(oper2)) != 0; 	/* "==" is commutative */
   328 }
   329 
   330 stock bool:operator>(Float:oper1, Float:oper2)
   331 {
   332 	return FloatCompare(oper1, oper2) > 0;
   333 }
   334 
   335 stock bool:operator>(Float:oper1, oper2)
   336 {
   337 	return FloatCompare(oper1, float(oper2)) > 0;
   338 }
   339 
   340 stock bool:operator>(oper1, Float:oper2)
   341 {
   342 	return FloatCompare(float(oper1), oper2) > 0;
   343 }
   344 
   345 stock bool:operator>=(Float:oper1, Float:oper2)
   346 {
   347 	return FloatCompare(oper1, oper2) >= 0;
   348 }
   349 
   350 stock bool:operator>=(Float:oper1, oper2)
   351 {
   352 	return FloatCompare(oper1, float(oper2)) >= 0;
   353 }
   354 
   355 stock bool:operator>=(oper1, Float:oper2)
   356 {
   357 	return FloatCompare(float(oper1), oper2) >= 0;
   358 }
   359 
   360 stock bool:operator<(Float:oper1, Float:oper2)
   361 {
   362 	return FloatCompare(oper1, oper2) < 0;
   363 }
   364 
   365 stock bool:operator<(Float:oper1, oper2)
   366 {
   367 	return FloatCompare(oper1, float(oper2)) < 0;
   368 }
   369 
   370 stock bool:operator<(oper1, Float:oper2)
   371 {
   372 	return FloatCompare(float(oper1), oper2) < 0;
   373 }
   374 
   375 stock bool:operator<=(Float:oper1, Float:oper2)
   376 {
   377 	return FloatCompare(oper1, oper2) <= 0;
   378 }
   379 
   380 stock bool:operator<=(Float:oper1, oper2)
   381 {
   382 	return FloatCompare(oper1, float(oper2)) <= 0;
   383 }
   384 
   385 stock bool:operator<=(oper1, Float:oper2)
   386 {
   387 	return FloatCompare(float(oper1), oper2) <= 0;
   388 }
   389 
   390 stock bool:operator!(Float:oper)
   391 {
   392 	return (_:oper & ((-1)/2)) == 0;		/* -1 = all bits to 1; /2 = remove most significant bit (sign)
   393 											works on both 32bit and 64bit systems; no constant required */
   394 }
   395 
   396 /**
   397  * Forbidden operators.
   398  *
   399  */
   400 forward operator%(Float:oper1, Float:oper2);
   401 forward operator%(Float:oper1, oper2);
   402 forward operator%(oper1, Float:oper2);
   403 
   404 #define FLOAT_PI 3.1415926535897932384626433832795
   405 
   406 /**
   407  * Converts degrees to radians.
   408  *
   409  * @param angle		Degrees.
   410  * @return			Radians.
   411  */
   412 stock Float:DegToRad(Float:angle)
   413 {
   414 	return (angle*FLOAT_PI)/180;
   415 }
   416 
   417 /**
   418  * Converts degrees to radians.
   419  *
   420  * @param angle		Radians.
   421  * @return			Degrees.
   422  */
   423 stock Float:RadToDeg(Float:angle)
   424 {
   425 	return (angle*180)/FLOAT_PI;
   426 }