panic on floating point division by zero

649 views
Skip to first unread message

Ian Lance Taylor

unread,
Jun 13, 2010, 2:12:06 AM6/13/10
to r...@google.com, golan...@googlegroups.com
Currently 6g/8g panic on a complex division by zero, but do not panic
on a floating point division by zero. I think these should be
consistent. Moreover, since floating point division by zero is
well-defined (non-zero divided by zero yields infinity, zero divided
by zero yields NaN), I think that it should not panic in either case.

We don't want to prevent programmers from using infinity or NaN in
floating point programs, so the only way to panic on floating point
division by zero is to insert an explicit zero check before each
floating point division. We could go that route if we want to, but I
don't think it's appropriate.

Integer division by zero should cause a panic.

Ian

Russ Cox

unread,
Jun 14, 2010, 12:08:18 PM6/14/10
to Ian Lance Taylor, golan...@googlegroups.com
I think we all agree that int division by zero should panic
and that float division by zero should not, so the topic
is really complex division by zero. I found some blog
posts saying that .NET returns NaN,NaN for complex
divide by zero but that GCC returns Inf,Inf. I don't know
what the right answer is. Is there some standard or
some expert we could consult? It seems like it should
not be a panic but I don't know what value it should be.

Russ

Ian Lance Taylor

unread,
Jun 14, 2010, 2:14:07 PM6/14/10
to r...@golang.org, golan...@googlegroups.com
Russ Cox <r...@golang.org> writes:

Annex G in the C99 standard spells out a set of rules for complex
division. It covers everything explicitly except 0+0i / 0+0i; by
implication, and by the code example it gives for how to implement
complex division, the result of that is NaN+NaNi.

Here are the key points:

A complex or imaginary value with at least one infinite part is
regarded as an infinity (even if its other part is a NaN). A
complex or imaginary value is a finite number if each of its parts
is a finite number (neither infinite nor NaN). A complex or
imaginary value is a zero if each of its parts is a zero.

...

For most operand types, the value of the result of a binary
operator with an imaginary or complex operand is completely
determined, with reference to real arithmetic, by the usual
mathematical formula. For some operand types, the usual
mathematical formula is problematic because of its treatment of
infinities and because of undue overflow or underflow; in these
cases the result satisfies certain properties (specified in
G.5.1), but is not completely determined.

...

The / operator satisifies the following infinity properties for
all complex operands:

-- if the first operand is an infinity and the second operand is a
finite number, then the result of the / operator is an infinity;

-- if the first operand is a finite number and the second operand
is an infinity, then the result of the / operator is a zero;

-- if the first operand is a nonzero finite number or an infinity
and the second operand is a zero, then the result of the /
operator is an infinity.

Reply all
Reply to author
Forward
0 new messages