We don't bite newbies here... much | |
PerlMonks |
Re: Okay! What!?!?!?by davido (Cardinal) |
on Apr 23, 2014 at 06:26 UTC ( [id://1083281]=note: print w/replies, xml ) | Need Help?? |
On my system (Perl5.18.2):
...outputs...
And (GNU C++).....
...outputs...
And (GNU C)
...produces this output:
This is because the decimal value 14.4 cannot be perfectly represented as a fraction in the form of n/(2^m), and therefore has a non-terminating binary expansion. The code I've shown above is first Perl, and second C++, and the third, C, just to illustrate that this is not a problem specific to just Perl. Here's yet another link that attempts to explain it: Re: shocking imprecision Update: Here's a Lisp (Racket, a Scheme derivative) example as well:
...outputs...
(The "#f" means "false"; 14.40 doesn't equal 14.399...) JavaScript (minimal example):
The pop-up displays: "14.399999999999999" Python:
Ruby:
SQL (sqlite):
Couldn't remember enough Pascal. "Go" must optimize away the math, because while it does produce an imprecise representation of 14.4, it produces the same imprecise representation for both 14.4, and 10+14.4-10. (GO):
Update: I left out Java because node lengths at PerlMonks are restricted to 64k (j/k -- I just don't know any Java) Dave
In Section
Seekers of Perl Wisdom
|
|