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