2004-01-31 23:56:22 +03:00
|
|
|
/* Float arithmetic
|
2004-02-08 14:32:35 +03:00
|
|
|
*
|
|
|
|
* (c) Copyright 1999, Artran, Inc.
|
|
|
|
* Written by Greg Garner (gmg@artran.com)
|
|
|
|
* Modified in March 2001 to include user defined
|
|
|
|
* operators for the floating point functions.
|
|
|
|
*
|
|
|
|
* This file is provided as is (no warranties).
|
|
|
|
*/
|
|
|
|
|
2004-06-30 12:34:52 +04:00
|
|
|
#if defined _float_included
|
|
|
|
#endinput
|
|
|
|
#endif
|
|
|
|
#define _float_included
|
|
|
|
|
2004-02-08 14:32:35 +03:00
|
|
|
/* Different methods of rounding */
|
|
|
|
enum floatround_method {
|
|
|
|
floatround_round = 0,
|
|
|
|
floatround_floor,
|
|
|
|
floatround_ceil,
|
|
|
|
floatround_tozero
|
|
|
|
}
|
2004-01-31 23:56:22 +03:00
|
|
|
|
2004-02-08 14:32:35 +03:00
|
|
|
enum anglemode {
|
|
|
|
radian = 0,
|
|
|
|
degrees,
|
|
|
|
grades
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert an integer into a floating point value */
|
2004-01-31 23:56:22 +03:00
|
|
|
native Float:float(value);
|
2004-02-08 14:32:35 +03:00
|
|
|
|
|
|
|
/* Convert a string into a floating point value */
|
2004-01-31 23:56:22 +03:00
|
|
|
native Float:floatstr(const string[]);
|
2004-02-08 14:32:35 +03:00
|
|
|
|
|
|
|
/* Multiple two floats together */
|
2004-01-31 23:56:22 +03:00
|
|
|
native Float:floatmul(Float:oper1, Float:oper2);
|
2004-02-08 14:32:35 +03:00
|
|
|
|
|
|
|
/* Divide the dividend float by the divisor float */
|
2004-01-31 23:56:22 +03:00
|
|
|
native Float:floatdiv(Float:dividend, Float:divisor);
|
2004-02-08 14:32:35 +03:00
|
|
|
|
|
|
|
/* Add two floats together */
|
2004-01-31 23:56:22 +03:00
|
|
|
native Float:floatadd(Float:dividend, Float:divisor);
|
2004-02-08 14:32:35 +03:00
|
|
|
|
|
|
|
/* Subtract oper2 float from oper1 float */
|
2004-01-31 23:56:22 +03:00
|
|
|
native Float:floatsub(Float:oper1, Float:oper2);
|
|
|
|
|
2004-02-08 14:32:35 +03:00
|
|
|
/* Return the fractional part of a float */
|
|
|
|
native Float:floatfract(Float:value);
|
2004-01-31 23:56:22 +03:00
|
|
|
|
2004-02-08 14:32:35 +03:00
|
|
|
/* Round a float into a integer value */
|
2004-01-31 23:56:22 +03:00
|
|
|
native floatround(Float:value, floatround_method:method=floatround_round);
|
2004-02-08 14:32:35 +03:00
|
|
|
|
|
|
|
/* Compare two integers. If the two elements are equal, return 0.
|
2004-02-21 23:30:04 +03:00
|
|
|
* If the first argument is greater than the second argument, return 1,
|
|
|
|
* If the first argument is less than the second argument, return -1. */
|
2004-01-31 23:56:22 +03:00
|
|
|
native floatcmp(Float:fOne, Float:fTwo);
|
|
|
|
|
2004-02-08 14:32:35 +03:00
|
|
|
/* Return the square root of the input value, same as floatpower(value, 0.5) */
|
|
|
|
native Float:floatsqroot(Float:value);
|
|
|
|
|
|
|
|
/* Return the value raised to the power of the exponent */
|
|
|
|
native Float:floatpower(Float:value, Float:exponent);
|
|
|
|
|
|
|
|
/* Return the logarithm */
|
|
|
|
native Float:floatlog(Float:value, Float:base=10.0);
|
|
|
|
|
|
|
|
/* Return the sine, cosine or tangent.
|
2006-09-01 07:29:27 +04:00
|
|
|
* The input angle may be in radians, degrees or grades. */
|
2004-02-08 14:32:35 +03:00
|
|
|
native Float:floatsin(Float:value, anglemode:mode=radian);
|
|
|
|
native Float:floatcos(Float:value, anglemode:mode=radian);
|
|
|
|
native Float:floattan(Float:value, anglemode:mode=radian);
|
|
|
|
|
2006-09-01 07:29:27 +04:00
|
|
|
/* Return the hyperbolic sine, cosine or tangent.
|
|
|
|
* The input angle may be in radians, degrees or grades. */
|
|
|
|
native Float:floatsinh(Float:angle, anglemode:mode=radian);
|
|
|
|
native Float:floatcosh(Float:angle, anglemode:mode=radian);
|
|
|
|
native Float:floattanh(Float:angle, anglemode:mode=radian);
|
|
|
|
|
2004-02-08 14:32:35 +03:00
|
|
|
/* Return the absolute value */
|
|
|
|
native Float:floatabs(Float:value);
|
|
|
|
|
2006-09-01 07:29:27 +04:00
|
|
|
/* Return the angle of a sine, cosine or tangent.
|
|
|
|
* The output angle may be in radians, degrees, or grades. */
|
2007-02-24 23:07:21 +03:00
|
|
|
native Float:floatatan(Float:angle, {anglemode,_}:radix);
|
|
|
|
native Float:floatacos(Float:angle, {anglemode,_}:radix);
|
|
|
|
native Float:floatasin(Float:angle, {anglemode,_}:radix);
|
|
|
|
native Float:floatatan2(Float:x, Float:y, {anglemode,_}:radix);
|
2004-06-25 02:11:32 +04:00
|
|
|
|
2004-01-31 23:56:22 +03:00
|
|
|
#pragma rational Float
|
|
|
|
|
|
|
|
/* user defined operators */
|
|
|
|
native Float:operator*(Float:oper1, Float:oper2) = floatmul;
|
|
|
|
native Float:operator/(Float:oper1, Float:oper2) = floatdiv;
|
|
|
|
native Float:operator+(Float:oper1, Float:oper2) = floatadd;
|
|
|
|
native Float:operator-(Float:oper1, Float:oper2) = floatsub;
|
|
|
|
|
|
|
|
stock Float:operator++(Float:oper)
|
2004-02-08 14:32:35 +03:00
|
|
|
return oper+1.0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock Float:operator--(Float:oper)
|
2004-02-08 14:32:35 +03:00
|
|
|
return oper-1.0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock Float:operator-(Float:oper)
|
2007-02-19 11:11:00 +03:00
|
|
|
return oper^Float:cellmin; /* IEEE values are sign/magnitude */
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock Float:operator*(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatmul(oper1, float(oper2)); /* "*" is commutative */
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock Float:operator/(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatdiv(oper1, float(oper2));
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock Float:operator/(oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatdiv(float(oper1), oper2);
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock Float:operator+(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatadd(oper1, float(oper2)); /* "+" is commutative */
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock Float:operator-(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatsub(oper1, float(oper2));
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock Float:operator-(oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatsub(float(oper1), oper2);
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator==(Float:oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, oper2) == 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator==(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, float(oper2)) == 0; /* "==" is commutative */
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator!=(Float:oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, oper2) != 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator!=(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, float(oper2)) != 0; /* "==" is commutative */
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator>(Float:oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, oper2) > 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator>(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, float(oper2)) > 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator>(oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(float(oper1), oper2) > 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator>=(Float:oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, oper2) >= 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator>=(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, float(oper2)) >= 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator>=(oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(float(oper1), oper2) >= 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator<(Float:oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, oper2) < 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator<(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, float(oper2)) < 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator<(oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(float(oper1), oper2) < 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator<=(Float:oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, oper2) <= 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator<=(Float:oper1, oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(oper1, float(oper2)) <= 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator<=(oper1, Float:oper2)
|
2004-02-08 14:32:35 +03:00
|
|
|
return floatcmp(float(oper1), oper2) <= 0;
|
2004-01-31 23:56:22 +03:00
|
|
|
|
|
|
|
stock bool:operator!(Float:oper)
|
2004-09-08 22:35:22 +04:00
|
|
|
return (_:oper & ((-1)/2)) == 0; /* -1 = all bits to 1; /2 = remove most significant bit (sign)
|
|
|
|
works on both 32bit and 64bit systems; no constant required */
|
2004-01-31 23:56:22 +03:00
|
|
|
/* forbidden operations */
|
|
|
|
forward operator%(Float:oper1, Float:oper2);
|
|
|
|
forward operator%(Float:oper1, oper2);
|
2007-03-09 06:04:40 +03:00
|
|
|
forward operator%(oper1, Float:oper2);
|
|
|
|
|
|
|
|
|
|
|
|
stock Float:floatmin(Float:ValueA, Float:ValueB)
|
|
|
|
{
|
|
|
|
if (ValueA<=ValueB)
|
|
|
|
{
|
|
|
|
return ValueA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValueB;
|
|
|
|
}
|
|
|
|
|
|
|
|
stock Float:floatmax(Float:ValueA, Float:ValueB)
|
|
|
|
{
|
|
|
|
if (ValueA>=ValueB)
|
|
|
|
{
|
|
|
|
return ValueA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValueB;
|
|
|
|
}
|
|
|
|
stock Float:floatclamp(Float:Value, Float:MinValue, Float:MaxValue)
|
|
|
|
{
|
|
|
|
if (Value<=MinValue)
|
|
|
|
{
|
|
|
|
return MinValue;
|
|
|
|
}
|
|
|
|
if (Value>=MaxValue)
|
|
|
|
{
|
|
|
|
return MaxValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Value;
|
|
|
|
}
|