in reply to Different values while applying format specifiers
It's floating point error. See for example What Every Computer Scientist Should Know About Floating-Point Arithmetic.
$ perl -wMstrict -le 'my $a=5.10*100; print "plain: ",$a, "\nint: ", int($a); printf "%%d: %d\n%%f: %.20f\n",$a,$a;' plain: 510 int: 509 %d: 509 %f: 509.99999999999994315658
One simple built-in way to round numbers in Perl is with sprintf, i.e. something like sprintf("%.0f",$a). For exact math in Perl, some examples are Math::BigInt, Math::BigFloat and Math::BigRat.
|
|---|