Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math:fmod: Implement a floating-point remainder function #181

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

math:fmod: Implement a floating-point remainder function #181

wants to merge 8 commits into from

Conversation

mansourmoufid
Copy link
Contributor

Hello,

This implements a floating-point remainder function, similar to the integer modulo operation.

This function can be useful for trigonometric operations which expect input to be bounded, for example to [0, 2pi].

It is done in three parts. First, the _fmod function is defined as a static inline function in a header file, so that its code can be re-used internally by other developers (of sine, cosine, etc). Second, the p_fmod_f32 function is declared as part of the library. Finally, the function p_fmod_2pi_f32 may be useful since [0, 2pi] is the typical domain of the trigonometric functions.

I implemented these as an experiment after discussion in #166. I hope others can find them useful.

Add a basic implementation of the function p_fmod_f32.

The function _fmod is defined as static inline in the p_fmod.h
header so that other functions in that directory could use it.

Signed-off-by: Mansour Moufid <mansourmoufid@gmail.com>
Signed-off-by: Mansour Moufid <mansourmoufid@gmail.com>
Signed-off-by: Mansour Moufid <mansourmoufid@gmail.com>
Signed-off-by: Mansour Moufid <mansourmoufid@gmail.com>
Signed-off-by: Mansour Moufid <mansourmoufid@gmail.com>
Use unambiguous variable names.

Signed-off-by: Mansour Moufid <mansourmoufid@gmail.com>
Signed-off-by: Mansour Moufid <mansourmoufid@gmail.com>
Signed-off-by: Mansour Moufid <mansourmoufid@gmail.com>
@mansourmoufid
Copy link
Contributor Author

By the way, I wasn't sure how to generate the gold data file, so I just used a bit of Python:

>>> import math
>>> import random
>>> n = 100
>>> x = [random.uniform(-100.0, 100.0) for i in range(n)]
>>> twopi = 2.0 * math.pi
>>> _fmod = lambda x: math.fmod(a, twopi) + (0.0 if x > 0.0 else twopi)
>>> data = [(a, 0.0, 0.0, _fmod(a)) for a in x]
>>> for (a, b, c, d) in data:
...     print("{:.6f},{:.6f},{:.6f},{:.6f}".format(a, b, c, d))

Let me know if that's incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant