[erlang-questions] Bug? Pretty-printing floating point values

Zvi <>
Wed Nov 21 20:29:11 CET 2007


Hi,

at least my C++ compiler (MSVC++) has thease 2 defines in   #include
<float.h>

#define DBL_DIG         15                      /* # of decimal digits of
precision */
#define FLT_DIG         6                       /* # of decimal digits of
precision */

Zvi


Per Hedeland wrote:
> 
> "Christian S" <> wrote:
>>
>>Doesn't look like the number of significant digits when read are stored.
> 
> Undoubtedly true, but not necessarily relevant.
> 
>>I wouldn't call it a bug. It is how floating point numbers work.
>>They're very exact with fractions that are a sum of limited numbers of
>>negative powers of two though.
> 
> A reasonable question could be why float_to_list/1 generates 20 digits
> when a 64-bit float (a.k.a. C double), which is what is used internally,
> only can hold 15-16 worth of them - I don't know off-hand what a 128-bit
> float would have, but presumably significantly more than 20, so it's not
> that either. I guess way back in the dark ages, someone thought that 20
> was a nice and even number (I hope it wasn't me:-). The 6.30000 form is
> of course just the ~p/~w formatting.
> 
> --Per Hedeland
> 
> Eshell V5.5.5  (abort with ^G)
> 1> F=6.30000000000000000000000000000.
> 6.30000
> 2> float_to_list(F).
> "6.29999999999999982236e+00"
> 3> io:format("~p~n",[F]).
> 6.30000
> ok
> 4> io:format("~.16g~n",[F]).
> 6.300000000000000
> ok
> 5> io:format("~.17g~n",[F]).
> 6.2999999999999998
> ok
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 
> 

-- 
View this message in context: http://www.nabble.com/Bug--Pretty-printing-floating-point-values-tf4850658.html#a13884355
Sent from the Erlang Questions mailing list archive at Nabble.com.




More information about the erlang-questions mailing list