in reply to Rounding With sprintf anomaly?

What HW/OS/Perl version are you on ? On my Activestate 631 both variations in your example is rounded up.

What I'm thinking of is that a floating point value not always kan be represented exactly and that your first and second case might be represented as 4.499999999999999..... in your box, which would explain the rounddown.

You could look into the Posix routines for a possibly alternate route to achieve your aim.
---
I would like to change the world but God won't let me have the source code.

Replies are listed 'Best First'.
Re: Re: Rounding With sprintf anomaly?
by oakbox (Chaplain) on Feb 19, 2002 at 09:57 UTC
    Intel chip, Linux (cobalt's hacked Redhat version), and Perl 5.6.1. POSIX 'floor' and 'ceiling' are mentioned in a couple of posts elsewhere on perlmonks. I'll look into those.

    I think the suggestion that the 5 isn't really a '5' holds the most water. There are actually about 28 separate calculations being performed in that section and it is only in THAT PARTICULAR section that the round doesn't perform as expected.

    Okay, I'm happy again. I just wanted an explanation for the (apparently) freaky behaviour.

    -oakbox

      I think if you want a proper rounding, you do this:

      $x=int($num+0.5);

      This gets rid of all those oddities, like a number looking like 5.5, but actually being 4.9999999....

      Sorry, typo, should be 5.499999...