plugins/include/float.inc
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/include/float.inc	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,415 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     1.8 + * =============================================================================
     1.9 + *
    1.10 + * This file is part of the SourceMod/SourcePawn SDK.
    1.11 + *
    1.12 + * This program is free software; you can redistribute it and/or modify it under
    1.13 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.14 + * Free Software Foundation.
    1.15 + * 
    1.16 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.17 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.18 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.19 + * details.
    1.20 + *
    1.21 + * You should have received a copy of the GNU General Public License along with
    1.22 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.23 + *
    1.24 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.25 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.26 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.27 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.28 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.29 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.30 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.31 + * or <http://www.sourcemod.net/license.php>.
    1.32 + *
    1.33 + * Version: $Id$
    1.34 + */
    1.35 + 
    1.36 +#if defined _float_included
    1.37 + #endinput
    1.38 +#endif
    1.39 +#define _float_included
    1.40 +
    1.41 +/**
    1.42 + * Converts an integer into a floating point value.
    1.43 + *
    1.44 + * @param value			Integer to convert.
    1.45 + * @return				Floating point value.
    1.46 + */
    1.47 +native Float:float(value);
    1.48 +
    1.49 +/**
    1.50 + * Multiplies two floats together.
    1.51 + *
    1.52 + * @param oper1			First value.
    1.53 + * @param oper2			Second value.
    1.54 + * @return				oper1*oper2.
    1.55 + */
    1.56 +native Float:FloatMul(Float:oper1, Float:oper2);
    1.57 +
    1.58 +/**
    1.59 + * Divides the dividend by the divisor.
    1.60 + *
    1.61 + * @param dividend		First value.
    1.62 + * @param divisor		Second value.
    1.63 + * @return				dividend/divisor.
    1.64 + */
    1.65 +native Float:FloatDiv(Float:dividend, Float:divisor);
    1.66 +
    1.67 +/**
    1.68 + * Adds two floats together.
    1.69 + *
    1.70 + * @param oper1			First value.
    1.71 + * @param oper2			Second value.
    1.72 + * @return				oper1+oper2.
    1.73 + */
    1.74 +native Float:FloatAdd(Float:oper1, Float:oper2);
    1.75 +
    1.76 +/**
    1.77 + * Subtracts oper2 from oper1.
    1.78 + *
    1.79 + * @param oper1			First value.
    1.80 + * @param oper2			Second value.
    1.81 + * @return				oper1-oper2.
    1.82 + */
    1.83 +native Float:FloatSub(Float:oper1, Float:oper2);
    1.84 +
    1.85 +/**
    1.86 + * Returns the decimal part of a float.
    1.87 + *
    1.88 + * @param value			Input value.
    1.89 + * @return				Decimal part.
    1.90 + */
    1.91 +native Float:FloatFraction(Float:value);
    1.92 +
    1.93 +/**
    1.94 + * Rounds a float to the closest integer to zero.
    1.95 + *
    1.96 + * @param value			Input value to be rounded.
    1.97 + * @return				Rounded value.
    1.98 + */
    1.99 +native RoundToZero(Float:value);
   1.100 +
   1.101 +/**
   1.102 + * Rounds a float to the next highest integer value.
   1.103 + *
   1.104 + * @param value			Input value to be rounded.
   1.105 + * @return				Rounded value.
   1.106 + */
   1.107 +native RoundToCeil(Float:value);
   1.108 +
   1.109 +/**
   1.110 + * Rounds a float to the next lowest integer value.
   1.111 + *
   1.112 + * @param value			Input value to be rounded.
   1.113 + * @return				Rounded value.
   1.114 + */
   1.115 +native RoundToFloor(Float:value);
   1.116 +
   1.117 +/**
   1.118 + * Standard IEEE rounding.
   1.119 + *
   1.120 + * @param value			Input value to be rounded.
   1.121 + * @return				Rounded value.
   1.122 + */
   1.123 +native RoundToNearest(Float:value);
   1.124 +
   1.125 +/**
   1.126 + * Compares two floats.
   1.127 + *
   1.128 + * @param fOne			First value.
   1.129 + * @param fTwo			Second value.
   1.130 + * @return				Returns 1 if the first argument is greater than the second argument.
   1.131 + *                      Returns -1 if the first argument is smaller than the second argument.
   1.132 + *                      Returns 0 if both arguments are equal.
   1.133 + */
   1.134 +native FloatCompare(Float:fOne, Float:fTwo);
   1.135 +
   1.136 +/**
   1.137 + * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
   1.138 + *
   1.139 + * @param value			Input value.
   1.140 + * @return				Square root of the value.
   1.141 + */
   1.142 +native Float:SquareRoot(Float:value);
   1.143 +
   1.144 +/**
   1.145 + * Returns the value raised to the power of the exponent.
   1.146 + *
   1.147 + * @param value			Value to be raised.
   1.148 + * @param exponent		Value to raise the base.
   1.149 + * @return				value^exponent.
   1.150 + */
   1.151 +native Float:Pow(Float:value, Float:exponent);
   1.152 +
   1.153 +/**
   1.154 + * Returns the value of raising the input by e.
   1.155 + *
   1.156 + * @param value			Input value.
   1.157 + * @return				exp(value).
   1.158 + */
   1.159 +native Float:Exponential(Float:value);
   1.160 +
   1.161 +/**
   1.162 + * Returns the logarithm of any base specified.
   1.163 + *
   1.164 + * @param value			Input value.
   1.165 + * @param base			Logarithm base to use, default is 10.
   1.166 + * @return				log(value)/log(base).
   1.167 + */
   1.168 +native Float:Logarithm(Float:value, Float:base=10.0);
   1.169 +
   1.170 +/**
   1.171 + * Returns the sine of the argument.
   1.172 + *
   1.173 + * @param value			Input value in radians.
   1.174 + * @return				sin(value).
   1.175 + */
   1.176 +native Float:Sine(Float:value);
   1.177 +
   1.178 +/**
   1.179 + * Returns the cosine of the argument.
   1.180 + *
   1.181 + * @param value			Input value in radians.
   1.182 + * @return				cos(value).
   1.183 + */
   1.184 +native Float:Cosine(Float:value);
   1.185 +
   1.186 +/**
   1.187 + * Returns the tangent of the argument.
   1.188 + *
   1.189 + * @param value			Input value in radians.
   1.190 + * @return				tan(value).
   1.191 + */
   1.192 +native Float:Tangent(Float:value);
   1.193 +
   1.194 +/**
   1.195 + * Returns an absolute value.
   1.196 + *
   1.197 + * @param value			Input value.
   1.198 + * @return				Absolute value of the input.
   1.199 + */
   1.200 +native Float:FloatAbs(Float:value);
   1.201 +
   1.202 +/**
   1.203 + * Returns the arctangent of the input value.
   1.204 + *
   1.205 + * @param angle			Input value.
   1.206 + * @return				atan(value) in radians.
   1.207 + */
   1.208 +native Float:ArcTangent(Float:angle);
   1.209 +
   1.210 +/**
   1.211 + * Returns the arccosine of the input value.
   1.212 + *
   1.213 + * @param angle			Input value.
   1.214 + * @return				acos(value) in radians.
   1.215 + */
   1.216 +native Float:ArcCosine(Float:angle);
   1.217 +
   1.218 +/**
   1.219 + * Returns the arcsine of the input value.
   1.220 + *
   1.221 + * @param angle			Input value.
   1.222 + * @return				asin(value) in radians.
   1.223 + */
   1.224 +native Float:ArcSine(Float:angle);
   1.225 +
   1.226 +/**
   1.227 + * Returns the arctangent2 of the input values.
   1.228 + *
   1.229 + * @param x				Horizontal value.
   1.230 + * @param y				Vertical value.
   1.231 + * @return				atan2(value) in radians.
   1.232 + */
   1.233 +native Float:ArcTangent2(Float:x, Float:y);
   1.234 +
   1.235 +/**
   1.236 + * Rounds a floating point number using the "round to nearest" algorithm.
   1.237 + *
   1.238 + * @param value			Floating point value to round.
   1.239 + * @return				The value rounded to the nearest integer.
   1.240 + */
   1.241 +stock RoundFloat(Float:value)
   1.242 +{
   1.243 +	return RoundToNearest(value);
   1.244 +}
   1.245 +
   1.246 +/**
   1.247 + * User defined operators.
   1.248 + *
   1.249 + */
   1.250 +#pragma rational Float
   1.251 +
   1.252 +native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
   1.253 +native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
   1.254 +native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
   1.255 +native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
   1.256 +
   1.257 +stock Float:operator++(Float:oper)
   1.258 +{
   1.259 +	return oper+1.0;
   1.260 +}
   1.261 +
   1.262 +stock Float:operator--(Float:oper)
   1.263 +{
   1.264 +	return oper-1.0;
   1.265 +}
   1.266 +
   1.267 +stock Float:operator-(Float:oper)
   1.268 +{
   1.269 +	return oper^Float:((-1)^((-1)/2));				/* IEEE values are sign/magnitude */
   1.270 +}
   1.271 +
   1.272 +stock Float:operator*(Float:oper1, oper2)
   1.273 +{
   1.274 +	return FloatMul(oper1, float(oper2));			/* "*" is commutative */
   1.275 +}
   1.276 +
   1.277 +stock Float:operator/(Float:oper1, oper2)
   1.278 +{
   1.279 +	return FloatDiv(oper1, float(oper2));
   1.280 +}
   1.281 +
   1.282 +stock Float:operator/(oper1, Float:oper2)
   1.283 +{
   1.284 +	return FloatDiv(float(oper1), oper2);
   1.285 +}
   1.286 +
   1.287 +stock Float:operator+(Float:oper1, oper2)
   1.288 +{
   1.289 +	return FloatAdd(oper1, float(oper2));			/* "+" is commutative */
   1.290 +}
   1.291 +
   1.292 +stock Float:operator-(Float:oper1, oper2)
   1.293 +{
   1.294 +	return FloatSub(oper1, float(oper2));
   1.295 +}
   1.296 +
   1.297 +stock Float:operator-(oper1, Float:oper2)
   1.298 +{
   1.299 +	return FloatSub(float(oper1), oper2);
   1.300 +}
   1.301 +
   1.302 +stock bool:operator==(Float:oper1, Float:oper2)
   1.303 +{
   1.304 +	return FloatCompare(oper1, oper2) == 0;
   1.305 +}
   1.306 +
   1.307 +stock bool:operator==(Float:oper1, oper2)
   1.308 +{
   1.309 +	return FloatCompare(oper1, float(oper2)) == 0; 	/* "==" is commutative */
   1.310 +}
   1.311 +
   1.312 +stock bool:operator!=(Float:oper1, Float:oper2)
   1.313 +{
   1.314 +	return FloatCompare(oper1, oper2) != 0;
   1.315 +}
   1.316 +
   1.317 +stock bool:operator!=(Float:oper1, oper2)
   1.318 +{
   1.319 +	return FloatCompare(oper1, float(oper2)) != 0; 	/* "==" is commutative */
   1.320 +}
   1.321 +
   1.322 +stock bool:operator>(Float:oper1, Float:oper2)
   1.323 +{
   1.324 +	return FloatCompare(oper1, oper2) > 0;
   1.325 +}
   1.326 +
   1.327 +stock bool:operator>(Float:oper1, oper2)
   1.328 +{
   1.329 +	return FloatCompare(oper1, float(oper2)) > 0;
   1.330 +}
   1.331 +
   1.332 +stock bool:operator>(oper1, Float:oper2)
   1.333 +{
   1.334 +	return FloatCompare(float(oper1), oper2) > 0;
   1.335 +}
   1.336 +
   1.337 +stock bool:operator>=(Float:oper1, Float:oper2)
   1.338 +{
   1.339 +	return FloatCompare(oper1, oper2) >= 0;
   1.340 +}
   1.341 +
   1.342 +stock bool:operator>=(Float:oper1, oper2)
   1.343 +{
   1.344 +	return FloatCompare(oper1, float(oper2)) >= 0;
   1.345 +}
   1.346 +
   1.347 +stock bool:operator>=(oper1, Float:oper2)
   1.348 +{
   1.349 +	return FloatCompare(float(oper1), oper2) >= 0;
   1.350 +}
   1.351 +
   1.352 +stock bool:operator<(Float:oper1, Float:oper2)
   1.353 +{
   1.354 +	return FloatCompare(oper1, oper2) < 0;
   1.355 +}
   1.356 +
   1.357 +stock bool:operator<(Float:oper1, oper2)
   1.358 +{
   1.359 +	return FloatCompare(oper1, float(oper2)) < 0;
   1.360 +}
   1.361 +
   1.362 +stock bool:operator<(oper1, Float:oper2)
   1.363 +{
   1.364 +	return FloatCompare(float(oper1), oper2) < 0;
   1.365 +}
   1.366 +
   1.367 +stock bool:operator<=(Float:oper1, Float:oper2)
   1.368 +{
   1.369 +	return FloatCompare(oper1, oper2) <= 0;
   1.370 +}
   1.371 +
   1.372 +stock bool:operator<=(Float:oper1, oper2)
   1.373 +{
   1.374 +	return FloatCompare(oper1, float(oper2)) <= 0;
   1.375 +}
   1.376 +
   1.377 +stock bool:operator<=(oper1, Float:oper2)
   1.378 +{
   1.379 +	return FloatCompare(float(oper1), oper2) <= 0;
   1.380 +}
   1.381 +
   1.382 +stock bool:operator!(Float:oper)
   1.383 +{
   1.384 +	return (_:oper & ((-1)/2)) == 0;		/* -1 = all bits to 1; /2 = remove most significant bit (sign)
   1.385 +											works on both 32bit and 64bit systems; no constant required */
   1.386 +}
   1.387 +
   1.388 +/**
   1.389 + * Forbidden operators.
   1.390 + *
   1.391 + */
   1.392 +forward operator%(Float:oper1, Float:oper2);
   1.393 +forward operator%(Float:oper1, oper2);
   1.394 +forward operator%(oper1, Float:oper2);
   1.395 +
   1.396 +#define FLOAT_PI 3.1415926535897932384626433832795
   1.397 +
   1.398 +/**
   1.399 + * Converts degrees to radians.
   1.400 + *
   1.401 + * @param angle		Degrees.
   1.402 + * @return			Radians.
   1.403 + */
   1.404 +stock Float:DegToRad(Float:angle)
   1.405 +{
   1.406 +	return (angle*FLOAT_PI)/180;
   1.407 +}
   1.408 +
   1.409 +/**
   1.410 + * Converts degrees to radians.
   1.411 + *
   1.412 + * @param angle		Radians.
   1.413 + * @return			Degrees.
   1.414 + */
   1.415 +stock Float:RadToDeg(Float:angle)
   1.416 +{
   1.417 +	return (angle*180)/FLOAT_PI;
   1.418 +}