3 * =============================================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * =============================================================================
7 * This file is part of the SourceMod/SourcePawn SDK.
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.
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
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/>.
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>.
33 #if defined _float_included
36 #define _float_included
39 * Converts an integer into a floating point value.
41 * @param value Integer to convert.
42 * @return Floating point value.
44 native Float:float(value);
47 * Multiplies two floats together.
49 * @param oper1 First value.
50 * @param oper2 Second value.
51 * @return oper1*oper2.
53 native Float:FloatMul(Float:oper1, Float:oper2);
56 * Divides the dividend by the divisor.
58 * @param dividend First value.
59 * @param divisor Second value.
60 * @return dividend/divisor.
62 native Float:FloatDiv(Float:dividend, Float:divisor);
65 * Adds two floats together.
67 * @param oper1 First value.
68 * @param oper2 Second value.
69 * @return oper1+oper2.
71 native Float:FloatAdd(Float:oper1, Float:oper2);
74 * Subtracts oper2 from oper1.
76 * @param oper1 First value.
77 * @param oper2 Second value.
78 * @return oper1-oper2.
80 native Float:FloatSub(Float:oper1, Float:oper2);
83 * Returns the decimal part of a float.
85 * @param value Input value.
86 * @return Decimal part.
88 native Float:FloatFraction(Float:value);
91 * Rounds a float to the closest integer to zero.
93 * @param value Input value to be rounded.
94 * @return Rounded value.
96 native RoundToZero(Float:value);
99 * Rounds a float to the next highest integer value.
101 * @param value Input value to be rounded.
102 * @return Rounded value.
104 native RoundToCeil(Float:value);
107 * Rounds a float to the next lowest integer value.
109 * @param value Input value to be rounded.
110 * @return Rounded value.
112 native RoundToFloor(Float:value);
115 * Standard IEEE rounding.
117 * @param value Input value to be rounded.
118 * @return Rounded value.
120 native RoundToNearest(Float:value);
123 * Compares two floats.
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.
131 native FloatCompare(Float:fOne, Float:fTwo);
134 * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
136 * @param value Input value.
137 * @return Square root of the value.
139 native Float:SquareRoot(Float:value);
142 * Returns the value raised to the power of the exponent.
144 * @param value Value to be raised.
145 * @param exponent Value to raise the base.
146 * @return value^exponent.
148 native Float:Pow(Float:value, Float:exponent);
151 * Returns the value of raising the input by e.
153 * @param value Input value.
154 * @return exp(value).
156 native Float:Exponential(Float:value);
159 * Returns the logarithm of any base specified.
161 * @param value Input value.
162 * @param base Logarithm base to use, default is 10.
163 * @return log(value)/log(base).
165 native Float:Logarithm(Float:value, Float:base=10.0);
168 * Returns the sine of the argument.
170 * @param value Input value in radians.
171 * @return sin(value).
173 native Float:Sine(Float:value);
176 * Returns the cosine of the argument.
178 * @param value Input value in radians.
179 * @return cos(value).
181 native Float:Cosine(Float:value);
184 * Returns the tangent of the argument.
186 * @param value Input value in radians.
187 * @return tan(value).
189 native Float:Tangent(Float:value);
192 * Returns an absolute value.
194 * @param value Input value.
195 * @return Absolute value of the input.
197 native Float:FloatAbs(Float:value);
200 * Returns the arctangent of the input value.
202 * @param angle Input value.
203 * @return atan(value) in radians.
205 native Float:ArcTangent(Float:angle);
208 * Returns the arccosine of the input value.
210 * @param angle Input value.
211 * @return acos(value) in radians.
213 native Float:ArcCosine(Float:angle);
216 * Returns the arcsine of the input value.
218 * @param angle Input value.
219 * @return asin(value) in radians.
221 native Float:ArcSine(Float:angle);
224 * Returns the arctangent2 of the input values.
226 * @param x Horizontal value.
227 * @param y Vertical value.
228 * @return atan2(value) in radians.
230 native Float:ArcTangent2(Float:x, Float:y);
233 * Rounds a floating point number using the "round to nearest" algorithm.
235 * @param value Floating point value to round.
236 * @return The value rounded to the nearest integer.
238 stock RoundFloat(Float:value)
240 return RoundToNearest(value);
244 * Backwards compatibility stock. Do not use.
248 #pragma deprecated Use RoundToNearest() or RoundFloat() instead
249 stock FloatRound(Float:value)
251 return RoundToNearest(value);
255 * User defined operators.
258 #pragma rational Float
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;
265 stock Float:operator++(Float:oper)
270 stock Float:operator--(Float:oper)
275 stock Float:operator-(Float:oper)
277 return oper^Float:((-1)^((-1)/2)); /* IEEE values are sign/magnitude */
280 stock Float:operator*(Float:oper1, oper2)
282 return FloatMul(oper1, float(oper2)); /* "*" is commutative */
285 stock Float:operator/(Float:oper1, oper2)
287 return FloatDiv(oper1, float(oper2));
290 stock Float:operator/(oper1, Float:oper2)
292 return FloatDiv(float(oper1), oper2);
295 stock Float:operator+(Float:oper1, oper2)
297 return FloatAdd(oper1, float(oper2)); /* "+" is commutative */
300 stock Float:operator-(Float:oper1, oper2)
302 return FloatSub(oper1, float(oper2));
305 stock Float:operator-(oper1, Float:oper2)
307 return FloatSub(float(oper1), oper2);
310 stock bool:operator==(Float:oper1, Float:oper2)
312 return FloatCompare(oper1, oper2) == 0;
315 stock bool:operator==(Float:oper1, oper2)
317 return FloatCompare(oper1, float(oper2)) == 0; /* "==" is commutative */
320 stock bool:operator!=(Float:oper1, Float:oper2)
322 return FloatCompare(oper1, oper2) != 0;
325 stock bool:operator!=(Float:oper1, oper2)
327 return FloatCompare(oper1, float(oper2)) != 0; /* "==" is commutative */
330 stock bool:operator>(Float:oper1, Float:oper2)
332 return FloatCompare(oper1, oper2) > 0;
335 stock bool:operator>(Float:oper1, oper2)
337 return FloatCompare(oper1, float(oper2)) > 0;
340 stock bool:operator>(oper1, Float:oper2)
342 return FloatCompare(float(oper1), oper2) > 0;
345 stock bool:operator>=(Float:oper1, Float:oper2)
347 return FloatCompare(oper1, oper2) >= 0;
350 stock bool:operator>=(Float:oper1, oper2)
352 return FloatCompare(oper1, float(oper2)) >= 0;
355 stock bool:operator>=(oper1, Float:oper2)
357 return FloatCompare(float(oper1), oper2) >= 0;
360 stock bool:operator<(Float:oper1, Float:oper2)
362 return FloatCompare(oper1, oper2) < 0;
365 stock bool:operator<(Float:oper1, oper2)
367 return FloatCompare(oper1, float(oper2)) < 0;
370 stock bool:operator<(oper1, Float:oper2)
372 return FloatCompare(float(oper1), oper2) < 0;
375 stock bool:operator<=(Float:oper1, Float:oper2)
377 return FloatCompare(oper1, oper2) <= 0;
380 stock bool:operator<=(Float:oper1, oper2)
382 return FloatCompare(oper1, float(oper2)) <= 0;
385 stock bool:operator<=(oper1, Float:oper2)
387 return FloatCompare(float(oper1), oper2) <= 0;
390 stock bool:operator!(Float:oper)
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 */
397 * Forbidden operators.
400 forward operator%(Float:oper1, Float:oper2);
401 forward operator%(Float:oper1, oper2);
402 forward operator%(oper1, Float:oper2);
404 #define FLOAT_PI 3.1415926535897932384626433832795
407 * Converts degrees to radians.
409 * @param angle Degrees.
412 stock Float:DegToRad(Float:angle)
414 return (angle*FLOAT_PI)/180;
418 * Converts degrees to radians.
420 * @param angle Radians.
423 stock Float:RadToDeg(Float:angle)
425 return (angle*180)/FLOAT_PI;