[A complimentary Cc of this posting was sent to
P
D:\>perl -e "printf '%.5f', 37.111115"
37.11111
D:\>perl -e "printf '%.6f', 37.1111115"
37.111112
D:\>perl -e "printf '%.7f', 37.11111115"
37.1111111
D:\>perl -e "printf '%.8f', 37.111111115"
37.11111112
This means that the compiler used to build Perl is extremely buggy (up
to being unusable for anything which requires correct arithmetic).
Require a refund.
The first thing to check should be
perl -e "printf '%.18f', 37.111111115"
37.111111114999999927
(after .18f different CRTL I have start to produce different results).
Note that 37.111111115 * 2**(53-4) is 20891698273602384.04108288
(exactly), and 20891698273602384./2**(53-4) is
37.111111114999999927022145129740238189697265625 (exactly). So THIS
should be the number rounded by printf() when the compiler uses 64-bit
IEEE FP numbers.
Actually, Sun's cc (Sun WorkShop 6 update 2 C 5.3 Patch 111679-09
2002/11/12) gives this result with
perl -e "printf '%.49f', 37.111111115"
this is pretty amazing; I would not expect the C standard requires so
much precision from printf(); does it? But about .18f should be
expected to provide correct results.
On two systems (with different vendors' CRTL) I get
~->perl -e 'printf qq(%.${_}f\n), q(37.) . 1 x $_ . 5 for 1..14'
37.1
37.12
37.111
37.1112
37.11111
37.111111
37.1111111
37.11111111
37.111111112
37.1111111112
37.11111111112
37.111111111111
37.1111111111111
37.11111111111111
Hope this helps,
Ilya