[erlang-questions] String representations of floating point numbers

Bob Ippolito bob@REDACTED
Sat Mar 16 01:32:54 CET 2019


If you dive into the implementation it's effectively a wrapper around
strtod from C with a validation pass that is more strict than the strtod
standard.

https://github.com/erlang/otp/blob/OTP-21.3/erts/emulator/sys/win32/sys_float.c#L54
https://github.com/erlang/otp/blob/OTP-21.3/erts/emulator/sys/unix/sys_float.c#L732

So as far as a regex goes it would be something like this:

[+-]?\d+[.,]\d+([eE][+-]?\d+)

The major differences between this and other popular float grammars are:

* At least one digit is required in each part
* Both integer and fractional parts are required, even if there's an
exponent part (so "1", ".1", "1e-1" would not be valid)
* The decimal separator is either , or . (the implementation will try the
other if necessary to compensate for a different locale)


On Fri, Mar 15, 2019 at 3:52 PM Hugo Mills <hugo@REDACTED> wrote:

>    Where in the manual is the set of allowable string representations
> of floating point numbers documented? I'd have expected it to be here:
>
> http://erlang.org/doc/reference_manual/data_types.html
>
> ... but apparently not.
>
>    Specifically, I'm trying to use list_to_float/1, and I've been
> trying to reverse engineer it:
>
> 1> list_to_float("-1").
> ** exception error: bad argument
>      in function  list_to_float/1
>         called as list_to_float("-1")
> 2> list_to_float("-1.0").
> -1.0
> 3> list_to_float("-1.0e-23").
> -1.0e-23
> 4> list_to_float("-1e-23").
> ** exception error: bad argument
>      in function  list_to_float/1
>         called as list_to_float("-1e-23")
> 5> list_to_float(".3").
> ** exception error: bad argument
>      in function  list_to_float/1
>         called as list_to_float(".3")
>
>    An actual written specification would be really handy here. Even
> just a regex or EBNF for them. I'm writing a parser for something
> where the definition of floating point literals isn't quite the same
> as Erlang's, and it's a bit painful.
>
>    Hugo.
>
> --
> Hugo Mills             | Your problem is that you've got too much taste to
> be
> hugo@REDACTED carfax.org.uk | a web developer.
> http://carfax.org.uk/  |
> PGP: E2AB1DE4          |                                          Steve
> Harris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.12 (GNU/Linux)
>
> iQIbBAEBAgAGBQJcjCyYAAoJEFheFHXiqx3kBo8P+KYpzGUmaV4lA8jopR5h39S8
> XMt6e+mO4zceiz1LE9mGFozayPPgPlchuJz3h/9EgtvNKXV8AvUGLFOZHtcE5qeM
> wDwQg6kreHW/5ejrpYFCH1rZt9DftnAzMQo0SXOCQTfLkAC5W/jRrZyAE8ibVEH+
> JoAOG/ZRUTz4E3bnoSGfcrY13IwD5A3sJ3Jex6rLttVjJyfCEbBJMx5ofFXO9WbY
> PfggXSD7EALHdUOT1vQWJsRiJClq25HjAucblA+zst/8N1wllpRnK3uhA1M22lNB
> OuYv0lnvKwYmyNSs/VcXRAne5CXjpf/CxJpVMuvXdbRYxrlsybWz9+UWMk7DB+TA
> fQvCRIGROG/DlVWo8KAoMN6ZgfxE3pmVjfN3p2usDza2V43QAY+jaZF7x2SyxKxo
> Bw4/jW7RODduzWu/XQvRX0780RQlbPbNcFjrOHIt3PZe7+Qtl++6QQcVCJ+TrE8h
> 1drKkbFvm2/ecyAXdMkXV8CxiJdbgcSkz2kgIYhTUpiGJz0ubCt3HOA/byh3P8em
> /5EhxuQQo5r+Yvn4rHkOIe2KBJuBHHhNky9f8xRU7j9JnxFvm6tQK8tJ4C3fIklI
> yua+T9thS3XifUNiWTw/WX6CjSYVl2/lFtmzXYZ5+LiZgd4IImEHnXik/jOv/2eC
> ayHhzl8YuA1yxsjULsw=
> =Y3WO
> -----END PGP SIGNATURE-----
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190315/df3d8a48/attachment.htm>


More information about the erlang-questions mailing list