3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
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
16 #if defined _float_included
19 #define _float_included
22 * Converts an integer into a floating point value.
24 * @param value Integer to convert.
25 * @return Floating point value.
27 native Float:float(value);
30 * Multiplies two floats together.
32 * @param oper1 First value.
33 * @param oper2 Second value.
34 * @return oper1*oper2.
36 native Float:FloatMul(Float:oper1, Float:oper2);
39 * Divides the dividend by the divisor.
41 * @param dividend First value.
42 * @param divisor Second value.
43 * @return dividend/divisor.
45 native Float:FloatDiv(Float:dividend, Float:divisor);
48 * Adds two floats together.
50 * @param oper1 First value.
51 * @param oper2 Second value.
52 * @return oper1+oper2.
54 native Float:FloatAdd(Float:oper1, Float:oper2);
57 * Subtracts oper2 from oper1.
59 * @param oper1 First value.
60 * @param oper2 Second value.
61 * @return oper1-oper2.
63 native Float:FloatSub(Float:oper1, Float:oper2);
66 * Returns the decimal part of a float.
68 * @param value Input value.
69 * @return Decimal part.
71 native Float:FloatFraction(Float:value);
74 * Rounds a float to the closest integer to zero.
76 * @param value Input value to be rounded.
77 * @return Rounded value.
79 native RoundToZero(Float:value);
82 * Rounds a float to the next highest integer value.
84 * @param value Input value to be rounded.
85 * @return Rounded value.
87 native RoundToCeil(Float:value);
90 * Rounds a float to the next lowest integer value.
92 * @param value Input value to be rounded.
93 * @return Rounded value.
95 native RoundToFloor(Float:value);
98 * Standard IEEE rounding.
100 * @param value Input value to be rounded.
101 * @return Rounded value.
103 native RoundToNearest(Float:value);
106 * Compares two floats.
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.
114 native FloatCompare(Float:fOne, Float:fTwo);
117 * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
119 * @param value Input value.
120 * @return Square root of the value.
122 native Float:SquareRoot(Float:value);
125 * Returns the value raised to the power of the exponent.
127 * @param value Value to be raised.
128 * @param exponent Value to raise the base.
129 * @return value^exponent.
131 native Float:Pow(Float:value, Float:exponent);
134 * Returns the value of raising the input by e.
136 * @param value Input value.
137 * @return exp(value).
139 native Float:Exponential(Float:value);
142 * Returns the logarithm of any base specified.
144 * @param value Input value.
145 * @param base Logarithm base to use, default is 10.
146 * @return log(value)/log(base).
148 native Float:Logarithm(Float:value, Float:base=10.0);
151 * Returns the sine of the argument.
153 * @param value Input value in radians.
154 * @return sin(value).
156 native Float:Sine(Float:value);
159 * Returns the cosine of the argument.
161 * @param value Input value in radians.
162 * @return cos(value).
164 native Float:Cosine(Float:value);
167 * Returns the tangent of the argument.
169 * @param value Input value in radians.
170 * @return tan(value).
172 native Float:Tangent(Float:value);
175 * Returns an absolute value.
177 * @param value Input value.
178 * @return Absolute value of the input.
180 native Float:FloatAbs(Float:value);
183 * Returns the arctangent of the input value.
185 * @param angle Input value.
186 * @return atan(value) in radians.
188 native Float:ArcTangent(Float:angle);
191 * Returns the arccosine of the input value.
193 * @param angle Input value.
194 * @return acos(value) in radians.
196 native Float:ArcCosine(Float:angle);
199 * Returns the arcsine of the input value.
201 * @param angle Input value.
202 * @return asin(value) in radians.
204 native Float:ArcSine(Float:angle);
207 * Returns the arctangent2 of the input values.
209 * @param x Horizontal value.
210 * @param y Vertical value.
211 * @return atan2(value) in radians.
213 native Float:ArcTangent2(Float:x, Float:y);
216 * Rounds a floating point number using the "round to nearest" algorithm.
218 * @param value Floating point value to round.
219 * @return The value rounded to the nearest integer.
221 stock RoundFloat(Float:value)
223 return RoundToNearest(value);
227 * Backwards compatibility stock. Do not use.
231 #pragma deprecated Use RoundToNearest() or RoundFloat() instead
232 stock FloatRound(Float:value)
234 return RoundToNearest(value);
238 * User defined operators.
241 #pragma rational Float
243 native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
244 native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
245 native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
246 native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
248 stock Float:operator++(Float:oper)
253 stock Float:operator--(Float:oper)
258 stock Float:operator-(Float:oper)
260 return oper^Float:((-1)^((-1)/2)); /* IEEE values are sign/magnitude */
263 stock Float:operator*(Float:oper1, oper2)
265 return FloatMul(oper1, float(oper2)); /* "*" is commutative */
268 stock Float:operator/(Float:oper1, oper2)
270 return FloatDiv(oper1, float(oper2));
273 stock Float:operator/(oper1, Float:oper2)
275 return FloatDiv(float(oper1), oper2);
278 stock Float:operator+(Float:oper1, oper2)
280 return FloatAdd(oper1, float(oper2)); /* "+" is commutative */
283 stock Float:operator-(Float:oper1, oper2)
285 return FloatSub(oper1, float(oper2));
288 stock Float:operator-(oper1, Float:oper2)
290 return FloatSub(float(oper1), oper2);
293 stock bool:operator==(Float:oper1, Float:oper2)
295 return FloatCompare(oper1, oper2) == 0;
298 stock bool:operator==(Float:oper1, oper2)
300 return FloatCompare(oper1, float(oper2)) == 0; /* "==" is commutative */
303 stock bool:operator!=(Float:oper1, Float:oper2)
305 return FloatCompare(oper1, oper2) != 0;
308 stock bool:operator!=(Float:oper1, oper2)
310 return FloatCompare(oper1, float(oper2)) != 0; /* "==" is commutative */
313 stock bool:operator>(Float:oper1, Float:oper2)
315 return FloatCompare(oper1, oper2) > 0;
318 stock bool:operator>(Float:oper1, oper2)
320 return FloatCompare(oper1, float(oper2)) > 0;
323 stock bool:operator>(oper1, Float:oper2)
325 return FloatCompare(float(oper1), oper2) > 0;
328 stock bool:operator>=(Float:oper1, Float:oper2)
330 return FloatCompare(oper1, oper2) >= 0;
333 stock bool:operator>=(Float:oper1, oper2)
335 return FloatCompare(oper1, float(oper2)) >= 0;
338 stock bool:operator>=(oper1, Float:oper2)
340 return FloatCompare(float(oper1), oper2) >= 0;
343 stock bool:operator<(Float:oper1, Float:oper2)
345 return FloatCompare(oper1, oper2) < 0;
348 stock bool:operator<(Float:oper1, oper2)
350 return FloatCompare(oper1, float(oper2)) < 0;
353 stock bool:operator<(oper1, Float:oper2)
355 return FloatCompare(float(oper1), oper2) < 0;
358 stock bool:operator<=(Float:oper1, Float:oper2)
360 return FloatCompare(oper1, oper2) <= 0;
363 stock bool:operator<=(Float:oper1, oper2)
365 return FloatCompare(oper1, float(oper2)) <= 0;
368 stock bool:operator<=(oper1, Float:oper2)
370 return FloatCompare(float(oper1), oper2) <= 0;
373 stock bool:operator!(Float:oper)
375 return (_:oper & ((-1)/2)) == 0; /* -1 = all bits to 1; /2 = remove most significant bit (sign)
376 works on both 32bit and 64bit systems; no constant required */
380 * Forbidden operators.
383 forward operator%(Float:oper1, Float:oper2);
384 forward operator%(Float:oper1, oper2);
385 forward operator%(oper1, Float:oper2);
387 #define FLOAT_PI 3.1415926535897932384626433832795
390 * Converts degrees to radians.
392 * @param angle Degrees.
395 stock Float:DegToRad(Float:angle)
397 return (angle*FLOAT_PI)/180;
401 * Converts degrees to radians.
403 * @param angle Radians.
406 stock Float:RadToDeg(Float:angle)
408 return (angle*180)/FLOAT_PI;