-
-
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
decimals() always return 4 when there are decimals #1955
Comments
and what's the use case? |
For convert a Rect2 with a negative size in a equivalent Rect2 with a positive size. The pos/end of the second is shifted of the size's number of decimals (pow(10, -number)). |
This is a hacky work around but does work.
or
|
I'm a joke with mathematics too, but you can achieve that easily multiplying the negative number by -1.0, the result will give you exactly the same number but positive. El 22 de mayo de 2015 01:20:45 CST, BBric [email protected] escribió:
Sent from my Android device with K-9 Mail. Please excuse my brevity. |
This works only with the size, but not with the position. max(0, str(abs(number - int(number))).length() - 2) It is not excessively complex of course but if this function exists it should work. |
Please try to be a bit more explicit in your issue titles and in the description, I think none of the commenters understood that you were reporting a bug in the built-in decimals() function, we likely all assumed you were asking to add such a function (and the "4 ?" are therefore hard to understand, but well). In the end I tried myself and could understand that indeed, the decimals() built-in function always returns 4. I'll fix that. |
This is a typical case of precision issue with floating point numbers. The problematic code is https://github.com/godotengine/godot/blob/897a1aa/core/math/math_funcs.cpp#L207-L220 When trying to analyse 5.133 for example, p_step will successively take the following values:
So it returns 4 (4 steps) instead of 3. I've googled about this a bit, and it's a typical issue, the main solution given being to parse a string instead of trying to do math with floating points. I've tried to improve the algorithm but did not manage to reach something working so far. |
The best I could come up with seems to work fine for numbers with less than 4 digits (the "max" in the function), but returns 0 for numbers with more than 4 digits:
I've tried to make some test before the while to check if the p_step has more than 4 digits, but I did not manage to find something that works. @reduz Any clue? |
Quick side question, what should |
I made a quick search on the repo and it looks like Math::decimals() is used in two places in the engine:
And the algorithm is somewhat reimplemented here: godot/tools/editor/animation_editor.cpp Lines 1191 to 1209 in 7d8f187
I don't know about the re-implementation, but we should check the two usages of Math::decimals() to see if they are broken currently, and if we could replace case in the case of dropping Math::decimals() completely. |
So by looking at the above two usages of Math::decimals(), I guess it could be worth implementing using string manipulation, and then removed from the GDScript API as it's too confusing to be presented as a reliable method IMO. For the engine, as well as the input floats are well controlled, it should be ok (here it's mostly about knowing the number of decimals of a range's step for example in the spin_box.cpp example). |
The aim of this function is well to count the number of decimals after the point ?
The text was updated successfully, but these errors were encountered: