Ruamoko: New math constants, some new math functions

Also, switch all of the math builtins functions we use from Rua to the
ones that accept and return floats, avoiding conversions to/from double.
This commit is contained in:
Jeff Teunissen 2011-12-09 22:36:41 -05:00
parent 054e52528e
commit 361255cf31
2 changed files with 163 additions and 49 deletions

View file

@ -47,100 +47,124 @@ static __attribute__ ((used)) const char rcsid[] =
static void static void
bi_sin (progs_t *pr) bi_sin (progs_t *pr)
{ {
R_FLOAT (pr) = sin (P_FLOAT (pr, 0)); R_FLOAT (pr) = sinf (P_FLOAT (pr, 0));
} }
static void static void
bi_cos (progs_t *pr) bi_cos (progs_t *pr)
{ {
R_FLOAT (pr) = cos (P_FLOAT (pr, 0)); R_FLOAT (pr) = cosf (P_FLOAT (pr, 0));
} }
static void static void
bi_tan (progs_t *pr) bi_tan (progs_t *pr)
{ {
R_FLOAT (pr) = tan (P_FLOAT (pr, 0)); R_FLOAT (pr) = tanf (P_FLOAT (pr, 0));
} }
static void static void
bi_asin (progs_t *pr) bi_asin (progs_t *pr)
{ {
R_FLOAT (pr) = asin (P_FLOAT (pr, 0)); R_FLOAT (pr) = asinf (P_FLOAT (pr, 0));
} }
static void static void
bi_acos (progs_t *pr) bi_acos (progs_t *pr)
{ {
R_FLOAT (pr) = acos (P_FLOAT (pr, 0)); R_FLOAT (pr) = acosf (P_FLOAT (pr, 0));
} }
static void static void
bi_atan (progs_t *pr) bi_atan (progs_t *pr)
{ {
R_FLOAT (pr) = atan (P_FLOAT (pr, 0)); R_FLOAT (pr) = atanf (P_FLOAT (pr, 0));
} }
static void static void
bi_atan2 (progs_t *pr) bi_atan2 (progs_t *pr)
{ {
R_FLOAT (pr) = atan2 (P_FLOAT (pr, 0), P_FLOAT (pr, 1)); R_FLOAT (pr) = atan2f (P_FLOAT (pr, 0), P_FLOAT (pr, 1));
} }
static void static void
bi_log (progs_t *pr) bi_log (progs_t *pr)
{ {
R_FLOAT (pr) = log (P_FLOAT (pr, 0)); R_FLOAT (pr) = logf (P_FLOAT (pr, 0));
}
static void
bi_log2 (progs_t *pr)
{
R_FLOAT (pr) = log2f (P_FLOAT (pr, 0));
} }
static void static void
bi_log10 (progs_t *pr) bi_log10 (progs_t *pr)
{ {
R_FLOAT (pr) = log10 (P_FLOAT (pr, 0)); R_FLOAT (pr) = log10f (P_FLOAT (pr, 0));
} }
static void static void
bi_pow (progs_t *pr) bi_pow (progs_t *pr)
{ {
R_FLOAT (pr) = pow (P_FLOAT (pr, 0), P_FLOAT (pr, 1)); R_FLOAT (pr) = powf (P_FLOAT (pr, 0), P_FLOAT (pr, 1));
}
static void
bi_sqrt (progs_t *pr)
{
R_FLOAT (pr) = sqrtf (P_FLOAT (pr, 0));
}
static void
bi_cbrt (progs_t *pr)
{
R_FLOAT (pr) = cbrtf (P_FLOAT (pr, 0));
}
static void
bi_hypot (progs_t *pr)
{
R_FLOAT (pr) = hypotf (P_FLOAT (pr, 0), P_FLOAT (pr, 1));
} }
static void static void
bi_sinh (progs_t *pr) bi_sinh (progs_t *pr)
{ {
R_FLOAT (pr) = sinh (P_FLOAT (pr, 0)); R_FLOAT (pr) = sinhf (P_FLOAT (pr, 0));
} }
static void static void
bi_cosh (progs_t *pr) bi_cosh (progs_t *pr)
{ {
R_FLOAT (pr) = cosh (P_FLOAT (pr, 0)); R_FLOAT (pr) = coshf (P_FLOAT (pr, 0));
} }
static void static void
bi_tanh (progs_t *pr) bi_tanh (progs_t *pr)
{ {
R_FLOAT (pr) = tanh (P_FLOAT (pr, 0)); R_FLOAT (pr) = tanhf (P_FLOAT (pr, 0));
} }
static void static void
bi_asinh (progs_t *pr) bi_asinh (progs_t *pr)
{ {
double y = P_FLOAT (pr, 0); double y = P_FLOAT (pr, 0);
R_FLOAT (pr) = log (y + sqrt (y * y + 1)); R_FLOAT (pr) = logf (y + sqrtf (y * y + 1));
} }
static void static void
bi_acosh (progs_t *pr) bi_acosh (progs_t *pr)
{ {
double y = P_FLOAT (pr, 0); double y = P_FLOAT (pr, 0);
R_FLOAT (pr) = log (y + sqrt (y * y - 1)); R_FLOAT (pr) = logf (y + sqrtf (y * y - 1));
} }
static void static void
bi_atanh (progs_t *pr) bi_atanh (progs_t *pr)
{ {
double y = P_FLOAT (pr, 0); double y = P_FLOAT (pr, 0);
R_FLOAT (pr) = log ((1 + y) / (1 - y)) / 2; R_FLOAT (pr) = logf ((1 + y) / (1 - y)) / 2;
} }
static builtin_t builtins[] = { static builtin_t builtins[] = {
@ -152,8 +176,12 @@ static builtin_t builtins[] = {
{"atan", bi_atan, -1}, {"atan", bi_atan, -1},
{"atan2", bi_atan2, -1}, {"atan2", bi_atan2, -1},
{"log", bi_log, -1}, {"log", bi_log, -1},
{"log2", bi_log2, -1},
{"log10", bi_log10, -1}, {"log10", bi_log10, -1},
{"pow", bi_pow, -1}, {"pow", bi_pow, -1},
{"sqrt", bi_sqrt, -1},
{"cbrt", bi_cbrt, -1},
{"hypot", bi_hypot, -1},
{"sinh", bi_sinh, -1}, {"sinh", bi_sinh, -1},
{"cosh", bi_cosh, -1}, {"cosh", bi_cosh, -1},
{"tanh", bi_tanh, -1}, {"tanh", bi_tanh, -1},

View file

@ -32,7 +32,7 @@
#define __ruamoko_math_h #define __ruamoko_math_h
/** /**
\defgroup math Math Functions \defgroup math Math Definitions
\{ \{
*/ */
@ -41,6 +41,8 @@
*/ */
@extern float random (void); @extern float random (void);
///\name Conversions
//\{
/** /**
Returns the integer component of \a f Returns the integer component of \a f
*/ */
@ -70,31 +72,43 @@
Returns the absolute value of \a f Returns the absolute value of \a f
*/ */
@extern float fabs (float f); @extern float fabs (float f);
//\}
/**************************************************************************** ///\name Exponentials and Logarithms
* VECTORS * //\{
****************************************************************************/ /**
Returns the natural log of \a x.
*/
@extern float log (float x);
/** /**
Transform vector \a v into a unit vector (a vector with a length of 1). Returns the base-2 log of \a x.
The direction is not changed, except for (possible) roundoff errors.
*/ */
@extern vector normalize (vector v); @extern float log2 (float x);
/** /**
Return the length of vector \a v Returns the base-10 log of \a x.
*/ */
@extern float vlen (vector v); @extern float log10 (float x);
/** /**
Returns the yaw angle ("bearing"), in degrees, associated with vector \a v. Returns \a x to the \a y power
*/ */
@extern float vectoyaw (vector v); @extern float pow (float x, float y);
/** /**
Returns a vector 'pitch yaw 0' corresponding to vector \a v. Returns the square root of \a x
*/ */
@extern vector vectoangles (vector v); @extern float sqrt (float x);
/**
Returns the cube root of \a x
*/
@extern float cbrt (float x);
//\}
///\name Trigonometric functions
//\{
/** /**
Returns the sine of \a x. Returns the sine of \a x.
@ -128,27 +142,99 @@
@extern float atan2 (float y, float x); @extern float atan2 (float y, float x);
/** /**
Returns the natural log of \a x. Returns the length of the hypotenuse of a right triangle with sides \a x
and \a y. That is, this function returns
<code>sqrt (\a x*\a x + \a y*\a y)</code>.
*/ */
@extern float log (float x); @extern float hypot (float x, float y);
/**
Returns the base-10 log of \a x.
*/
@extern float log10 (float x);
/**
Returns \a x to the \a y power
*/
@extern float pow (float x, float y);
@extern float sinh (float x);
@extern float cosh (float x);
@extern float tanh (float x);
@extern float asinh (float x);
@extern float acosh (float x);
@extern float atanh (float x);
//\} //\}
///\name Hyperbolic functions
//\{
/**
Returns the hyperbolic sine of \a x
*/
@extern float sinh (float x);
/**
Returns the hyperbolic cosine of \a x
*/
@extern float cosh (float x);
/**
Returns the hyperbolic tangent of \a x
*/
@extern float tanh (float x);
/**
Returns the area hyperbolic sine of \a x
*/
@extern float asinh (float x);
/**
Returns the area hyperbolic cosine of \a x
*/
@extern float acosh (float x);
/**
Returns the area hyperbolic tangent of \a x
*/
@extern float atanh (float x);
//\}
///\name Vector Functions
//\{
/**
Transform vector \a v into a unit vector (a vector with a length of 1).
The direction is not changed, except for (possible) rounding errors.
*/
@extern vector normalize (vector v);
/**
Return the length of vector \a v
*/
@extern float vlen (vector v);
/**
Returns the yaw angle ("bearing"), in degrees, associated with vector \a v.
*/
@extern float vectoyaw (vector v);
/**
Returns a vector 'pitch yaw 0' corresponding to vector \a v.
*/
@extern vector vectoangles (vector v);
//\}
/**
\name Constants
Constants for speeding up math calculations.
These constants are defined to replace some common math functions. Since
these are the same values that would be returned by any math functions or
floating-point calculations, these allow you to get the results without
actually calling them.
\note There does not appear to be a portable way to translate C's HUGE_VAL
value, the basis for positive and negative infinities, to Ruamoko.
\{
*/
# define M_E 2.7182818284590452354 ///< Euler's number \em e, the irrational base of the natural logarithm and a really neat thing
# define M_LOG2E 1.4426950408889634074 ///< The log, base 2, of \em e: <code>log2 (\ref M_E)</code>
# define M_LOG10E 0.43429448190325182765 ///< The log, base 10, of \em e: <code>log10 (\ref M_E)</code>
# define M_LN2 0.69314718055994530942 ///< The natural log evaluated at 2: <code>log (2)</code>
# define M_LN10 2.30258509299404568402 ///< The natural log evaluated at 10: <code>log (10)</code>
# define M_PI 3.14159265358979323846 ///< The most famous irrational number, and the sixteenth letter of the Greek alphabet
# define M_PI_2 1.57079632679489661923 ///< Half of pi, (\ref M_PI/2)
# define M_PI_4 0.78539816339744830962 ///< One quarter of pi, (\ref M_PI/4)
# define M_PI_6 0.52359877559829887308 ///< One sixth of pi, (\ref M_PI/6)
# define M_1_PI 0.31830988618379067154 ///< The reciprocal of pi, (1/\ref M_PI)
# define M_2_PI 0.63661977236758134308 ///< Twice the reciprocal of pi, (2/\ref M_PI)
# define M_2_SQRTPI 1.12837916709551257390 ///< Twice the reciprocal of the square root of pi, 2/\ref sqrt(\ref M_PI)
# define M_SQRT2 1.41421356237309504880 ///< The square root of 2
# define M_SQRT1_2 0.70710678118654752440 ///< 1/sqrt(2)
# define M_SQRT3 1.73205080756887729353 ///< The square root of 3
# define M_SQRT1_3 0.57735026918962576451 ///< 1/sqrt(3)
/**
\} Constants
\} Math Definitions
*/
#endif //__ruamoko_math_h #endif //__ruamoko_math_h