<div dir="ltr">Just to stack up some more languages:<div><br></div><div><div>$ perl -E'say 2452.45*100'</div><div>245245</div><div>$ perl -E'say 2452.45*100 - 245245'</div><div>-2.91038304567337e-11</div></div>
<div><br></div><div>ocaml</div><div><br></div><div><div># let a = 2452.45 *. 100.0;;</div><div>val a : float = 245244.999999999971</div></div><div><br></div><div>But dc computes precise</div><div><br></div><div><div>$ dc <<<"20k2452.45 100*245245-p"</div>
<div>0</div></div><div><br></div><div><div>$ dc <<<"20k28 85*312*5*0.85/p"</div><div>4368000.00000000000000000000</div></div><div><br></div><div>H.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Wed, Nov 27, 2013 at 4:20 AM, Richard A. O'Keefe <span dir="ltr"><<a href="mailto:ok@cs.otago.ac.nz" target="_blank">ok@cs.otago.ac.nz</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im"><br>
On 27/11/2013, at 2:28 PM, Sanath Prasanna wrote:<br>
<br>
> Hi all,<br>
> when running following sample in ERL shell, I got wrong value. What is the reason for this?<br>
><br>
> 2452.45*100. = 245244.99999999997<br>
><br>
> It should be 245245.0 or 245245<br>
><br>
<br>
</div>What do you mean "should"?<br>
Some programming languages lie to you about the number they give you.<br>
Ruby for one.<br>
You have to probe a little deeper.<br>
<br>
m% irb<br>
>> 2452.45*100<br>
=> 245245.0<br>
>> 2452.45*100 - 245245.0<br>
=> -2.91038304567337e-11<br>
<br>
So Ruby _actually_ calculated the *same* answer as Erlang,<br>
but showed you a different number.<br>
<br>
SWI Prolog calculated the same number as Erlang.<br>
<br>
m% swipl<br>
?- X is 2452.45*100.<br>
X = 245244.99999999997.<br>
<br>
Gambit Scheme calculated the same number as Erlang.<br>
<br>
m% gsi<br>
Gambit v4.6.0<br>
> (* 2452.45 100)<br>
245244.99999999997<br>
<br>
R calculates the same number as Erlang, but like Ruby,<br>
rounds the number before displaying it:<br>
<br>
m% R<br>
> x <- 2452.45 * 100<br>
> x<br>
[1] 245245<br>
> x - 245245<br>
[1] -2.910383e-11<br>
<br>
Javascript calculates the same number as Erlang.<br>
<br>
m% /Scratch/js-1.6.20070208/js<br>
js> 2452.45*100<br>
245244.99999999997<br>
<br>
I think we're beginning to see a pattern.<br>
<br>
m% cat >foo.c<br>
#include <stdio.h><br>
<br>
int main(void) {<br>
    printf("%.12f\n", 2452.45*100);<br>
    return 0;<br>
}<br>
<EOF><br>
m% cc foo.c<br>
m% a.out<br>
245244.999999999971<br>
<br>
It looks very much as though Erlang calculates exactly the<br>
answer that it "should" calculate.<br>
<br>
Remember, this is IEEE double-precision *binary* floating-<br>
point arithmetic.  100 can be represented exactly in that<br>
arithmetic, but 2452.45 *cannot*.  From a C program, when<br>
you convert 2452.45 to an IEEE double, what you get is<br>
2452.44999999999981810106.<br>
<br>
There _is_ an IEEE standard for decimal floating point<br>
arithmetic, and recent IBM z/Series and Power computers<br>
offer high speed hardware support for it, and there _is_<br>
at least a draft of a standard for decimal floating point<br>
in C, which IBM's XLC supports, GCC supports (see<br>
<a href="http://gcc.gnu.org/onlinedocs/gcc/Decimal-Float.html" target="_blank">http://gcc.gnu.org/onlinedocs/gcc/Decimal-Float.html</a>),<br>
but clang doesn't.<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br></div>