<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><div data-crea="font-wrapper" style="font-family: Tahoma; font-size: 16px; direction: ltr"><div><div style="color: #000000;background-color: #FFFFFF">
Hello Mikael,<br><br><br>
thank you for the explanation!<br>
Of course the reasons for the implementation of list_to_float/1 is due to the erlang requirements.<br>When your mileage varies, you have to walk a bit..<br><br><br>Right now I have implemented my conversion with some regular expressions, as I need this only<br>for reading configuration definitions, which is not performance-critical.<br><br><br>
kind regards,<br>
dieter<br><br><br><div>On 27.04.19 10:30, Mikael Pettersson
wrote:<br></div>
<blockquote type="cite" cite="mid:CAM43=SN4TDRux96OuQ==g+8jvzu06eHXPRW6BtCY0gWwhfkZkw@mail.gmail.com">
<pre wrap="">On Thu, Apr 25, 2019 at 6:04 PM <a href="mailto:dieter@schoen.or.at" target="_blank" tabindex="-1" rel="external" style="cursor: inherit;"><dieter@schoen.or.at></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Hi all,
I am writing a lexer for a SCPI parser. The number format of 488.2 is more tolerant than
list_to_float/1, it seems.
.123 and 456. are both valid in 488.2, but list_to_float/1 rejects them.
I have not yet found an erlang alternative to list_to_float/1,
my current approach is to add the omitted zeroes and feed the patched string to list_to_float/1.
This works if the number is only a mantissa, but
it gets a bit more complicated when there is also an exponent, like 123.e11
my leex expression for the number looks like
{MANTISSA}{EXPONENT} : {token, {float, TokenLine, i488tofloat(TokenChars)}}.
With this approach, I have to parse the string in my function a second time, which is not elegant at all.
Is there a way to have access to the regex macros (or regex groups) on the left side, like \1, \2?
I already tried to push back zeroes, but immediately ran into the promised consequences from note 4 on
<a href="http://erlang.org/doc/man/leex.html" target="_blank" tabindex="-1" rel="external" style="cursor: inherit;">http://erlang.org/doc/man/leex.html</a>, so I quickly left that alley.
The question might be heretic, but why is list_to_float so strict? Every calculator allows to omit a leading 0 before the decimal point.
</pre>
</blockquote>
<pre wrap="">list_to_float is there to convert Erlang-style floating-point numbers,
not anyone else's, and since Erlang has some restrictions(*) on its
floats, that's reflected in this function.
This is normal. C's strtoul() for instance can't convert Erlang-style
Base#Digits numbers.
You have two options: either fix up the string to match Erlang's
syntax before passing it to list_to_integer (which is what you're
already doing) or write your own stand-alone conversion code. I'd
usually do the latter.
(*) Apart from syntax Erlang's floats are restricted to "finite"
values: infinities and NaNs are not permitted anywhere. I don't
approve of this restriction, but it's there.
</pre>
</blockquote>
</div></div></div></body></html>