in reply to Re^2: How to calculate the sum of columns to be equal to 100?
in thread How to calculate the sum of columns to be equal to 100?

Thanks Laurent_R, this is useful information I hadn’t come across before. Is it documented anywhere? I don’t see anything in sprintf or the Camel Book, and the latest C Standard doesn’t seem to cover it either?

I actually once had to write a special rounding module just because my client considered the above to be simply wrong and wanted 2.5 to be rounded to 3.

Simply adding a minimum field width to the format seems to do the trick:

1:03 >perl -wE "printf qq[%9f\t%.0f\t%2.0f\n], ($_ + 0.5) x 3 for 0 . +. 10;" 0.500000 0 1 1.500000 2 2 2.500000 2 3 3.500000 4 4 4.500000 4 5 5.500000 6 6 6.500000 6 7 7.500000 8 8 8.500000 8 9 9.500000 10 10 10.500000 10 11 1:03 >perl -v This is perl 5, version 18, subversion 0 (v5.18.0) built for MSWin32-x +86-multi-thread-64int

It should perhaps also be pointed out that where the internal representation of a floating point number is inexact, all bets are off:

1:29 >perl -wE "printf qq[%21.18f\t%.1f\t%4.1f\n], ($_ + 0.05) x 3 fo +r 0 .. 10;" 0.050000000000000003 0.1 0.1 1.050000000000000000 1.1 1.1 2.049999999999999800 2.0 2.0 3.049999999999999800 3.0 3.0 4.049999999999999800 4.0 4.0 5.049999999999999800 5.0 5.0 6.049999999999999800 6.0 6.0 7.049999999999999800 7.0 7.0 8.050000000000000700 8.1 8.1 9.050000000000000700 9.1 9.1 10.050000000000001000 10.1 10.1 1:29 >

Cheers,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: How to calculate the sum of columns to be equal to 100?
by Laurent_R (Canon) on Aug 15, 2013 at 18:53 UTC
Re^4: How to calculate the sum of columns to be equal to 100?
by Laurent_R (Canon) on Aug 15, 2013 at 18:03 UTC

    I read it originally in the documentation of the standard C library. You can certainly find the IEEE standard on this.

    Strangely, your "trick" with the format does not work for me on 5.14.2:

    $ perl -wE 'printf qq[%9f\t%.0f\t%2.0f\n], ($_ + 0.5) x 3 for 0 .. 10; +' 0.500000 0 0 1.500000 2 2 2.500000 2 2 3.500000 4 4 4.500000 4 4 5.500000 6 6 6.500000 6 6 7.500000 8 8 8.500000 8 8 9.500000 10 10 10.500000 10 10

      The trick works for me on DWIM Perl 5.14.2:

      11:55 >perl -wE "printf qq[%9f\t%.0f\t%2.0f\n], ($_ + 0.5) x 3 for 0 . +. 10;" 0.500000 0 1 1.500000 2 2 2.500000 2 3 3.500000 4 4 4.500000 4 5 5.500000 6 6 6.500000 6 7 7.500000 8 8 8.500000 8 9 9.500000 10 10 10.500000 10 11 11:55 >perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for MSWin32-x +86-multi-thread

      Likewise on my 3 Strawberry Perls:

      • This is perl, v5.10.1 (*) built for MSWin32-x86-multi-thread
      • This is perl 5, version 16, subversion 0 (v5.16.0) built for MSWin32-x86-multi-thread-64int
      • This is perl 5, version 18, subversion 0 (v5.18.0) built for MSWin32-x86-multi-thread-64int

      I’m running under Windows Vista 32-bit. So it seems that Perl’s printf behaves differently under Windows. No mention of this in perlport or perlwin32, though. Strange.

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,