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 * Different methods of rounding.
24 enum floatround_method
26 floatround_round = 0, /**< Standard IEEE rounding */
27 floatround_floor, /**< Next lowest integer value. */
28 floatround_ceil, /**< Next highest integer value. */
29 floatround_tozero /** Closest integer to zero. */
33 * Converts an integer into a floating point value.
35 * @param value Integer to convert.
36 * @return Floating point value.
38 native Float:float(value);
41 * Multiplies two floats together.
43 * @param oper1 First value.
44 * @param oper2 Second value.
45 * @return oper1*oper2.
47 native Float:FloatMul(Float:oper1, Float:oper2);
50 * Divides the dividend by the divisor.
52 * @param dividend First value.
53 * @param divisor Second value.
54 * @return dividend/divisor.
56 native Float:FloatDiv(Float:dividend, Float:divisor);
59 * Adds two floats together.
61 * @param oper1 First value.
62 * @param oper2 Second value.
63 * @return oper1+oper2.
65 native Float:FloatAdd(Float:oper1, Float:oper2);
68 * Subtracts oper2 from oper1.
70 * @param oper1 First value.
71 * @param oper2 Second value.
72 * @return oper1-oper2.
74 native Float:FloatSub(Float:oper1, Float:oper2);
77 * Returns the decimal part of a float.
79 * @param value Input value.
80 * @return Decimal part.
82 native Float:FloatFraction(Float:value);
85 * Rounds a float into an integer number.
87 * @param value Input value to be rounded.
88 * @param method Rounding method to use.
89 * @return Rounded value.
91 native FloatRound(Float:value, floatround_method:method=floatround_round);
94 * Compares two floats.
96 * @param fOne First value.
97 * @param fTwo Second value.
98 * @return Returns 1 if the first argument is greater than the second argument.
99 * Returns -1 if the first argument is smaller than the second argument.
100 * Returns 0 if both arguments are equal.
102 native FloatCompare(Float:fOne, Float:fTwo);
105 * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
107 * @param value Input value.
108 * @return Square root of the value.
110 native Float:SquareRoot(Float:value);
113 * Returns the value raised to the power of the exponent.
115 * @param value Value to be raised.
116 * @param exponent Value to raise the base.
117 * @return value^exponent.
119 native Float:Pow(Float:value, Float:exponent);
122 * Returns the value of raising the input by e.
124 * @param value Input value.
125 * @return exp(value).
127 native Float:Exponential(Float:value);
130 * Returns the logarithm of any base specified.
132 * @param value Input value.
133 * @param base Logarithm base to use, default is 10.
134 * @return log(value)/log(base).
136 native Float:Logarithm(Float:value, Float:base=10.0);
139 * Returns the sine of the argument.
141 * @param value Input value in radians.
142 * @return sin(value).
144 native Float:Sine(Float:value);
147 * Returns the cosine of the argument.
149 * @param value Input value in radians.
150 * @return cos(value).
152 native Float:Cosine(Float:value);
155 * Returns the tangent of the argument.
157 * @param value Input value in radians.
158 * @return tan(value).
160 native Float:Tangent(Float:value);
163 * Returns an absolute value.
165 * @param value Input value.
166 * @return Absolute value of the input.
168 native Float:FloatAbs(Float:value);
171 * Returns the arctangent of the input value.
173 * @param angle Input value.
174 * @return atan(value) in radians.
176 native Float:ArcTangent(Float:angle);
179 * Returns the arccosine of the input value.
181 * @param angle Input value.
182 * @return acos(value) in radians.
184 native Float:ArcCosine(Float:angle);
187 * Returns the arcsine of the input value.
189 * @param angle Input value.
190 * @return asin(value) in radians.
192 native Float:ArcSine(Float:angle);
195 * Returns the arctangent2 of the input values.
197 * @param x Horizontal value.
198 * @param y Vertical value.
199 * @return atan2(value) in radians.
201 native Float:ArcTangent2(Float:x, Float:y);
204 * User defined operators.
207 #pragma rational Float
209 native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
210 native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
211 native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
212 native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
214 stock Float:operator++(Float:oper)
217 stock Float:operator--(Float:oper)
220 stock Float:operator-(Float:oper)
221 return oper^Float:((-1)^((-1)/2)); /* IEEE values are sign/magnitude */
223 stock Float:operator*(Float:oper1, oper2)
224 return FloatMul(oper1, float(oper2)); /* "*" is commutative */
226 stock Float:operator/(Float:oper1, oper2)
227 return FloatDiv(oper1, float(oper2));
229 stock Float:operator/(oper1, Float:oper2)
230 return FloatDiv(float(oper1), oper2);
232 stock Float:operator+(Float:oper1, oper2)
233 return FloatAdd(oper1, float(oper2)); /* "+" is commutative */
235 stock Float:operator-(Float:oper1, oper2)
236 return FloatSub(oper1, float(oper2));
238 stock Float:operator-(oper1, Float:oper2)
239 return FloatSub(float(oper1), oper2);
241 stock bool:operator==(Float:oper1, Float:oper2)
242 return FloatCompare(oper1, oper2) == 0;
244 stock bool:operator==(Float:oper1, oper2)
245 return FloatCompare(oper1, float(oper2)) == 0; /* "==" is commutative */
247 stock bool:operator!=(Float:oper1, Float:oper2)
248 return FloatCompare(oper1, oper2) != 0;
250 stock bool:operator!=(Float:oper1, oper2)
251 return FloatCompare(oper1, float(oper2)) != 0; /* "==" is commutative */
253 stock bool:operator>(Float:oper1, Float:oper2)
254 return FloatCompare(oper1, oper2) > 0;
256 stock bool:operator>(Float:oper1, oper2)
257 return FloatCompare(oper1, float(oper2)) > 0;
259 stock bool:operator>(oper1, Float:oper2)
260 return FloatCompare(float(oper1), oper2) > 0;
262 stock bool:operator>=(Float:oper1, Float:oper2)
263 return FloatCompare(oper1, oper2) >= 0;
265 stock bool:operator>=(Float:oper1, oper2)
266 return FloatCompare(oper1, float(oper2)) >= 0;
268 stock bool:operator>=(oper1, Float:oper2)
269 return FloatCompare(float(oper1), oper2) >= 0;
271 stock bool:operator<(Float:oper1, Float:oper2)
272 return FloatCompare(oper1, oper2) < 0;
274 stock bool:operator<(Float:oper1, oper2)
275 return FloatCompare(oper1, float(oper2)) < 0;
277 stock bool:operator<(oper1, Float:oper2)
278 return FloatCompare(float(oper1), oper2) < 0;
280 stock bool:operator<=(Float:oper1, Float:oper2)
281 return FloatCompare(oper1, oper2) <= 0;
283 stock bool:operator<=(Float:oper1, oper2)
284 return FloatCompare(oper1, float(oper2)) <= 0;
286 stock bool:operator<=(oper1, Float:oper2)
287 return FloatCompare(float(oper1), oper2) <= 0;
289 stock bool:operator!(Float:oper)
290 return (_:oper & ((-1)/2)) == 0; /* -1 = all bits to 1; /2 = remove most significant bit (sign)
291 works on both 32bit and 64bit systems; no constant required */
294 * Forbidden operators.
297 forward operator%(Float:oper1, Float:oper2);
298 forward operator%(Float:oper1, oper2);
299 forward operator%(oper1, Float:oper2);
301 #define FLOAT_PI 3.1415926535897932384626433832795
304 * Converts degrees to radians.
306 * @param angle Degrees.
309 stock Float:DegToRad(Float:angle)
311 return (angle*FLOAT_PI)/180;
315 * Converts degrees to radians.
317 * @param angle Radians.
320 stock Float:RadToDeg(Float:angle)
322 return (angle*180)/FLOAT_PI;