[Core] [Math] Fix fposmod() function #19279
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The output of the
fposmod
function is wrong. According to the documentation, the value "wraps equally in positive and negative". The docs describe correct behavior in the wording, but we can see that this is not the actual behavior with the example output and test code. Code:Output:
In the positives, the value wraps on a range of
[0, b)
(0 inclusive, b exclusive), while in the negatives, the value wraps around the range(0, b]
(0 exclusive, b inclusive). This is incorrect behavior. The values should wrap around in the same way in both the positives and negatives. Furthermore, passing the output back into the function should return the same value again.In fact, the whole implementation of the function is wrong. In theory returning
[0, b)
means negative values forb
would give negative results, but very strange results happen when I pass-5
.This is the correct implementation of
fposmod
overfmod
, always on the range[0, b)
:The only weird thing, is that the GDscript test code prints negative zero for some reason? I'm probably missing some minor tweak to make it appear as positive zero.EDIT: Adding0.0
removes the negative sign.