[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Locate an underflow

Craig Markwardt wrote:
> Paul van Delst <paul.vandelst@noaa.gov> writes:
> > Hmm. I do see your point, but if I grab someone else's code (not
> > just IDL code BTW) the first thing I do is run their supplied test
> > case (I hope there is one) with all warning flags on (for IDL,
> > !EXCEPT = 2; for Fortran or similar, set the platform specific
> > compiler switch to trap under/overflows, divide by zero, etc.).
> >
> > If, on running said code, I get a crapload of underflow errors, it's
> > an indication that that either a) the code hasn't been tested very
> > well or b) the programmer didn't really think about the problem
> > enough (and I'm guilty of both of these.... most of the time
> > actually).  If there are (usually harmelss) underflow errors, how do
> > I know that there won't be other more serious errors at some point
> > for different input?
> Yah, but consider the difference between the following bits of code:
> 1>  y = exp(-x^2)
> 2>  u = x^2
> 2>  sz = size(x)
> 2>  isdouble = sz(sz(0)+1) EQ 5
> 2>  mask = u LT alog(machar(double=isdouble).xmax)
> 2>  y = mask*exp(-u*mask)
> Both sets of code accomplish the same thing, computing a gaussian
> function, except the second one avoids bogus underflow error messages.
> Which one do you think I'd rather write? :-)

The one that avoids errors? :o)

> Which one shows the original mathematical intent more ?

If the code is commented I fail to see the problem. Really. How about

  y = gaussian_function( x )

which encapsulates all the checking? (Prefixed with the original author's initials of
course to avoid namespace collisions... :o)

To be fair, the entire debate has no meaning without some context. I agree with you (and
William Thompson) completely for "regular" stuff - you know, the day to day computing that
everyone does. However, for applications (e.g. flight control software, numerical weather
prediction, etc.) upon which a lot more is at stake (e.g. lives, property damage, etc) I
think it's worth the extra (not much more) effort. And when you've done it once, you just
re-use the same function/routine/procedure/whatever.


Paul van Delst           A little learning is a dangerous thing;
CIMSS @ NOAA/NCEP        Drink deep, or taste not the Pierian spring;
Ph: (301)763-8000 x7274  There shallow draughts intoxicate the brain,
Fax:(301)763-8545        And drinking largely sobers us again.
                                         Alexander Pope.