don't NaN & infinities hide FP errors

Peter Maas peter at
Wed Nov 17 11:27:48 CET 2004

kartik schrieb:
> Since Python floats are implemented using C doubles, assuming IEEE 754
> compliance, certain operations should result in NaN or infinity. Don't
> such special values hide errors? Would we want our variables to be in
> such states? I feel it would be better if a floating point division by
> zero (or other actions that produce NaNs or infinities) throws an
> exception, just like an integer divide by zero.
> Am I right?

Just try it. 1./0. raises a ZeroDivisionError, math.log(0) raises a
math range error, and math.log(-1) yields nan. If you want math.log(-1)
raise an error, too, you must write your own log() that tests for nan
and raises a NaN error or write a trace function that looks for nan
returned by math.log() (see sys.settrace()).

Peter Maas,  M+R Infosysteme,  D-52070 Aachen,  Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')

More information about the Python-list mailing list