Numerics, NaNs, IEEE 754 and C99

Grant Edwards grante at visi.com
Wed Jun 14 12:17:52 EDT 2006


On 2006-06-14, Christophe <chris.cavalaria at free.fr> wrote:
> Grant Edwards a écrit :
>> The division by zero trap is really annoying.  In my world the
>> right thing to do is to return Inf.
>
> Your world is flawed then, this is a big mistake. NaN is the
> only aceptable return value for a division by zero.

You're probably right if you're talking about math, but I'm not
doing math.  I'm doing engineering.  In all of the situations
I've ever encountered, Inf was a much better choice.

Aside from Python, every FP library or processor I've ever used
returned Inf for divide by zero (which is the behavior required
by IEEE 754).

I need my Python programs to work the same way as everything
else.


http://standards.ieee.org/reading/ieee/interp/754-1985.html

  In IEEE Std 754-1985, subclause 7.2- Division by Zero, it says:

     "If the divisor is zero and the dividend is a finite nonzero
      number, then the division by zero shall be signaled. The
      result, when no trap occurs, shall be a correctly signed
      (infinity symbol)(6.3)."

  While this is apparently the convention decided on by the
  committee, it is mathematically incorrect and it seems as if
  it should have been designated as Not-a-Number, since
  division by zero is mathematically undefined and implies that
  0*infinity=1, which is patently absurd.

  Why was this convention chosen instead of NaN, since it leads
  to a further degradation of our children's math abilities,
  given that the IEEE floating-point standard would be considered
  to be authoritative on this subject, yet produces an erroneous
  results.

  Interpretation for IEEE Std 754-1985
  
  When a non-zero number is divided by a zero number, that is a
  divide by zero. It is interpreted as an attempt to
  take a limit of the ratio of two numbers as the denominator
  becomes too small to be represented in the number system
  while the numerator remains representable. Such a limit is best
  represented by an infinity of the appropriate sign.
  
  When zero is divided by zero, no such extrapolation can be
  made. If it is caused by an attempt to take the limit of
  the ratio of two numbers when both become two small to be
  represented, then the limit cannot be determined. If it
  is caused by some mistake in the programming, then no limit
  exists. Thus, this case is thought to be invalid and a NaN
  of appropriate sign is returned. (The sign is the only bit of
  information that can be determined.)
  
  While counter examples to the mathematical interpretation of
  both of these results can be constructed they tend to be either
  the result of extreme scaling or an attempt to evaluate a
  non-analytic function. The former can be resolved by
  rescaling. But, as the latter involve functions that cannot
  (formally) be evaluated on a computer (without extreme effort
  anyway) in the region of their non-analyticity, usually no good
  solution exists.

-- 
Grant Edwards                   grante             Yow!  I represent a
                                  at               sardine!!
                               visi.com            



More information about the Python-list mailing list