-
Notifications
You must be signed in to change notification settings - Fork 3k
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
binary_to_float doesn't correctly handle scientific notation with no decimal places specified, such as "1e0" #9061
Comments
Native support for this format could slightly speed up Line 1167 in 2b23f5b
|
In my mind, the
|
Thanks for raising this issue! I've been going back and forth on this, I think it makes sense to support it (at least with an option) together with bases 2 and 16 from #9106, but I need to think a bit more about the implementation. Currently it lands in |
Erlang has held on to the principle "in order to be a floating point number, it has to actually have contain a decimal point", which goes all the way back to its Prolog roots, but you can argue that floating point literals are such a common ground that they should be universally interchangeable across languages. I think it was a design mistake by the C folks to allow the dotless 1e0 version, but since it's there in C, Java, JS, etc., we'd better support it too. Possibly we could accept the dotless form in the scanner and conversion functions, but let the compiler emit a warning if you actually use that form in code, saying something like "for readability, include the decimal point". |
Had to test this for myself, because I'd never heard about it, but it's there! But not documented, it seems? Can the commas be dropped? And if we allow "1e0" we should also allow ".01" (perhaps not warranting a compiler warning). |
Yes, I think we should drop support for commas. I didn't know they were allowed. |
There’s a wrinkle to that. If we keep using I think we should consider vendoring something like https://github.com/fastfloat/fast_float to get around these issues. |
I took a quick look, and only C/C++ strtod cares about locale. Java and Javascript only accept '.'. Seems reasonable to do the same. |
Indeed, my comment was that we have to handle this nonsense if our implementation lands in |
Likely because in quite a few locales comma is the default decimal separator |
Describe the bug
The binary_to_float built in function should support numbers in scientific notation without decimal places specified.
To Reproduce
The following raises a badarg error:
binary_to_float(<<"1e0">>).
Expected behavior
1 = binary_to_float(<<"1e0">>).
Affected versions
OTP 27 and all prior versions with this function.
Additional context
Other languages, such as C++ and Golang will use this format when all decimal places are 0, so support for this format is important for cross-compatibility.
The text was updated successfully, but these errors were encountered: