convert strings like "1e-8" to floats.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

convert strings like "1e-8" to floats.

Oleksii Semilietov
Hi all!

I playing with converting binaries and lists to floats and I found something what I can't solve via elegant way. I believe it's my leakage of understanding floats.

So, what is different between 1e-8 and 1.0e-8 ?

For example we have float as list "0.01".  It is easy to convert it to float just via list_to_float("0.01").
But when floats coming from external world as binaries or lists which looks like "1e-8", and not like "1.0e-8", I can't find proper way to convert it. 

For example in PHP both 1e-8 and 1.0e-8 is equivalent
var_dump(floatval("1e-8") === floatval("1.0e-8")) returns true.
Javascript 1E-8 === 1.0E-8 is also true.

In Erlang

list_to_float("1.0E-8") =:= 0.00000001
true

but

list_to_float("1E-8") =:= 0.00000001.
** exception error: bad argument in function list_to_float/1 called as list_to_float("1E-8") 


How to convert it via right way?

--
Oleksii D. Semilietov

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: convert strings like "1e-8" to floats.

Per Hedeland
On 2017-02-07 10:54, Oleksii Semilietov wrote:
>
> I playing with converting binaries and lists to floats and I found
> something what I can't solve via elegant way. I believe it's my leakage of
> understanding floats.

No, I think it's just that Erlang is a bit more, uh, pedantic than other
languages about what the "good representation" of a float is.

> But when floats coming from external world as binaries or lists which looks
> like "1e-8", and not like "1.0e-8", I can't find proper way to convert it.

I don't know if there is a "proper" way, but something like this should
do the trick:

tolerant_list_to_float(L) ->
    case re:run(L, "^([+-]?\\d+)([Ee]\\d+)?$", [{capture, [1, 2], list}]) of
        {match, [M, E]} ->
            list_to_float(M ++ ".0" ++ E);
        _ ->
            list_to_float(L)
    end.

--Per
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: convert strings like "1e-8" to floats.

Mikael Pettersson-5
In reply to this post by Oleksii Semilietov
Oleksii Semilietov writes:
 > Hi all!
 >
 > I playing with converting binaries and lists to floats and I found
 > something what I can't solve via elegant way. I believe it's my leakage of
 > understanding floats.
 >
 > So, what is different between 1e-8 and 1.0e-8 ?
 >
 > For example we have float as list "0.01".  It is easy to convert it to
 > float just via list_to_float("0.01").
 > But when floats coming from external world as binaries or lists which looks
 > like "1e-8", and not like "1.0e-8", I can't find proper way to convert it.
 >
 > For example in PHP both 1e-8 and 1.0e-8 is equivalent
 > var_dump(floatval("1e-8") === floatval("1.0e-8")) returns true.
 > Javascript 1E-8 === 1.0E-8 is also true.
 >
 > In Erlang
 >
 > list_to_float("1.0E-8") =:= 0.00000001
 > true
 >
 > but
 >
 > list_to_float("1E-8") =:= 0.00000001.
 > ** exception error: bad argument in function list_to_float/1 called as
 > list_to_float("1E-8")
 >
 >
 > How to convert it via right way?

Write your own conversion routine.

Your mistake is assuming literals like floats have the same look and feel
regardless of language or environment.  That's in general not true.

You may also consider the different notation for integers with non-decimal
base, e.g. 0xabba vs 16#abba, or character literals, etc.

What it means is that a tool written in one language but processing source
code for another, must be prepared to do all conversions on its own without
relying on ready-made services in the implementation language.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: convert strings like "1e-8" to floats.

Jesper Louis Andersen-2
Another example is OCaml, which supports entering integers as

# 1_000_000;;
- : int = 1000000

which is very nifty, but unique to OCaml and thus not too portable.

On Wed, Feb 8, 2017 at 12:16 PM Mikael Pettersson <[hidden email]> wrote:
Oleksii Semilietov writes:
 > Hi all!
 >
 > I playing with converting binaries and lists to floats and I found
 > something what I can't solve via elegant way. I believe it's my leakage of
 > understanding floats.
 >
 > So, what is different between 1e-8 and 1.0e-8 ?
 >
 > For example we have float as list "0.01".  It is easy to convert it to
 > float just via list_to_float("0.01").
 > But when floats coming from external world as binaries or lists which looks
 > like "1e-8", and not like "1.0e-8", I can't find proper way to convert it.
 >
 > For example in PHP both 1e-8 and 1.0e-8 is equivalent
 > var_dump(floatval("1e-8") === floatval("1.0e-8")) returns true.
 > Javascript 1E-8 === 1.0E-8 is also true.
 >
 > In Erlang
 >
 > list_to_float("1.0E-8") =:= 0.00000001
 > true
 >
 > but
 >
 > list_to_float("1E-8") =:= 0.00000001.
 > ** exception error: bad argument in function list_to_float/1 called as
 > list_to_float("1E-8")
 >
 >
 > How to convert it via right way?

Write your own conversion routine.

Your mistake is assuming literals like floats have the same look and feel
regardless of language or environment.  That's in general not true.

You may also consider the different notation for integers with non-decimal
base, e.g. 0xabba vs 16#abba, or character literals, etc.

What it means is that a tool written in one language but processing source
code for another, must be prepared to do all conversions on its own without
relying on ready-made services in the implementation language.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: convert strings like "1e-8" to floats.

Richard Carlsson-3
Both the NNN_NNN and the Base#NNN notation go back to Ada, so they are not particular either for OCaml or Erlang, respectively.


        /Richard

2017-02-08 16:35 GMT+01:00 Jesper Louis Andersen <[hidden email]>:
Another example is OCaml, which supports entering integers as

# 1_000_000;;
- : int = 1000000

which is very nifty, but unique to OCaml and thus not too portable.

On Wed, Feb 8, 2017 at 12:16 PM Mikael Pettersson <[hidden email]> wrote:
Oleksii Semilietov writes:
 > Hi all!
 >
 > I playing with converting binaries and lists to floats and I found
 > something what I can't solve via elegant way. I believe it's my leakage of
 > understanding floats.
 >
 > So, what is different between 1e-8 and 1.0e-8 ?
 >
 > For example we have float as list "0.01".  It is easy to convert it to
 > float just via list_to_float("0.01").
 > But when floats coming from external world as binaries or lists which looks
 > like "1e-8", and not like "1.0e-8", I can't find proper way to convert it.
 >
 > For example in PHP both 1e-8 and 1.0e-8 is equivalent
 > var_dump(floatval("1e-8") === floatval("1.0e-8")) returns true.
 > Javascript 1E-8 === 1.0E-8 is also true.
 >
 > In Erlang
 >
 > list_to_float("1.0E-8") =:= 0.00000001
 > true
 >
 > but
 >
 > list_to_float("1E-8") =:= 0.00000001.
 > ** exception error: bad argument in function list_to_float/1 called as
 > list_to_float("1E-8")
 >
 >
 > How to convert it via right way?

Write your own conversion routine.

Your mistake is assuming literals like floats have the same look and feel
regardless of language or environment.  That's in general not true.

You may also consider the different notation for integers with non-decimal
base, e.g. 0xabba vs 16#abba, or character literals, etc.

What it means is that a tool written in one language but processing source
code for another, must be prepared to do all conversions on its own without
relying on ready-made services in the implementation language.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: convert strings like "1e-8" to floats.

Richard A. O'Keefe-2
In reply to this post by Mikael Pettersson-5


On 9/02/17 12:16 AM, Mikael Pettersson wrote:
> Write your own conversion routine.
>
> Your mistake is assuming literals like floats have the same look and feel
> regardless of language or environment.  That's in general not true.

This is spot on, but I suggest *transliteration* from the target syntax
to Erlang literal syntax followed by using list_to_float/1 to do the
actual *conversion*.  Transliterating from one syntax to another is
just straightforward text processing, e.g., turn ".2D7" into "0.2e7".
Converting *accurately* is somewhere past "difficult" and well into
"arcane".



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: convert strings like "1e-8" to floats.

Richard A. O'Keefe-2
In reply to this post by Jesper Louis Andersen-2


On 9/02/17 4:35 AM, Jesper Louis Andersen wrote:
> Another example is OCaml, which supports entering integers as
>
> # 1_000_000;;
> - : int = 1000000
>
> which is very nifty, but unique to OCaml and thus not too portable.

*Not* unique to OCaml.  Allowed by Ada and ISO Pascal Extended and
Quintus Prolog and Eiffel and F# and >>> Java <<< amongst others.

Fortran (pre Fortran 95) and Algol (60 and 68) allowed embedded
spaces in number literals.  _ is a little safer...
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions