faluco@242: /**
ds@475: * vim: set ts=4 :
ds@1330: * =============================================================================
dvander@374: * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
ds@1330: * =============================================================================
ds@1330: *
ds@1330: * This file is part of the SourceMod/SourcePawn SDK.
dvander@374: *
ds@1330: * This program is free software; you can redistribute it and/or modify it under
ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330: * Free Software Foundation.
ds@1330: *
ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1330: * details.
ds@1330: *
ds@1330: * You should have received a copy of the GNU General Public License along with
ds@1330: * this program. If not, see .
ds@1330: *
ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1330: * this exception to all derivative works. AlliedModders LLC defines further
ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330: * or .
dvander@374: *
dvander@374: * Version: $Id$
faluco@242: */
dvander@374:
faluco@242: #if defined _float_included
faluco@242: #endinput
faluco@242: #endif
faluco@242: #define _float_included
faluco@242:
faluco@242: /**
faluco@242: * Converts an integer into a floating point value.
faluco@242: *
faluco@242: * @param value Integer to convert.
faluco@242: * @return Floating point value.
faluco@242: */
faluco@242: native Float:float(value);
faluco@242:
faluco@242: /**
faluco@242: * Multiplies two floats together.
faluco@242: *
faluco@242: * @param oper1 First value.
faluco@242: * @param oper2 Second value.
faluco@242: * @return oper1*oper2.
faluco@242: */
faluco@242: native Float:FloatMul(Float:oper1, Float:oper2);
faluco@242:
faluco@242: /**
faluco@242: * Divides the dividend by the divisor.
faluco@242: *
faluco@242: * @param dividend First value.
faluco@242: * @param divisor Second value.
faluco@242: * @return dividend/divisor.
faluco@242: */
faluco@242: native Float:FloatDiv(Float:dividend, Float:divisor);
faluco@242:
faluco@242: /**
faluco@242: * Adds two floats together.
faluco@242: *
faluco@242: * @param oper1 First value.
faluco@242: * @param oper2 Second value.
faluco@242: * @return oper1+oper2.
faluco@242: */
faluco@242: native Float:FloatAdd(Float:oper1, Float:oper2);
faluco@242:
faluco@242: /**
faluco@242: * Subtracts oper2 from oper1.
faluco@242: *
faluco@242: * @param oper1 First value.
faluco@242: * @param oper2 Second value.
faluco@242: * @return oper1-oper2.
faluco@242: */
faluco@242: native Float:FloatSub(Float:oper1, Float:oper2);
faluco@242:
faluco@242: /**
faluco@242: * Returns the decimal part of a float.
faluco@242: *
faluco@242: * @param value Input value.
faluco@242: * @return Decimal part.
faluco@242: */
faluco@242: native Float:FloatFraction(Float:value);
faluco@242:
faluco@242: /**
faluco@831: * Rounds a float to the closest integer to zero.
faluco@831: *
faluco@831: * @param value Input value to be rounded.
faluco@831: * @return Rounded value.
faluco@831: */
faluco@831: native RoundToZero(Float:value);
faluco@831:
faluco@831: /**
faluco@831: * Rounds a float to the next highest integer value.
faluco@242: *
faluco@242: * @param value Input value to be rounded.
faluco@831: * @return Rounded value.
faluco@831: */
faluco@831: native RoundToCeil(Float:value);
faluco@831:
faluco@831: /**
faluco@831: * Rounds a float to the next lowest integer value.
faluco@831: *
faluco@831: * @param value Input value to be rounded.
faluco@242: * @return Rounded value.
faluco@242: */
faluco@831: native RoundToFloor(Float:value);
faluco@831:
faluco@831: /**
faluco@831: * Standard IEEE rounding.
faluco@831: *
faluco@831: * @param value Input value to be rounded.
faluco@831: * @return Rounded value.
faluco@831: */
faluco@851: native RoundToNearest(Float:value);
faluco@242:
faluco@242: /**
faluco@242: * Compares two floats.
faluco@242: *
faluco@242: * @param fOne First value.
faluco@242: * @param fTwo Second value.
faluco@242: * @return Returns 1 if the first argument is greater than the second argument.
faluco@242: * Returns -1 if the first argument is smaller than the second argument.
faluco@242: * Returns 0 if both arguments are equal.
faluco@242: */
faluco@242: native FloatCompare(Float:fOne, Float:fTwo);
faluco@242:
faluco@242: /**
faluco@242: * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
faluco@242: *
faluco@242: * @param value Input value.
faluco@242: * @return Square root of the value.
faluco@242: */
faluco@242: native Float:SquareRoot(Float:value);
faluco@242:
faluco@242: /**
faluco@242: * Returns the value raised to the power of the exponent.
faluco@242: *
faluco@242: * @param value Value to be raised.
faluco@242: * @param exponent Value to raise the base.
faluco@242: * @return value^exponent.
faluco@242: */
faluco@242: native Float:Pow(Float:value, Float:exponent);
faluco@242:
faluco@242: /**
faluco@242: * Returns the value of raising the input by e.
faluco@242: *
faluco@242: * @param value Input value.
faluco@242: * @return exp(value).
faluco@242: */
faluco@242: native Float:Exponential(Float:value);
faluco@242:
faluco@242: /**
faluco@242: * Returns the logarithm of any base specified.
faluco@242: *
faluco@242: * @param value Input value.
faluco@242: * @param base Logarithm base to use, default is 10.
faluco@242: * @return log(value)/log(base).
faluco@242: */
faluco@242: native Float:Logarithm(Float:value, Float:base=10.0);
faluco@242:
faluco@242: /**
faluco@242: * Returns the sine of the argument.
faluco@242: *
faluco@242: * @param value Input value in radians.
faluco@242: * @return sin(value).
faluco@242: */
faluco@242: native Float:Sine(Float:value);
faluco@242:
faluco@242: /**
faluco@242: * Returns the cosine of the argument.
faluco@242: *
faluco@242: * @param value Input value in radians.
faluco@242: * @return cos(value).
faluco@242: */
faluco@242: native Float:Cosine(Float:value);
faluco@242:
faluco@242: /**
faluco@242: * Returns the tangent of the argument.
faluco@242: *
faluco@242: * @param value Input value in radians.
faluco@242: * @return tan(value).
faluco@242: */
faluco@242: native Float:Tangent(Float:value);
faluco@242:
faluco@242: /**
dvander@619: * Returns an absolute value.
faluco@242: *
faluco@242: * @param value Input value.
dvander@619: * @return Absolute value of the input.
faluco@242: */
faluco@242: native Float:FloatAbs(Float:value);
faluco@242:
faluco@242: /**
faluco@242: * Returns the arctangent of the input value.
faluco@242: *
faluco@242: * @param angle Input value.
faluco@242: * @return atan(value) in radians.
faluco@242: */
faluco@242: native Float:ArcTangent(Float:angle);
faluco@242:
faluco@242: /**
faluco@242: * Returns the arccosine of the input value.
faluco@242: *
faluco@242: * @param angle Input value.
faluco@242: * @return acos(value) in radians.
faluco@242: */
faluco@242: native Float:ArcCosine(Float:angle);
faluco@242:
faluco@242: /**
faluco@242: * Returns the arcsine of the input value.
faluco@242: *
faluco@242: * @param angle Input value.
faluco@242: * @return asin(value) in radians.
faluco@242: */
faluco@242: native Float:ArcSine(Float:angle);
faluco@242:
faluco@242: /**
faluco@242: * Returns the arctangent2 of the input values.
faluco@242: *
faluco@242: * @param x Horizontal value.
faluco@242: * @param y Vertical value.
faluco@242: * @return atan2(value) in radians.
faluco@242: */
faluco@244: native Float:ArcTangent2(Float:x, Float:y);
faluco@242:
faluco@242: /**
dvander@845: * Rounds a floating point number using the "round to nearest" algorithm.
dvander@845: *
dvander@845: * @param value Floating point value to round.
dvander@845: * @return The value rounded to the nearest integer.
faluco@836: */
dvander@845: stock RoundFloat(Float:value)
faluco@836: {
dvander@845: return RoundToNearest(value);
dvander@845: }
faluco@836:
faluco@836: /**
dvander@845: * Backwards compatibility stock. Do not use.
dvander@845: *
faluco@836: * @deprecated
faluco@836: */
ds@1268: #pragma deprecated Use RoundToNearest() or RoundFloat() instead
dvander@845: stock FloatRound(Float:value)
dvander@845: {
dvander@845: return RoundToNearest(value);
dvander@845: }
faluco@836:
faluco@836: /**
faluco@242: * User defined operators.
faluco@242: *
faluco@242: */
faluco@242: #pragma rational Float
faluco@242:
faluco@242: native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
faluco@242: native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
faluco@242: native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
faluco@242: native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
faluco@242:
faluco@242: stock Float:operator++(Float:oper)
dvander@815: {
faluco@242: return oper+1.0;
dvander@815: }
faluco@242:
faluco@242: stock Float:operator--(Float:oper)
dvander@815: {
faluco@242: return oper-1.0;
dvander@815: }
faluco@242:
faluco@242: stock Float:operator-(Float:oper)
dvander@815: {
faluco@242: return oper^Float:((-1)^((-1)/2)); /* IEEE values are sign/magnitude */
dvander@815: }
faluco@242:
faluco@242: stock Float:operator*(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatMul(oper1, float(oper2)); /* "*" is commutative */
dvander@815: }
faluco@242:
faluco@242: stock Float:operator/(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatDiv(oper1, float(oper2));
dvander@815: }
faluco@242:
faluco@242: stock Float:operator/(oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatDiv(float(oper1), oper2);
dvander@815: }
faluco@242:
faluco@242: stock Float:operator+(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatAdd(oper1, float(oper2)); /* "+" is commutative */
dvander@815: }
faluco@242:
faluco@242: stock Float:operator-(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatSub(oper1, float(oper2));
dvander@815: }
faluco@242:
faluco@242: stock Float:operator-(oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatSub(float(oper1), oper2);
dvander@815: }
faluco@242:
faluco@242: stock bool:operator==(Float:oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, oper2) == 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator==(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, float(oper2)) == 0; /* "==" is commutative */
dvander@815: }
faluco@242:
faluco@242: stock bool:operator!=(Float:oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, oper2) != 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator!=(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, float(oper2)) != 0; /* "==" is commutative */
dvander@815: }
faluco@242:
faluco@242: stock bool:operator>(Float:oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, oper2) > 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator>(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, float(oper2)) > 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator>(oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(float(oper1), oper2) > 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator>=(Float:oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, oper2) >= 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator>=(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, float(oper2)) >= 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator>=(oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(float(oper1), oper2) >= 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator<(Float:oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, oper2) < 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator<(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, float(oper2)) < 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator<(oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(float(oper1), oper2) < 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator<=(Float:oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, oper2) <= 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator<=(Float:oper1, oper2)
dvander@815: {
faluco@242: return FloatCompare(oper1, float(oper2)) <= 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator<=(oper1, Float:oper2)
dvander@815: {
faluco@242: return FloatCompare(float(oper1), oper2) <= 0;
dvander@815: }
faluco@242:
faluco@242: stock bool:operator!(Float:oper)
dvander@815: {
faluco@242: return (_:oper & ((-1)/2)) == 0; /* -1 = all bits to 1; /2 = remove most significant bit (sign)
faluco@242: works on both 32bit and 64bit systems; no constant required */
dvander@815: }
faluco@242:
faluco@242: /**
faluco@242: * Forbidden operators.
faluco@242: *
faluco@242: */
faluco@242: forward operator%(Float:oper1, Float:oper2);
faluco@242: forward operator%(Float:oper1, oper2);
faluco@261: forward operator%(oper1, Float:oper2);
faluco@261:
faluco@261: #define FLOAT_PI 3.1415926535897932384626433832795
faluco@261:
dvander@619: /**
dvander@619: * Converts degrees to radians.
dvander@619: *
dvander@619: * @param angle Degrees.
dvander@619: * @return Radians.
dvander@619: */
faluco@261: stock Float:DegToRad(Float:angle)
faluco@261: {
faluco@261: return (angle*FLOAT_PI)/180;
faluco@261: }
faluco@261:
dvander@619: /**
dvander@619: * Converts degrees to radians.
dvander@619: *
dvander@619: * @param angle Radians.
dvander@619: * @return Degrees.
dvander@619: */
faluco@261: stock Float:RadToDeg(Float:angle)
faluco@261: {
faluco@261: return (angle*180)/FLOAT_PI;
ds@475: }