[Python-Dev] math.sqrt(-1) - NaN, ValueError or whatever you want?
tim.one at comcast.net
Thu Feb 5 17:19:39 EST 2004
> The answers to my math questions never seem to stick, so I'm afraid
> I'm gonna have to ask this question again (even though I think I did
> the same a couple of years ago, but google doesn't find it).
> It turns out that on MacOS math.sqrt(-1) returns "nan", as does
> math.log(-1), and I didn't try any other ones.
> This is true for all possible combinations of MacPython 2.3, 2.4a0 or
> apple-Python 2.3, MacPython-OS9 or MacPython-OSX, and Mac OS 9 or Mac
> OS X.
> When I tried the same on a linux box (Python 2.4a0) it raised
> ValueError: math domain error, which is what I would have expected.
Take this to heart (it's from the current math module docs):
Note: The math module consists mostly of thin wrappers around the
platform C math library functions. Behavior in exceptional cases is
loosely specified by the C standards, and Python inherits much of its
math-function error-reporting behavior from the platform C
implementation. As a result, the specific exceptions raised in error
cases (and even whether some arguments are considered to be exceptional
at all) are not defined in any useful cross-platform or cross-release
way. For example, whether math.log(0) returns -Inf or raises
ValueError or OverflowError isn't defined, and in cases where
math.log(0) raises OverflowError, math.log(0L) may raise ValueError
> Why this difference?
> Is it something I should fix?
Good luck <wink>.
You'll understand the "<wink>" above after you give up.
> Or document it?
I think the above already does. Else you're trying to document a maze of
moving targets (including, on some platforms, the specific compiler options,
link options, and environment variables the user indulges).
More information about the Python-Dev