Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Re: Filthy Floats

by I0 (Priest)
on Jul 17, 2001 at 10:56 UTC ( [id://97260]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Filthy Floats
in thread Filthy Floats

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;

Replies are listed 'Best First'.
Re4: Filthy Floats
by Hofmator (Curate) on Jul 17, 2001 at 14:13 UTC

    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://97260]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-18 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found