[erlang-questions] Float multiplication behaviours

Serge Aleynikov <>
Tue May 1 16:30:57 CEST 2012


FWIW, if the issue is displaying the property rounded value of the 
float, the float_to_list/2 patch that I submitted awhile back, rounds 
and displays this property (hopefully it'll make it in the release soon):

8> io:fwrite("~p~n", [10.20*100]).
1019.9999999999999
9> float_to_list(10.20*100).
"1.01999999999999988631e+03"
10> float_to_list(10.20*100, []).
"1020.000000"
11> float_to_list(10.20*100, [compact]).
"1020.0"


On 4/24/2012 6:34 AM, Valentin Nechayev wrote:
>   Mon, Apr 23, 2012 at 13:46:14, vinoski wrote about "Re: [erlang-questions] Float multiplication behaviours":
>
>>> ()49>  10.20*100.
>>> 1019.9999999999999
>> http://floating-point-gui.de/
>
> The question is more complex if to compare with different language
> system behaviors and Erlang behavior difference between output formats.
> For example, Python (2.7) shows exactly the same value as Erlang in the
> quote, but C with Glibc (), Perl
> and Ruby's irb shows simply 1020. The latter one shows rounding which
> could be unreasonable, because doesn't differ adjacent values:
>
> $ cat tx1.c
> #include<stdio.h>
> #include<math.h>
> main() {
>    double a = 10.20;
>    a *= 100;
>    double ap, an;
>    ap = nextafter(a, 1000.0);
>    an = nextafter(a, 0.0);
>    printf("%g%s%g%s%g\n",
>        an, (an == a) ? "==" : "!=",
>        a, (a == ap) ? "==" : "!=", ap);
>    return 0;
> }
> $ ./tx1
> 1020!=1020!=1020
>
> If the thread starter expects the value to be rounded in accustomed way, this
> can help in its reading, but can also give confusing results on values which
> are looking equal but differs really. Erlang manner to print with full details
> with ~p is more debug-oriented than user-oriented:
>
> 2>  io:fwrite("~p~n", [10.20*100]).
> 1019.9999999999999
> ok
> 3>  io:fwrite("~g~n", [10.20*100]).
> 1020.00
> ok
>
> (Sadly, math module is quite thin and doesn't carry IEEE required
> functions, as nextafter().)
>
> For more floating-point details, it's better to go to
> <http://randomascii.wordpress.com/2012/04/21/exceptional-floating-point/>
> and start reading from the previous topics.
>
>
> -netch-
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An embedded message was scrubbed...
From: Serge Aleynikov <>
Subject: Re: [erlang-patches] [patch] new float_to_list/2
Date: Sat, 21 Apr 2012 01:19:04 -0400
Size: 12863
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120501/25cae606/attachment.mht>


More information about the erlang-questions mailing list