in reply to Floating point problems
Output: 0 2 2 2 2 3 As you can see, it does round toward even. Here is another example with int() that doesn't work the way you think it would:foreach my $i (0.5, 1.5, 1.7, 2.3 ,2.5, 2.7){ printf "%2.0f", $i; }
You would think the above would print 3, but it prints 2. The following works like you expect and prints 3.print int(0.6/0.2);
The short is that there are several methods for rounding, and you really need to know when and where each method is used or you don't use floating point maths to calculate money until displayed for output. Also, you should read "What Every Computer Scientist Should Know About Floating-Point Arithmetic". You can find it by googling. People have different uses for different rounding schemes, again read about rounding on wikipedia. These different needs means that not all functions will round the same. By the way, this is not just a perl problem/difference.print 0.6/0.2;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Floating point problems
by DoctorBinary (Initiate) on Oct 25, 2010 at 13:09 UTC |