underflow exception (was Re: float limits)

Tim Peters tim.one at home.com
Thu Jan 31 18:42:33 EST 2002


[Alexandre Fayolle]
> ...
> By the way, is there a (preferably portable) way of getting an
> exception when a numerical computation underflows, rather than having
> it silently rounding things to zero.

No.  You can check your platform (OS & C) docs for ways they may provide to
enable underflow traps in the hardware, but that's it.

> I finally get a DivisionByZero exception, but getting the exception
> sooner in the process could avoid me some trouble, I'm sure. (for those
> of you who're wondering why I'm getting underflows, I'm computing
> probablities, and this involves multiplying *lots* of numbers in
> the [0,1[  range;

In that case I'd try explicitly checking the intermediate products, from
time to time, to see whether they've gotten so small as to be
indistinguishable from 0 for the app's purposes.  Depending on the whole
algorithm, it may be quite sensible to treat a probability of (say) 1e-20 as
if exactly zero, and may even speed things up to do so.  The difference
between a probability of 1e-100 and one of 1e-300 has no consequence in any
universe I've visited <wink>, and if so there's no point burning cycles to
put an ever-finer point on a useless distinction.





More information about the Python-list mailing list