[erlang-questions] Maths Problem -> 2452.45*100. = 245244.99999999997
Richard A. O'Keefe
<>
Wed Nov 27 04:20:06 CET 2013
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.
More information about the erlang-questions
mailing list