[Haskell-cafe] 1/0

Richard A. O'Keefe ok at cs.otago.ac.nz
Mon Jun 16 21:54:46 EDT 2008


On 17 Jun 2008, at 11:07 am, Evan Laforge wrote:

> So, I know this has been discussed before, but:
>
>> 1/0
> Infinity
>> 0/0
> NaN
>
> ... so I see from the archives that Infinity is mandated by ieee754
> even though my intuition says both should be NaN.

Other people have other intuitions.  It may be that your intuition
is telling you that neither result should be an ordinary number,
and if that's what it's really telling you, it's right: the C
function isfinite(x) is true of all signed zero, subnormal, or
normal x, false of signed infinities and NaNs.

 > Every other language throws an exception, even C will crash the
> program,

Not true.  C99 *definitely* allows both infinities and NaNs (see
Annex F of the C99 standard) and C89 practice also allowed it.
Some C89 systems required you to use signal() with SIGFPE to
turn IEEE extended results into exceptions; others required you
to use signal() to disable this; others used yet other means.

The Scheme systems I tried turn (/ 1.0 0.0) into a printed
representation of IEEE infinity.  Of the Prolog systems I checked,
some did and some didn't.  The Standard ML system I tried gave
"inf" as the response to 1.0/0.0.

Basically, with any programming language implementation that
truthfully claims conformance to IEEE 754 or a successor standard,
x/0 MUST NOT crash unless your program explicitly asks for such
behaviour.  As for programming language implementations that do
not make such a claim, who knows what they will do?

Since integers do not have the special IEEE values, conversion
of IEEE values to integral values really ought to be checked.
(Of course, in C what you typically get is garbage, but that can
be put more generally...)




More information about the Haskell-Cafe mailing list