### Overview

In the course of implementing QuakeScheme and integrating Ogg Vorbis, I had need for additional math functions not provided in `bg_lib.c` nor `q_math.c`.

The file `q_math.c` is not supposed to be modified (which presumably includes adding functions), so I created a new file.

### Usage

• `int Q_floor (float x)` - largest integer value that doesn't exceed x.
• `int Q_ceil (float x)` - Round x to nearest integer, half-way case rounds towards even number.
• `int Q_trunc (float x)` - Truncates x (removes fractional portion).
• `int Q_round (float x)` - Rounds x to nearest integer, half-way case rounds away from zero.
• `float Q_exp (float x)` - Returns value of the transcendental number e raised to the power of x.
• `float Q_log (float x)` - Returns natural logarithm of x.
• `float Q_sin (float x)` - Returns sine of x.
• `float Q_cos (float x)` - Returns cosine of x.
• `float Q_tan (float x)` - Returns tangent of x.
• `float Q_asin (float x)` - Returns arcsine of x.
• `float Q_acos (float x)` - Returns arccosine of x.
• `float Q_atan (float x)` - Returns arctangent of x.
• `float Q_atan2 (float y, float x)` - Returns arctangent of y/x.
• `float Q_sqrt (float x)` - Returns arctangent of x.
• `float Q_hypot (float a, float b)` - Returns the hypotenuse of a right triange with legs lengths of a and b.
• `float Q_rint (float x)` - Rounds x to nearest integer as float, but (supposed to) raises the inexact exception if the return value differs from x by too much (meaning of "too much" varies...).
• `long ldexp (double x, int y)` - Returns x multiplied by 2 to the power of y (x * 2y).

### Limitations

The logarithmic functions are horridly inaccurate (e.g. 2^5 == exp(5*log(2)) => 32.000003).

The complete set of functions in `math.h` is not provided.

### Programming Notes

Q_sin, Q_cos, Q_atan2, and Q_sqrt are just wrappers around the provided syscalls.

Antilogarithm is calculated using a 1024-element antilogarithm lookup table and linear interpolation. The 1024 elements span the domain 1 to e. The range is thus from 0.0 to 1.0. Logarithm is calculated by searching the other side of the table.

Logarithmic functions were developed before the rest of `bg_math.c`, which is why logs are separated from the rest. The log lookup table (`lntable.frag`) was programmatically generated.

(2002.02.23)

Files:

Or as a tarball: bg_math-20020223.tar.gz (35KB) ("polluting" form)
-- PhaethonH (PhaethonH@gmail.com)