2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is part of the SourceMod/SourcePawn SDK. This file may only be used
7 * or modified under the Terms and Conditions of its License Agreement, which is found
8 * in LICENSE.txt. The Terms and Conditions for making SourceMod extensions/plugins
9 * may change at any time. To view the latest information, see:
10 * http://www.sourcemod.net/license.php
15 #if defined _float_included
18 #define _float_included
22 * All the trigonometric functions take in or return its values in RADIANS.
23 * Use DegToRad or RadToDeg stocks to convert the radix units.
26 /* Different methods of rounding */
27 enum floatround_method
36 * Converts an integer into a floating point value.
38 * @param value Integer to convert.
39 * @return Floating point value.
41 native Float:float(value);
44 * Converts a string into a floating point value.
46 * @param str String to convert.
47 * @return Converted floating point number.
49 native Float:FloatStr(const String:str[]);
52 * Multiplies two floats together.
54 * @param oper1 First value.
55 * @param oper2 Second value.
56 * @return oper1*oper2.
58 native Float:FloatMul(Float:oper1, Float:oper2);
61 * Divides the dividend by the divisor.
63 * @param dividend First value.
64 * @param divisor Second value.
65 * @return dividend/divisor.
67 native Float:FloatDiv(Float:dividend, Float:divisor);
70 * Adds two floats together.
72 * @param oper1 First value.
73 * @param oper2 Second value.
74 * @return oper1+oper2.
76 native Float:FloatAdd(Float:oper1, Float:oper2);
79 * Subtracts oper2 from oper1.
81 * @param oper1 First value.
82 * @param oper2 Second value.
83 * @return oper1-oper2.
85 native Float:FloatSub(Float:oper1, Float:oper2);
88 * Returns the decimal part of a float.
90 * @param value Input value.
91 * @return Decimal part.
93 native Float:FloatFraction(Float:value);
96 * Rounds a float into an integer number.
98 * @param value Input value to be rounded.
99 * @param method Rounding method to use.
100 * @return Rounded value.
102 native FloatRound(Float:value, floatround_method:method=floatround_round);
105 * Compares two floats.
107 * @param fOne First value.
108 * @param fTwo Second value.
109 * @return Returns 1 if the first argument is greater than the second argument.
110 * Returns -1 if the first argument is smaller than the second argument.
111 * Returns 0 if both arguments are equal.
113 native FloatCompare(Float:fOne, Float:fTwo);
116 * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
118 * @param value Input value.
119 * @return Square root of the value.
121 native Float:SquareRoot(Float:value);
124 * Returns the value raised to the power of the exponent.
126 * @param value Value to be raised.
127 * @param exponent Value to raise the base.
128 * @return value^exponent.
130 native Float:Pow(Float:value, Float:exponent);
133 * Returns the value of raising the input by e.
135 * @param value Input value.
136 * @return exp(value).
138 native Float:Exponential(Float:value);
141 * Returns the logarithm of any base specified.
143 * @param value Input value.
144 * @param base Logarithm base to use, default is 10.
145 * @return log(value)/log(base).
147 native Float:Logarithm(Float:value, Float:base=10.0);
150 * Returns the sine of the argument.
152 * @param value Input value in radians.
153 * @return sin(value).
155 native Float:Sine(Float:value);
158 * Returns the cosine of the argument.
160 * @param value Input value in radians.
161 * @return cos(value).
163 native Float:Cosine(Float:value);
166 * Returns the tangent of the argument.
168 * @param value Input value in radians.
169 * @return tan(value).
171 native Float:Tangent(Float:value);
174 * Returns the absolute value.
176 * @param value Input value.
177 * @return abs(value).
179 native Float:FloatAbs(Float:value);
182 * Returns the arctangent of the input value.
184 * @param angle Input value.
185 * @return atan(value) in radians.
187 native Float:ArcTangent(Float:angle);
190 * Returns the arccosine of the input value.
192 * @param angle Input value.
193 * @return acos(value) in radians.
195 native Float:ArcCosine(Float:angle);
198 * Returns the arcsine of the input value.
200 * @param angle Input value.
201 * @return asin(value) in radians.
203 native Float:ArcSine(Float:angle);
206 * Returns the arctangent2 of the input values.
208 * @param x Horizontal value.
209 * @param y Vertical value.
210 * @return atan2(value) in radians.
212 native Float:ArcTangent2(Float:x, Float:y);
215 * User defined operators.
218 #pragma rational Float
220 native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
221 native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
222 native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
223 native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
225 stock Float:operator++(Float:oper)
228 stock Float:operator--(Float:oper)
231 stock Float:operator-(Float:oper)
232 return oper^Float:((-1)^((-1)/2)); /* IEEE values are sign/magnitude */
234 stock Float:operator*(Float:oper1, oper2)
235 return FloatMul(oper1, float(oper2)); /* "*" is commutative */
237 stock Float:operator/(Float:oper1, oper2)
238 return FloatDiv(oper1, float(oper2));
240 stock Float:operator/(oper1, Float:oper2)
241 return FloatDiv(float(oper1), oper2);
243 stock Float:operator+(Float:oper1, oper2)
244 return FloatAdd(oper1, float(oper2)); /* "+" is commutative */
246 stock Float:operator-(Float:oper1, oper2)
247 return FloatSub(oper1, float(oper2));
249 stock Float:operator-(oper1, Float:oper2)
250 return FloatSub(float(oper1), oper2);
252 stock bool:operator==(Float:oper1, Float:oper2)
253 return FloatCompare(oper1, oper2) == 0;
255 stock bool:operator==(Float:oper1, oper2)
256 return FloatCompare(oper1, float(oper2)) == 0; /* "==" is commutative */
258 stock bool:operator!=(Float:oper1, Float:oper2)
259 return FloatCompare(oper1, oper2) != 0;
261 stock bool:operator!=(Float:oper1, oper2)
262 return FloatCompare(oper1, float(oper2)) != 0; /* "==" is commutative */
264 stock bool:operator>(Float:oper1, Float:oper2)
265 return FloatCompare(oper1, oper2) > 0;
267 stock bool:operator>(Float:oper1, oper2)
268 return FloatCompare(oper1, float(oper2)) > 0;
270 stock bool:operator>(oper1, Float:oper2)
271 return FloatCompare(float(oper1), oper2) > 0;
273 stock bool:operator>=(Float:oper1, Float:oper2)
274 return FloatCompare(oper1, oper2) >= 0;
276 stock bool:operator>=(Float:oper1, oper2)
277 return FloatCompare(oper1, float(oper2)) >= 0;
279 stock bool:operator>=(oper1, Float:oper2)
280 return FloatCompare(float(oper1), oper2) >= 0;
282 stock bool:operator<(Float:oper1, Float:oper2)
283 return FloatCompare(oper1, oper2) < 0;
285 stock bool:operator<(Float:oper1, oper2)
286 return FloatCompare(oper1, float(oper2)) < 0;
288 stock bool:operator<(oper1, Float:oper2)
289 return FloatCompare(float(oper1), oper2) < 0;
291 stock bool:operator<=(Float:oper1, Float:oper2)
292 return FloatCompare(oper1, oper2) <= 0;
294 stock bool:operator<=(Float:oper1, oper2)
295 return FloatCompare(oper1, float(oper2)) <= 0;
297 stock bool:operator<=(oper1, Float:oper2)
298 return FloatCompare(float(oper1), oper2) <= 0;
300 stock bool:operator!(Float:oper)
301 return (_:oper & ((-1)/2)) == 0; /* -1 = all bits to 1; /2 = remove most significant bit (sign)
302 works on both 32bit and 64bit systems; no constant required */
305 * Forbidden operators.
308 forward operator%(Float:oper1, Float:oper2);
309 forward operator%(Float:oper1, oper2);
310 forward operator%(oper1, Float:oper2);
312 #define FLOAT_PI 3.1415926535897932384626433832795
314 stock Float:DegToRad(Float:angle)
316 return (angle*FLOAT_PI)/180;
319 stock Float:RadToDeg(Float:angle)
321 return (angle*180)/FLOAT_PI;