2004-01-31 20:56:22 +00:00
|
|
|
/* Float arithmetic
|
2004-02-08 11:32:35 +00: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 08:34:52 +00:00
|
|
|
#if defined _float_included
|
|
|
|
#endinput
|
|
|
|
#endif
|
|
|
|
#define _float_included
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
#pragma rational Float
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Different methods of rounding
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
enum floatround_method {
|
|
|
|
floatround_round = 0,
|
|
|
|
floatround_floor,
|
|
|
|
floatround_ceil,
|
|
|
|
floatround_tozero
|
2007-08-10 04:52:12 +00:00
|
|
|
};
|
2004-01-31 20:56:22 +00:00
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Different units of measurement for angles
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
enum anglemode {
|
|
|
|
radian = 0,
|
|
|
|
degrees,
|
|
|
|
grades
|
2007-08-10 04:52:12 +00:00
|
|
|
};
|
2004-02-08 11:32:35 +00:00
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Converts an integer into a floating point value.
|
|
|
|
*
|
|
|
|
* @param value Value to be converted
|
|
|
|
*
|
|
|
|
* @return Converted value
|
|
|
|
*/
|
2004-01-31 20:56:22 +00:00
|
|
|
native Float:float(value);
|
2004-02-08 11:32:35 +00:00
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Converts a string into a floating point value.
|
|
|
|
*
|
|
|
|
* @param string Input string to be converted
|
|
|
|
*
|
|
|
|
* @return Converted value
|
|
|
|
*/
|
2004-01-31 20:56:22 +00:00
|
|
|
native Float:floatstr(const string[]);
|
2004-02-08 11:32:35 +00:00
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the fractional part of a floating point value
|
|
|
|
*
|
|
|
|
* @param string Floating point value to get the fractional part from
|
|
|
|
*
|
|
|
|
* @return The fractional part
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
native Float:floatfract(Float:value);
|
2004-01-31 20:56:22 +00:00
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Rounds a floating point value to an integer value
|
|
|
|
*
|
|
|
|
* @note For the list of available rounding methods look at
|
|
|
|
* floatround_method enumeration.
|
|
|
|
*
|
|
|
|
* @param value Floating point value to be rounded
|
|
|
|
* @param method Rounding method
|
|
|
|
*
|
|
|
|
* @return Converted value
|
|
|
|
*/
|
2004-01-31 20:56:22 +00:00
|
|
|
native floatround(Float:value, floatround_method:method=floatround_round);
|
2004-02-08 11:32:35 +00:00
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Compares two floating point values.
|
|
|
|
*
|
|
|
|
* @param fOne First value to be compared
|
|
|
|
* @param fTwo Second value to be compared
|
|
|
|
*
|
|
|
|
* @return If arguments are equal, returns 0.
|
|
|
|
* If the first one is greater, returns 1.
|
|
|
|
* If the second one is greater, returns -1.
|
|
|
|
*/
|
2004-01-31 20:56:22 +00:00
|
|
|
native floatcmp(Float:fOne, Float:fTwo);
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the square root of a floating point value
|
|
|
|
*
|
|
|
|
* @note Same as floatpower(value, 0.5)
|
|
|
|
*
|
|
|
|
* @param value Floating point value to get square root from
|
|
|
|
*
|
|
|
|
* @return Square root of the input value
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
native Float:floatsqroot(Float:value);
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the value raised to the power of the exponent
|
|
|
|
*
|
|
|
|
* @param value Floating point value to be raised
|
|
|
|
* @param exponent The exponent
|
|
|
|
*
|
|
|
|
* @return Value raised to the power of the exponent
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
native Float:floatpower(Float:value, Float:exponent);
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the logarithm of value
|
|
|
|
*
|
|
|
|
* @param value Floating point value to calculate the logarithm for
|
|
|
|
* @param base The optional logarithmic base to use.
|
|
|
|
* Defaults to 10, or the natural logarithm
|
|
|
|
*
|
|
|
|
* @return Square root of the input value
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
native Float:floatlog(Float:value, Float:base=10.0);
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the sine of a given angle
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The angle to calculate the sine from
|
|
|
|
* @param mode What unit of measurement is the angle specified in
|
|
|
|
* Defaults to radians
|
|
|
|
*
|
|
|
|
* @return The sine of a given angle
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
native Float:floatsin(Float:value, anglemode:mode=radian);
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the cosine of a given angle
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The angle to calculate the cosine from
|
|
|
|
* @param mode What unit of measurement is the angle specified in
|
|
|
|
* Defaults to radians
|
|
|
|
*
|
|
|
|
* @return The cosine of a given angle
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
native Float:floatcos(Float:value, anglemode:mode=radian);
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the tangent of a given angle
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The angle to calculate the tangent from
|
|
|
|
* @param mode What unit of measurement is the angle specified in
|
|
|
|
* Defaults to radians
|
|
|
|
*
|
|
|
|
* @return The tangent of a given angle
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
native Float:floattan(Float:value, anglemode:mode=radian);
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the hyperbolic sine of a given angle
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The angle to calculate the hyperbolic sine from
|
|
|
|
* @param mode What unit of measurement is the angle specified in
|
|
|
|
* Defaults to radians
|
|
|
|
*
|
|
|
|
* @return The hyperbolic sine of a given angle
|
|
|
|
*/
|
2006-09-01 03:29:27 +00:00
|
|
|
native Float:floatsinh(Float:angle, anglemode:mode=radian);
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the hyperbolic cosine of a given angle
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The angle to calculate the hyperbolic cosine from
|
|
|
|
* @param mode What unit of measurement is the angle specified in
|
|
|
|
* Defaults to radians
|
|
|
|
*
|
|
|
|
* @return The hyperbolic cosine of a given angle
|
|
|
|
*/
|
2006-09-01 03:29:27 +00:00
|
|
|
native Float:floatcosh(Float:angle, anglemode:mode=radian);
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the hyperbolic tangent of a given angle
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The angle to calculate the hyperbolic tangent from
|
|
|
|
* @param mode What unit of measurement is the angle specified in
|
|
|
|
* Defaults to radians
|
|
|
|
*
|
|
|
|
* @return The hyperbolic tangent of a given angle
|
|
|
|
*/
|
2006-09-01 03:29:27 +00:00
|
|
|
native Float:floattanh(Float:angle, anglemode:mode=radian);
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the absolute value of a floating point value
|
|
|
|
*
|
|
|
|
* @param value The floating point value to get the absolute value from
|
|
|
|
*
|
|
|
|
* @return The absolute value
|
|
|
|
*/
|
2004-02-08 11:32:35 +00:00
|
|
|
native Float:floatabs(Float:value);
|
|
|
|
|
2006-09-01 03:29:27 +00:00
|
|
|
/* Return the angle of a sine, cosine or tangent.
|
|
|
|
* The output angle may be in radians, degrees, or grades. */
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the angle of the given tangent
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The tangent to calculate the angle from
|
|
|
|
* @param mode What unit of measurement should the output angle be in
|
|
|
|
*
|
|
|
|
* @return The angle of a tangent
|
|
|
|
*/
|
2007-02-24 20:07:21 +00:00
|
|
|
native Float:floatatan(Float:angle, {anglemode,_}:radix);
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the angle of the given cosine
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The cosine to calculate the angle from
|
|
|
|
* @param mode What unit of measurement should the output angle be in
|
|
|
|
*
|
|
|
|
* @return The angle of a cosine
|
|
|
|
*/
|
2007-02-24 20:07:21 +00:00
|
|
|
native Float:floatacos(Float:angle, {anglemode,_}:radix);
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the angle of the given sine
|
|
|
|
*
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param value The sine to calculate the angle from
|
|
|
|
* @param mode What unit of measurement should the output angle be in
|
|
|
|
*
|
|
|
|
* @return The angle of a sine
|
|
|
|
*/
|
2007-02-24 20:07:21 +00:00
|
|
|
native Float:floatasin(Float:angle, {anglemode,_}:radix);
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the principal value of arctangent of y/x
|
|
|
|
*
|
|
|
|
* @note Someone should verify this native, not sure what it actually does.
|
|
|
|
* @note For available units of measurements(modes) look at the anglemode enum
|
|
|
|
*
|
|
|
|
* @param x Value representing the proportion of the x-coordinate.
|
|
|
|
* @param y Value representing the proportion of the x-coordinate.
|
|
|
|
* @param mode What unit of measurement should the output angle be in
|
|
|
|
*
|
|
|
|
* @return Arctangent of y/x
|
|
|
|
*/
|
2007-02-24 20:07:21 +00:00
|
|
|
native Float:floatatan2(Float:x, Float:y, {anglemode,_}:radix);
|
2004-06-24 22:11:32 +00:00
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Multiply two floats together */
|
|
|
|
native Float:floatmul(Float:oper1, Float:oper2);
|
|
|
|
|
|
|
|
/* Divide the dividend float by the divisor float */
|
|
|
|
native Float:floatdiv(Float:dividend, Float:divisor);
|
|
|
|
|
|
|
|
/* Add two floats together */
|
|
|
|
native Float:floatadd(Float:dividend, Float:divisor);
|
|
|
|
|
|
|
|
/* Subtract oper2 float from oper1 float */
|
|
|
|
native Float:floatsub(Float:oper1, Float:oper2);
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
/* 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 11:32:35 +00:00
|
|
|
return oper+1.0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock Float:operator--(Float:oper)
|
2004-02-08 11:32:35 +00:00
|
|
|
return oper-1.0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock Float:operator-(Float:oper)
|
2007-02-19 08:11:00 +00:00
|
|
|
return oper^Float:cellmin; /* IEEE values are sign/magnitude */
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock Float:operator*(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatmul(oper1, float(oper2)); /* "*" is commutative */
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock Float:operator/(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatdiv(oper1, float(oper2));
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock Float:operator/(oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatdiv(float(oper1), oper2);
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock Float:operator+(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatadd(oper1, float(oper2)); /* "+" is commutative */
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock Float:operator-(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatsub(oper1, float(oper2));
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock Float:operator-(oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatsub(float(oper1), oper2);
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator==(Float:oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, oper2) == 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator==(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, float(oper2)) == 0; /* "==" is commutative */
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator!=(Float:oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, oper2) != 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator!=(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, float(oper2)) != 0; /* "==" is commutative */
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator>(Float:oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, oper2) > 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator>(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, float(oper2)) > 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator>(oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(float(oper1), oper2) > 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator>=(Float:oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, oper2) >= 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator>=(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, float(oper2)) >= 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator>=(oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(float(oper1), oper2) >= 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator<(Float:oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, oper2) < 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator<(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, float(oper2)) < 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator<(oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(float(oper1), oper2) < 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator<=(Float:oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, oper2) <= 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator<=(Float:oper1, oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(oper1, float(oper2)) <= 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator<=(oper1, Float:oper2)
|
2004-02-08 11:32:35 +00:00
|
|
|
return floatcmp(float(oper1), oper2) <= 0;
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
stock bool:operator!(Float:oper)
|
2017-12-08 23:22:43 +00: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 20:56:22 +00:00
|
|
|
/* forbidden operations */
|
|
|
|
forward operator%(Float:oper1, Float:oper2);
|
|
|
|
forward operator%(Float:oper1, oper2);
|
2007-03-09 03:04:40 +00:00
|
|
|
forward operator%(oper1, Float:oper2);
|
|
|
|
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns whichever value is the smaller one
|
|
|
|
*
|
|
|
|
* @param ValueA The first value
|
|
|
|
* @param ValueB The second value
|
|
|
|
*
|
|
|
|
* @return ValueA if it is smaller than ValueB, and vice versa
|
|
|
|
*/
|
2007-03-09 03:04:40 +00:00
|
|
|
stock Float:floatmin(Float:ValueA, Float:ValueB)
|
|
|
|
{
|
|
|
|
if (ValueA<=ValueB)
|
|
|
|
{
|
|
|
|
return ValueA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValueB;
|
|
|
|
}
|
|
|
|
|
2017-12-08 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns whichever value is the greater one
|
|
|
|
*
|
|
|
|
* @param ValueA The first value
|
|
|
|
* @param ValueB The second value
|
|
|
|
*
|
|
|
|
* @return ValueA if it is greater than ValueB, and vice versa
|
|
|
|
*/
|
2007-03-09 03:04:40 +00:00
|
|
|
stock Float:floatmax(Float:ValueA, Float:ValueB)
|
|
|
|
{
|
|
|
|
if (ValueA>=ValueB)
|
|
|
|
{
|
|
|
|
return ValueA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValueB;
|
|
|
|
}
|
2017-12-08 23:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clamps a value between a minimum and a maximum floating point value
|
|
|
|
*
|
|
|
|
* @param Value The value to be clamped
|
|
|
|
* @param MinValue Minimum value
|
|
|
|
* @param MaxValue Maximum value
|
|
|
|
*
|
|
|
|
* @return The Value clamped between MinValue and MaxValue
|
|
|
|
*/
|
2007-03-09 03:04:40 +00:00
|
|
|
stock Float:floatclamp(Float:Value, Float:MinValue, Float:MaxValue)
|
|
|
|
{
|
|
|
|
if (Value<=MinValue)
|
|
|
|
{
|
|
|
|
return MinValue;
|
|
|
|
}
|
|
|
|
if (Value>=MaxValue)
|
|
|
|
{
|
|
|
|
return MaxValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Value;
|
2017-12-08 23:22:43 +00:00
|
|
|
}
|