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

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

Replies are listed 'Best First'.
Re: Re4: Filthy Floats
by iakobski (Pilgrim) on Jul 17, 2001 at 15:34 UTC
    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

Re: Re4: Filthy Floats
by I0 (Priest) on Jul 17, 2001 at 22:16 UTC
    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...