This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: printf("%0.0f", 0.5) yields unexpected result (0)
- To: "Nathan Sidwell" <nathan at codesourcery dot com>, "Mo McKinlay" <mmckinlay at gnu dot org>
- Subject: Re: printf("%0.0f", 0.5) yields unexpected result (0)
- From: "Tim Prince" <tprince at computer dot org>
- Date: Fri, 18 Aug 2000 10:15:06 -0700
- Cc: "Horst von Brand" <vonbrand at inf dot utfsm dot cl>, "Marc Boeren" <M dot Boeren at guidance dot nl>, <gcc-bugs at gcc dot gnu dot org>
- References: <Pine.LNX.4.21.0008161621160.3235-100000@sphere.man.uk.ekto.org> <399C0E5E.61A2AD6D@codesourcery.com>
I'm no accountant, but I believe there was an accounting principle in
certain specialties, long before IEEE P754 or even computerization,
which specified this rounding method. Whether that was the source of
the idea, I don't know. I can imagine that there may have been billing
situations where it was fairer to both parties to remove the bias on
these round-offs, which could have been quite common in manual
calculations.
Tim Prince
----- Original Message -----
From: "Nathan Sidwell" <nathan@codesourcery.com>
To: "Mo McKinlay" <mmckinlay@gnu.org>
Cc: "Horst von Brand" <vonbrand@inf.utfsm.cl>; "Marc Boeren"
<M.Boeren@guidance.nl>; <gcc-bugs@gcc.gnu.org>
Sent: Thursday, August 17, 2000 9:10 AM
Subject: Re: printf("%0.0f", 0.5) yields unexpected result (0)
> Mo McKinlay wrote:
> >
> > # Round to next even digit if .5, what's strange about that?
> >
> > I don't know what school you guys went to, but I was always taught
to
> > round up to the next number, not the next *even* number. I may be
being
> > incredibly stupid, but where on earth did the ISO committee get this
> > behaviour from? I've never heard of next-even-number-rounding before
in my
> > life :)
> Ah, yes round 1/2 up is OK for real numbers, but floating point
numbers
> are not real numbers. As such, if you rounded up you'd introduce a
bias.
> Strange as it may seem round to nearest even is better for floats.
>
> Quick example. Suppose the exact results were evenly distributed over
the
> set of half integers and the representable results were in the set of
> integers. Now rounding the 1/2 values up would introduce an average
> systematic bias of +1/4 rounding towards nearest even gives an average
> bias of zero.
>
> nathan
> --
> Dr Nathan Sidwell :: http://www.codesourcery.com ::
CodeSourcery LLC
> 'But that's a lie.' - 'Yes it is. What's your point?'
> nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ :
nathan@acm.org