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