[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Why do some math functions return -0
- From: Tony Finch <dot@...>
- Date: Mon, 20 Sep 2010 09:03:31 +0100
On 20 Sep 2010, at 08:29, David Kastrup <email@example.com> wrote:
> And you did not realize that in all the presented code, there was _no_
> way that would _ever_ generate -0 unless there was a processing error
It can happen if the rounding mode is towards negative infinity. IEEE 754 says:
6.3. The Sign Bit
This standard does not interpret the sign of an NaN. Otherwise, the sign of a product or quotient is the exclusive or of the operands' signs; the sign of a sum, or of a difference x–y regarded as a sum x+ (–y), differs from at most one of the addends' signs, and the sign of the result of the round floating-point number to integral value operation is the sign of the operand. These rules shall apply even when operands or results are zero or infinite.
When the sum of two operands with opposite signs (or the difference of two operands with like signs) is exactly zero, the sign of that sum (or difference) shall be + in all rounding modes except round toward –INFINITY, in which mode that sign shall be –. However, x+x = x–(–x) retains the same sign as x even when x is zero.
Except that sqrt(–0) shall be –0, every valid square root shall have a positive sign.
f.anthony.n.finch <firstname.lastname@example.org> http://dotat.at/