in reply to [solved] rounding variations after multiplication and division
See also What Every Computer Scientist Should Know About Floating Point. And consider the following variation of your program:
use strict; my $ammount; my $value; my $response = qq{Value should be 189.81 (rounded to two decimals)} . qq{ and sprintf gives us: %.24f \n}; $ammount = 159.5; $value = $ammount * 1.19; print sprintf($response, $value); $ammount = 159.5; $value = ($ammount*119)/100; print sprintf($response, $value);
Which produces
Value should be 189.81 (rounded to two decimals) and sprintf gives us: + 189.804999999999978399500833 Value should be 189.81 (rounded to two decimals) and sprintf gives us: + 189.805000000000006821210263
You can clearly see from this output why sprintf rounds the two values differently.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: rounding variations after multiplication and division
by DutchCoder (Scribe) on Aug 06, 2009 at 12:46 UTC |