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.
In reply to Re: rounding variations after multiplication and division
by ig
in thread [solved] rounding variations after multiplication and division
by DutchCoder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |