Bunch of math functions. More...
Macros | |
#define | sqr(x) ((x)*(x)) |
Computes the square of a number. | |
#define | cube(x) ((x)*(x)*(x)) |
Computes the cube of a number. | |
#define | PI 3.14159265358979323846264338327 |
The mathematical constant π (pi). | |
#define | MIN(x, y) ((x < y) ? x : y) |
Computes the minimum of two values. | |
#define | MAX(x, y) ((x > y) ? x : y) |
Computes the maximum of two values. | |
#define | CLAMP(x, y, z) (MIN(MAX(x,y),z)) |
Clamps a value between a minimum and maximum range. | |
Functions | |
f32 | frsqrt (f32 number) |
Computes the fast inverse square root of a given number using the "Fast Inverse Square Root" algorithm. | |
f32 | to_radians (f32 angle) |
Converts an angle from degrees to radians. | |
f32 | to_degrees (f32 angle) |
Converts an angle from radians to degrees. | |
f32 | float_approach (f32 *const origin, const f32 dest, const f32 factor) |
Approaches a target float by a given factor. | |
float * | vec3_approach (float *origin, float *dest, const f32 factor) |
Approaches a target vector by a given factor. | |
Bunch of math functions.
#define sqr | ( | x | ) | ((x)*(x)) |
Computes the square of a number.
This macro takes a number and returns its square by multiplying the number by itself.
x | The number to be squared. |
#define cube | ( | x | ) | ((x)*(x)*(x)) |
Computes the cube of a number.
This macro takes a number and returns its cube by multiplying the number by itself twice.
x | The number to be cubed. |
#define PI 3.14159265358979323846264338327 |
The mathematical constant π (pi).
This macro defines the value of π (pi) to a high degree of precision.
#define MIN | ( | x, | |
y | |||
) | ((x < y) ? x : y) |
Computes the minimum of two values.
This macro takes two values and returns the smaller of the two.
x | The first value. |
y | The second value. |
#define MAX | ( | x, | |
y | |||
) | ((x > y) ? x : y) |
Computes the maximum of two values.
This macro takes two values and returns the larger of the two.
x | The first value. |
y | The second value. |
Clamps a value between a minimum and maximum range.
This macro takes a value and ensures it falls within the specified range. If the value is less than the minimum, it returns the minimum. If the value is greater than the maximum, it returns the maximum. Otherwise, it returns the value itself.
x | The minimum allowable value. |
y | The value to be clamped. |
z | The maximum allowable value. |
Computes the fast inverse square root of a given number using the "Fast Inverse Square Root" algorithm.
number | {f32} The input number for which the inverse square root is to be calculated. Must be a positive number. |
This function utilizes a clever bit manipulation technique to compute the fast inverse square root, originally popularized by its use in the Quake III Arena graphics engine. The algorithm performs an initial approximation using a magic constant and then refines the result using one iteration of Newton's method for improved accuracy.
The formula used for the approximation is as follows:
x = number i = *(int*)&x // Bit-level hacking to get integer representation i = 0x5f3759df - (i >> 1) // The magic constant x = *(float*)&i // Convert back to float x = x * (1.5 - (number * 0.5 * x * x)) // Newton's method refinement
This function is typically used in graphics and game development, where performance is critical, and is suitable for scenarios where the input number is non-negative.
Converts an angle from degrees to radians.
angle | {f32} The angle in degrees to be converted. |
This function takes an angle specified in degrees and converts it to radians using the formula:
radians = degrees * (PI / 180)
where PI is a constant representing the mathematical constant π. The function returns the equivalent angle in radians, allowing for easier interpretation and usage in applications where radians are preferred.
Converts an angle from radians to degrees.
angle | {f32} The angle in radians to be converted. |
This function takes an angle specified in radians and converts it to degrees using the formula:
degrees = radians * (180 / PI)
where PI is a constant representing the mathematical constant π. The function returns the equivalent angle in degrees, allowing for easier interpretation and usage in applications where degrees are preferred.
Approaches a target float by a given factor.
a | {f32} The float to be approached. |
b | {f32} The target float. |
t | {f32} The approach factor. |
This macro moves float a towards float b by a factor of t.
float * vec3_approach | ( | float * | origin, |
float * | dest, | ||
const f32 | factor | ||
) |
Approaches a target vector by a given factor.
a | {vec3} The vector to be approached. |
b | {vec3} The target vector. |
t | {f32} The approach factor. |
This macro moves vector a towards vector b by a factor of t.