-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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::round icorrectly rounds big values #48841
Comments
I suspect there is a problem in your test. When I tried debugging:
In the code, the results after the round are the same, i.e. DBL_MAX is the same rounded. What could cause an INF is if double is being truncated to float somewhere in the test. The other possibility is different behaviour of your CPU (maybe using SSE or rounding mode), but a problem with the test seems more likely. |
And this correctly returns true on my machine:
What OS / CPU are you trying this on? And can you replicate it in c++? Curiously, on 3.x at least, The There may even be some compiler quirks as to how this is handled, particularly with inline functions. See here for an interesting discussion: |
Oh, sorry, I a little overlooked. You are right But const double value = 9007199254740991;
const double round1 = std::round(value) << std::endl; // 9007199254740991.0
const double round2 = Math::round(value) << std::endl; // 9007199254740992.0 The fix suggested for return (p_val >= 0) ? Math::ceil(p_val - 0.5) : Math::floor(p_val + 0.5); Tested on Linux with GCC 11.1.0. |
There is another problem caused by the implementation of var x = 4503599627370497.0
print(x)
print(round(x))
print(floor(x)) Output:
|
@aaronfranke, I was ahead of you by a couple of seconds :D |
@Shatur95 Can you test if #48887 fixes this bug for you? |
@aaronfranke, yes, thank you! |
Godot version:
6dad72d
OS/device including version:
Any
Issue description:
Math::round
returnsinf
forDBL_MAX
because it adds0.5
to the passed value that causes overflow for maximum value:godot/core/math/math_funcs.h
Line 278 in 365ab88
Steps to reproduce:
Minimal reproduction project:
Just add the following check:
CHECK(Math::is_equal_approx(Math::round(DBL_MAX), DBL_MAX));
to
round/floor/ceil
test case in this PR: #48721The text was updated successfully, but these errors were encountered: