Re: Division by zero: float vs. int

From: Chris Uppal (chris.uppal_at_metagnostic.REMOVE-THIS.org)
Date: 09/29/04


Date: Wed, 29 Sep 2004 08:26:37 +0100

Tor Iver Wilhelmsen wrote:

> > sqrt(-10) ==should== sqrt(-20)
>
> No; For instance sqrt(-9) = 3i and sqrt(-100) = 10i; both are existing
> (complex) numbers, they simply cannot be represented as reals.

I say yes, i.e. I agree with Thomas.

The NaN result is saying "this calculation has no defined result", or -- if you
prefer to invoke the complex plane -- "this calculation has two values neither
of which can be approximated as floating point". I prefer the first
formulation, not least because we are /not/ working in the complex plane[*].

([*] Remember that the complex plane is not the only mathematical abstraction
that can be considered to embed the real line, and in which sqrt() is defined
for all real values)

So the NaN result is a meta-level assertion shoe-horned into IEEE FP
representation. It would obviously be better -- in general -- to throw an
exception (perhaps a resumable one), but IEEE doesn't have language-specific
concepts like that (AFAIK), so all it can do is represent the meta-linguistic
assertion as a special reserved bit pattern.

There are only a handfull of such meta-lingistic concepts needed to cover the
failure modes of fp calculations. I presume that IEEE have done their
homework, and have covered all the bases, and so that all the meaningfull
assertions of the form "this calculation has no result because..." have been
given NaN values or similar.

Now, I don't see any semantic utility in distinguishing between "sqrt(-1) has
no defined result" and "sqrt(-18) has no defined value" -- in fact I think they
are the /same/ assertion (especially since IEEE NaN does not preserve the
expression that could not be computed), i.e. I think that "this calculation has
no defined result" /should/ be treated as equal to "this calculation has no
defined result" -- indeed I'd say that it's /obvious/ that they should be
equal.

That isn't to say that I'd want to make all the different kinds of NaN equal.
And it may be that that is the motivation behind making none of them equal, not
even to themselves. Since Java has no clear concept of the different kinds of
NaN as first-class primitive values (e.g there are no float/double literals for
them).

Alternatively, it may be -- as a pragmatic matter -- that generations of
experience in "real" FP work by numerical programmers have found that the
semantically impure device of treating all NaNs as unequal results in simpler,
clearer, or faster expression of important caclulations, and that Java is just
following that. It sounds plausible to me, but I don't know (I'm /not/ a
numerical programmer -- I'm pleased to say ;-)

    -- chris



Relevant Pages

  • Re: Discussion of why java.lang.Number does not implement Comparable
    ... NaN is produced, and if not, you want NaN to have well-defined (if ... Please don't try to change the playing field out from under the ... embedded into the reals, ... Suppose you have the positive reals and compare them via integer part, ...
    (comp.lang.java.programmer)
  • Re: Calculus XOR Probability
    ... For finite sets, you COUNT the objects, ... discuss the mathematical structure of the values including NaN under ... number you shout from the ditty, can it, since ... Yes, perhaps it is, but it isn't the reals (not the real reals, ...
    (sci.math)
  • Re: detect Inf or NaN
    ... The Fortran standard doesn't, but I believe that IEEE 754 does. ... even if the operands' formats differ. ... Every NaN shall compare unordered with everything, ... thinking of reals (as there aren't infinite integers with any current ...
    (comp.lang.fortran)
  • Re: Use cases for quiet NaN comparison?
    ... rationale for the behaviour. ... It seems to be a Parable of the Monkeys. ... that something has gone fundamentally wrong in a calculation, and the NaN ...
    (sci.math)
  • Re: problem reading REALs from *
    ... to be nan. ... all I ever get is nan from the calculations, ... You need 3 reals for the quadratic formula, ...
    (comp.lang.fortran)