Infinity?

Richard A. O'Keefe ok at cs.otago.ac.nz
Thu Oct 10 00:52:26 UTC 2002


Andrew Henrick <ahenrick at nd.edu> wrote:
	Q3.  How can I detect an underflow/overflow in the system.
	     ... "Infinity"

If you run Squeak on a machine that supports IEEE arithmetic,
you get IEEE arithmetic.

IEEE 754 floating-point arithmetic has the following features,
all for excellent reason, but all serving to highlight the fact
that floating-point arithmetic is not at all the same as arithmetic
on "real" numbers:

(1) +0.0 and -0.0 are different.
    They have different behaviour, but the standard numeric equality
    is supposed to regard them as equivalent.
    sqrt(-0.0) is defined to be -0.0.
(2) There are two special values +Infinity and -Infinity.
    1.0/(+0.0) gives you +Infinity,
    1.0/(-0.0) gives you -Infinity.
    -Infinity < any finite number < +Infinity.
    -Infinity = -Infinity, +Infinity = +Infinity.
(3) There are lots and lots of other special values called "Not-a-Number".
    They come in two kinds: "Quiet NaNs" (QNaNs) and "Signalling NaNs"
    (SNaNs).  Operations with an undefined result give you a NaN,
    such as 0.0/0.0.  NaNs compare unequal to everything,
    *including themselves*.  Strange but true.
(4) Most numbers are "normalised".  That is, they are shifted until
    there is a 1 to the left of the binary point and then the 1 is
    thrown away.  But there are also "denormalised" or "subnormal"
    numbers which are very small.

Apart from that, it's just like most other base-2 floating point schemes.

This may explain why there are
    Float infinity
    Float nan
    Float negativeZero
and
    aFloat isInfinite
    aFloat isNaN

Oddly enough, Float has a class variable MaxVal, but does not
provide an accessor for it, unlike SmallInteger maxVal.

    Float class methodsFor: 'constants'
<CSOTD>
    maxVal
	"Answer the most positive finite floating point number."
	^MaxVal

    minVal
	"Answer the most negative finite floating point number."
	^MaxVal negated

    epsilon
	"Answer the value of one unit in the lowest place when
	 the exponent is zero.  This is NOT the value of the
	 class variable Epsilon."
	^2.2204460492503131e-16
</CSOTD>

Now the IEEE 754 standard does *recommend* that there be a way to
ask for interrupts or some other kind of signal instead of quietly
producing infinities and NaNs, but it does not *require* this, and
some systems have been produced lacking such means.  Until C99
came out, there was no portable way to ask for this anyway, and we
can't yet rely on finding C99 compilers and libraries, so it would
be a lot of work for Squeak to ensure that such interrupts were
given.

In fact the IEEE standard was designed so that you SHOULDN'T be
relying on interrupts for this kind of stuff.  The idea is that
you do your calculation and then check at the end to see whether
you got a NaN (oh dear, try some other way) or an Infinity (in
which case the right answer is too big to represent).

A good reference for this stuff is the old Apple SANE manual.




More information about the Squeak-dev mailing list