[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: printf("%.0f", 0.5) and round-to-even
From: |
Bruno Haible |
Subject: |
Re: printf("%.0f", 0.5) and round-to-even |
Date: |
Tue, 30 Sep 2008 12:43:18 +0200 |
User-agent: |
KMail/1.5.4 |
Hi Eric,
> An interesting question popped up on the bug-gnu-utils list:
> http://lists.gnu.org/archive/html/bug-gnu-utils/2008-09/msg00055.html
>
> POSIX states that with %f, the low-order digit is rounded in an
> implementation-
> defined manner. However, since 0.5 is exactly half-way between 0 and 1, and
> most machines default to IEEE round-to-even, should we make the gnulib printf-
> posix check enforce that printf("%.0f", 0.5) outputs "0" per round-to-even
> rules? If so, then at least cygwin 1.5.x fails this check, by outputting "1".
On one hand, having deterministic round-to-even output is certainly a feature.
On the other hand:
- If POSIX says it is implementation-defined, then programmers should
know that they are relying on behaviour beyond POSIX.
- It requires extra code that most likely not everyone who just wants a
POSIX printf is willing to bear.
I would therefore propose a module fprintf-posix-ext (or fprintf-gnu) that
provides this POSIX behaviour with extensions. And so on for the other
*printf variants.
Does glibc do round-to-even here generally (not just in the one special case
you mentioned?) If so, the module name should be called fprintf-gnu, otherwise
fprintf-posix-ext.
Bruno