[E-Lang] Operators #2: Comparison
Karp, Alan
alan_karp@hp.com
Tue, 10 Apr 2001 14:33:12 -0700
Dean Tribble wrote:
A rant against the use of NaNs in E. (Sorry if I misrepresented
you.)
First of all, I've encountered NaNs and made good use of them quite
frequently back in my days of scientific computing. NaNs were put into the
IEEE spec for good reason, and they are widely used to good effect.
Throwing an exception defeats a good bit of their reason for being. People
who want exceptions can use signalling NaNs, SNaNs.
Silently propagating QNaNs (quiet, as opposed to SNaNs) allows more
efficient programs to be written. Take the simple case of finding the norm
of a vector.
s = 0.0;
for (i=0; i<N; i++) s += v[i]*v[i];
s = sqrt(s);
The problem is that v[i]*v[i] might overflow even though s is representable.
In the bad old days, we'd go through v, find the largest element, and scale
the whole vector, even though the overflow almost never occurred. Today, I
simply go through the loop and check s==s at the end to see if there was a
problem. If s!=s, I go back and do things the slow way. The new way runs
20%-70% faster on modern workstations, depending on memory speed. This
simple example doesn't use compares, but there are plenty that do, so I
don't recommend doing anything special there, either.
There are also rules for propagating NaNs. For example, A+B returns A if
both A and B are NaNs. That means a programmer can encode debugging info
into the mantissa field of a NaN and count on it surviving a sequence of
operations. This feature is important enough that I have a patent on how to
do it with software quad precision.
The bottom line is that E should follow the IEEE standard exactly. A lot of
very smart people worked on it for a long time. Each feature is there for a
very good reason, even if no one person understands them all (except for Vel
Kahan, of course).
Dean Tribble also wrote:
> The third is the use of float64 at all, when lots of graphics, etc,
> profitably use float32.
Scientific calculations need float64, so you can't ignore it. Besides,
modern machines all do 64-bit ops as fast as 32-bit ops. Even intrinsic
libraries are only 10-20% slower on 64-bit numbers. However, you don't want
to leave out 32-bit floats. In addition to memory considerations, there are
special architecture features. The iA64 (aka Itanium) has parallel floating
point ops that let you operate in parallel on 2 float32s in a single, 64-bit
floating point register.
_________________________
Alan Karp
Principal Scientist
Decision Technology Department
Hewlett-Packard Laboratories MS 1U-2
1501 Page Mill Road
Palo Alto, CA 94304
(650) 857-3967, fax (650) 857-6278
https://ecardfile.com/id/Alan_Karp
http://www.hpl.hp.com/personal/Alan_Karp/