sprintf rounding with FreeBSD and perl 5.8.x

Discussion in 'Perl Misc' started by cherbst@gmail.com, Jan 24, 2008.

  1. Guest

    I'm getting a weird result with some rounding in a sprintf with perl
    5.8.8:

    c = 2.47 / 100; # => 0.0247
    sprintf("%.1f", 18500 * c); # => 457.0 (NOT on FreeBSD)
    sprintf("%.1f", 18500 * 0.0247); # => 456.9

    FreeBSD gives 456.9 for both sprintfs, I was able to reproduce it on a
    fresh FreeBSD 6.3 install with the perl binary package, and from a
    compiled binary.

    On Linux perl 5.8.8 gives 457.0, and I get that answer on everything
    else I tried (ruby and python on Linux and FreeBSD). I tried using
    each of the methods Math::Round, but that is different from sprintf on
    Linux, and that is the behavior I was trying to get. Is anybody aware
    of something like an unusual CFLAG that might be getting passed when
    perl is compiled on FreeBSD?
    , Jan 24, 2008
    #1
    1. Advertising

  2. J. Gleixner Guest

    wrote:
    > I'm getting a weird result with some rounding in a sprintf with perl
    > 5.8.8:
    >
    > c = 2.47 / 100; # => 0.0247
    > sprintf("%.1f", 18500 * c); # => 457.0 (NOT on FreeBSD)
    > sprintf("%.1f", 18500 * 0.0247); # => 456.9


    That's not valid perl, so I have no idea how you're getting a result.
    J. Gleixner, Jan 24, 2008
    #2
    1. Advertising

  3. Guest

    On Jan 24, 12:03 pm, "J. Gleixner" <glex_no-s...@qwest-spam-
    no.invalid> wrote:
    > That's not valid perl, so I have no idea how you're getting a result.


    I had omitted the sigils, runs OK otherwise:

    ---

    #!/usr/bin/perl -w

    use strict;

    my $c = 2.47 / 100; # => 0.0247
    printf("%.1f\n", 18500 * $c); # => 457.0 (NOT on FreeBSD)
    printf("%.1f\n", 18500 * 0.0247); # => 456.9

    ---
    , Jan 24, 2008
    #3
  4. wrote:
    > I'm getting a weird result with some rounding in a sprintf with perl
    > 5.8.8:
    >
    > c = 2.47 / 100; # => 0.0247


    Odd comment! What do you mean "0.0247"?

    $c = 2.47 / 100;
    does not produce the result 0.0247, since 2.47 and .0247 aren't
    representable in finite binary digits:

    $ perl -e 'printf("%.20f\n%.20f\n", 2.47, 0.0247);'
    2.47000000000000019540
    0.02469999999999999973

    hence ...

    $ perl -e 'printf("%.20f\n%.20f\n", 2.47 / 100, 0.0247);'
    0.02470000000000000320
    0.02469999999999999973


    > sprintf("%.1f", 18500 * c); # => 457.0 (NOT on FreeBSD)
    > sprintf("%.1f", 18500 * 0.0247); # => 456.9


    $ perl -e 'printf("%.20f\n%.20f\n", 18500*(2.47/100), 18500*0.0247);'
    456.95000000000004547474
    456.94999999999998863132

    $ perl -e 'printf("%.1f\n%.1f\n", 18500*(2.47/100), 18500*0.0247);'
    457.0
    456.9


    > FreeBSD gives 456.9 for both sprintfs, I was able to reproduce it on a
    > fresh FreeBSD 6.3 install with the perl binary package, and from a
    > compiled binary.


    Maybe printing the intermediate results to more digits (as above) would
    shed some light?

    >
    > On Linux perl 5.8.8 gives 457.0, and I get that answer on everything
    > else I tried (ruby and python on Linux and FreeBSD). I tried using
    > each of the methods Math::Round, but that is different from sprintf on
    > Linux, and that is the behavior I was trying to get. Is anybody aware
    > of something like an unusual CFLAG that might be getting passed when
    > perl is compiled on FreeBSD?


    Not I, but I'd guess there is something interesting to see in
    perl -V
    on your BSD and Linux systems.
    RedGrittyBrick, Jan 24, 2008
    #4
  5. Guest

    On Jan 24, 12:39 pm, RedGrittyBrick <>
    wrote:
    > wrote:
    > > I'm getting a weird result with some rounding in a sprintf with perl
    > > 5.8.8:

    >
    > > c = 2.47 / 100; # => 0.0247

    >
    > Odd comment! What do you mean "0.0247"?
    >
    > $c = 2.47 / 100;
    > does not produce the result 0.0247, since 2.47 and .0247 aren't
    > representable in finite binary digits:


    Yes, I took a crash course in floating point problems over the last
    few days. All I'm really trying to get at is why perl on FreeBSD is
    different from everything else (ruby and python on Linux and FreeBSD).

    > Not I, but I'd guess there is something interesting to see in
    > perl -V
    > on your BSD and Linux systems.


    They *are* different, I hadn't used -V before:

    Linux:
    Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT
    PERL_MALLOC_WRAP THREADS_HAVE_PIDS
    USE_ITHREADS
    USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API
    FreeBSD:
    Compile-time options: MYMALLOC PERL_MALLOC_WRAP USE_64_BIT_INT
    USE_LARGE_FILES USE_PERLIO

    USE_64_BIT_INT looks interesting, still trying to hunt down where it's
    set (not Makefile, not config.sh...)
    , Jan 24, 2008
    #5
  6. [A complimentary Cc of this posting was sent to

    <>], who wrote in article <>:
    > USE_64_BIT_INT looks interesting, still trying to hunt down where it's
    > set (not Makefile, not config.sh...)


    Assuming IEEE complience (should be taken as given), only NVsize
    should matter. See the beginning of the thread on milliseconds for
    details.

    Hope this helps,
    Ilya
    Ilya Zakharevich, Jan 24, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Blnukem
    Replies:
    9
    Views:
    165
  2. Mike Hunter
    Replies:
    0
    Views:
    112
    Mike Hunter
    Oct 2, 2003
  3. Paul Southworth
    Replies:
    4
    Views:
    116
    Anno Siegel
    Feb 23, 2004
  4. Robert TV

    Rounding numbers with sprintf

    Robert TV, Jun 12, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    237
    Gunnar Hjalmarsson
    Jun 12, 2004
  5. Jim Pazarena

    perl and GDBM and FreeBSD

    Jim Pazarena, Feb 8, 2005, in forum: Perl Misc
    Replies:
    0
    Views:
    197
    Jim Pazarena
    Feb 8, 2005
Loading...

Share This Page