2022-03-01 19:29:42 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
2022-05-01 22:06:35 +00:00
|
|
|
/**
|
|
|
|
* Computes one `x` modulo `y` for floats.
|
|
|
|
*
|
|
|
|
* Acts like the standard C fmodf except does not handle Infinity. See https://en.cppreference.com/w/c/numeric/math/fmod
|
|
|
|
* for the details. It summarizes this function as follows:
|
|
|
|
* "The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y,
|
|
|
|
* where n is x/y with its fractional part truncated.
|
|
|
|
*
|
|
|
|
* The returned value has the same sign as x and is less or equal to y in magnitude."
|
|
|
|
*
|
|
|
|
* @param x dividend
|
|
|
|
* @param y modulus
|
|
|
|
*
|
|
|
|
* @return f32 0.0f if y is 0.0f, or x modulo y otherwise
|
|
|
|
*/
|
2022-03-01 19:29:42 +00:00
|
|
|
f32 fmodf(f32 x, f32 y) {
|
2022-05-01 22:06:35 +00:00
|
|
|
s32 n;
|
2022-03-01 19:29:42 +00:00
|
|
|
|
|
|
|
if (y == 0.0f) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2022-05-01 22:06:35 +00:00
|
|
|
n = x / y;
|
2022-03-01 19:29:42 +00:00
|
|
|
|
2022-05-01 22:06:35 +00:00
|
|
|
return x - (n * y);
|
2022-03-01 19:29:42 +00:00
|
|
|
}
|