-
-
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() is not functioning properly #22216
Comments
|
Thanks for your response but issue is with decimals() function not with the division. To prove it: just try my debug project but simply state 'temp_wind = 22.5' instead of 'temp_wind = wind_speed /2'. The result will still be: '22.5 contains no decimals' |
I was confusing this to be trying to report a total decimal place count. |
I've been playing around with some different values: print ("result of decimals(1.0) is " + str(decimals(1.0))) Which yielded: result of decimals(1.0) is 0 From my tests it seems that decimals() is returning 0 for any value greater than 1.0 and has an issue with the number 0 as that returns a result of 9. |
The function seems to work fine, it's just that it's kind of pointless. The docs are slightly unclear when they say "Returns the position of the first non-zero digit, after the decimal point.", as they should say "Returns the position of the first non-zero digit, taking into account the integral part." So |
Thanks for your reply! May I ask why decimals(0) returns a value of 9 then though? Shouldn't it return 0? |
Probably because some casting happens internally from int to float and In any scenario this method is bogus as heck and I suggest that we drop it as soon as we can :P |
Deal! I'll just use another way to randomly decide if my clouds go left or right ;-) |
See also #21425 which renames this function to |
With all due respect, but I disagree. I'm wondering why you would call a function 'decimals' if it includes checking the integral part? If I were to look at that kind of function without prior knowledge, I'm quite certain I'd assume it to focus solely on the part after the comma, i.e. decimals. After checking the docs, it does state so quite clearly, too: 'Returns the position of the first non-zero digit, after the decimal point.' I've played around with the function a bit, and I'm wondering, is it acceptable to cast to String within the math-class? Also, it seems to work fine without referencing 'ustring.h' at the top, but is it the standard to do so, anyway? Sorry, I'm relatively new to C++ and am trying to cover all my bases, as well as the best coding practices for Godot, before creating a Pull Request. Thanks in advance! EDIT: Specifically, what I currently have, is the following:
It produces the following result (code taken from @DanielReyns with addition of integer 0 and 1, as well as very small decimals):
The above log shows that it's precise up to 6 decimal points, after that it, uhh, what's the word? Abbreviates? Shortens? Floors/Ceilings? Anyway, like I said, max. 6 decimal points. |
@DeuxAlpha I would say that it underflows relative to the string representation. Godot's implementation works purely based on the float value, it arbitrarily stops at 9, but with float precision you could go to 30-something if you wish. EDIT: To clarify, 30-something is only possible with floats that are 0.000something, bringing the exponent down, as 1.000 would only ever have 6 decimal places of precision. |
Agreed, I was able to ramp the precision up to 14 decimal places within the string-method, but I don't want to rely on it in this instance. I'll try to approach it from another angle. Thanks for the input @aaronfranke! EDIT: Then again, don't mind my rambling... |
If someone can fix the implementation so that it behaves like described in the docs (i.e. strip the integral part and the count number of leading zero digits / place of the first non-zero digit), that would be welcome. I don't think a String approach would be good though, that's likely to be very slow. |
@akien-mga Done, how's this? I left it as a separate commit from my "add step_decimals as an alias and fix the C# method" commit (by the way, does not break compat anymore).
Output:
|
Looks great! How does it handle |
The way it handles floating precision errors (the same as it did before) is by comparing the numbers to 0.09999, etc, instead of 0.1. This is fine as far as I can tell. |
I initially misunderstood that a decimal counter. I haven't yet run into a situation where I need to know the decimal position of the first none zero decimal place. Now I'm very curious what this gets used for generally? |
Godot version: 3.0.6.stable.official.8314054
OS/device including version: Windows 10
Issue description:
The function Decimals returns 0 even when I have a decimal number.
information in Godot's API reference:
Steps to reproduce:
code used:
Minimal reproduction project:
Debug Project.zip
In order to easily reproduce just relaunch project over and over untill you get an odd value, or just assign wind_speed directly with a non random odd number. In the debug project I've made sure to print the result in the debug window yielding:
I hope this information suffices to look into the issue.
Thanks in advance!
The text was updated successfully, but these errors were encountered: