-
-
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
is_equal_approx works incorrectly with doubles #48876
Comments
We basically have two options here.
A bit of history: this function used |
For (2), using a similar approach to
Personally I would be tempted to go for this approach, rather than treat everything as doubles - at least from c++. On the flip side I don't know how often Another thing is that apart from the conversions, the best epsilon to use might be different for float and doubles. The best epsilon also presumably varies according to where in the range you are comparing, as precision is higher close to zero than FLT_MAX... but I'm not sure there's a lot you can reasonably do about that - as the error also depends on prior operations etc. That's partly why real care has to be taken with blanket CMP_EPSILON type defines. |
I think this is fine. We have similar warnings for comparisons between different types (e.g I guess we'll see how many such warnings are raised by existing code if this gets implemented. If it's too many/too tedious we can consider another option. (I just hope that |
Actually come to think of it, you can also deal with this using the following approach (with casts as needed). It should compile out:
|
Godot version:
3.x (3.3.1)
Probably also 4.x
OS/device including version:
N/A
Issue description:
is_equal_approx
function works incorrectly for doubles.Steps to reproduce:
Explanation
While looking at #48841 I noticed this bug.
It occurs because
is_equal_approx
is defined using real_t, which is currently float. When you call this function with doubles outside float range, I'm presuming they are truncated to INF, and comparison of INF to INF comes out as equal.The solution is probably to define specific versions of these functions for float and for double, and probably let the compiler choose which to use when passing real_t. This problem may also be present in other math functions, I haven't looked through.
I'm not planning on doing a PR for this BTW, so anyone interested can take a look.
The text was updated successfully, but these errors were encountered: