Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Filthy Floats

by Hofmator (Curate)
on Jul 16, 2001 at 20:53 UTC ( [id://97084]=note: print w/replies, xml ) Need Help??


in reply to Re: Filthy Floats
in thread Filthy Floats

Watchout, (s)printf does the rounding itself, it doesn't truncate!

printf "%.3f => %.2f\n", 1.023, 1.023; printf "%.3f => %.2f\n", 1.026, 1.026; #prints 1.023 => 1.02 1.026 => 1.03
but sadly this rounding is buggy on numbers ending in .5:
printf "%.5f => %.2f\n", 1.025, 1.025; # prints instead of the correct 1.03 1.025 => 1.02
A quick and ugly 'fix' is something like printf "%.5f => %.2f\n", 1.025+1e-10, 1.025+1e-10; For better solutions and some discussion see this recent thread.

-- Hofmator

Replies are listed 'Best First'.
Re: Re: Re: Filthy Floats
by scain (Curate) on Jul 16, 2001 at 21:14 UTC
    Hmm... not so sure that is buggy. I think a fairly common usage of round is for numbers that end in with 5 in the last place is to round to the even number. Wierd but true.

    No I don't have a reference, so that is just my recollection.

    Scott

Re: Re: Re: Filthy Floats
by I0 (Priest) on Jul 17, 2001 at 10:56 UTC
    Actually, round to even would be correct for numbers ending in .5
    But in this case, 1.025 really is closer to 1.02
    printf "%.20f => %.2f\n", 1.025, 1.025;

      Actually, round to even would be correct for numbers ending in .5

      This is new to me, up to now I always rounded .5 up. But you seem to be right, e.g. Mathematica rounds in that way. The reason for this is that the rounding should not give a statistical bias - as far as I understand it.

      But in this case, 1.025 really is closer to 1.02 printf "%.20f => %.2f\n", 1.025, 1.025;
      # this prints 1.02499999999999990000 => 1.02
      D'oh, well, been tripped up again by floating point number representations (tye explains that very nicely here in this thread) ...

      What is interesting is the following example where the rounding towards the nearest even number (0) does not take place - and the 0.5 can be represented exactly as a float (as merlyn pointed out somewhere else in this node):

      printf "%.20f => %.0f", 0.5, 0.5; # prints 0.50000000000000000000 => 1

      -- Hofmator

        This is new to me, up to now I always rounded .5 up. But you seem to be right, e.g. Mathematica rounds in that way. The reason for this is that the rounding should not give a statistical bias - as far as I understand it.

        You understand it correctly.

        If your number ends in exactly 5 then you should round it up half the time and down the other half of the time. You could use any handy rule for this - round to even is an easy one. You could also round the first one you come across up, the next one down and so on.

        -- iakobski

        Round to even is best for bases equal to 2 mod 4, e.g. base 2 or base 10
        Round to odd would be best for bases equal to 0 mod 4
        printf "%.20f => %.0f", 0.5, 0.5; # prints 0.50000000000000000000 => 1
        Looks to me like a bug in printf...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://97084]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found