Commit 2558e8b9 authored by Mark Dickinson's avatar Mark Dickinson

Make floating-point exception error messages slightly more verbose: in

particular, the error message now allows one to distinguish between a
ValueError arising from a singularity (e.g. log(0.)), which would
usually produce +-infinity in non-stop mode, and a ValueError resulting
from an invalid input (e.g. sqrt(-1.)), which would normally produce a
NaN in non-stop mode.
parent cf8d13f4
...@@ -127,31 +127,31 @@ Trigonometric Functions ...@@ -127,31 +127,31 @@ Trigonometric Functions
>>> sin(INF) >>> sin(INF)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: math domain error ValueError: math domain error (invalid argument)
>>> sin(NINF) >>> sin(NINF)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: math domain error ValueError: math domain error (invalid argument)
>>> sin(NAN) >>> sin(NAN)
nan nan
>>> cos(INF) >>> cos(INF)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: math domain error ValueError: math domain error (invalid argument)
>>> cos(NINF) >>> cos(NINF)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: math domain error ValueError: math domain error (invalid argument)
>>> cos(NAN) >>> cos(NAN)
nan nan
>>> tan(INF) >>> tan(INF)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: math domain error ValueError: math domain error (invalid argument)
>>> tan(NINF) >>> tan(NINF)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: math domain error ValueError: math domain error (invalid argument)
>>> tan(NAN) >>> tan(NAN)
nan nan
...@@ -169,11 +169,11 @@ True ...@@ -169,11 +169,11 @@ True
>>> asin(INF), asin(NINF) >>> asin(INF), asin(NINF)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: math domain error ValueError: math domain error (invalid argument)
>>> acos(INF), acos(NINF) >>> acos(INF), acos(NINF)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: math domain error ValueError: math domain error (invalid argument)
>>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2) >>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
(True, True) (True, True)
......
...@@ -741,9 +741,9 @@ class MathTests(unittest.TestCase): ...@@ -741,9 +741,9 @@ class MathTests(unittest.TestCase):
func = getattr(math, fn) func = getattr(math, fn)
try: try:
result = func(ar) result = func(ar)
except ValueError: except ValueError as exc:
message = ("Unexpected ValueError in " + message = (("Unexpected ValueError: %s\n " +
"test %s:%s(%r)\n" % (id, fn, ar)) "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
self.fail(message) self.fail(message)
self.ftest("%s:%s(%r)" % (id, fn, ar), result, er) self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
......
...@@ -174,18 +174,21 @@ math_1_to_whatever(PyObject *arg, double (*func) (double), ...@@ -174,18 +174,21 @@ math_1_to_whatever(PyObject *arg, double (*func) (double),
PyFPE_START_PROTECT("in math_1", return 0); PyFPE_START_PROTECT("in math_1", return 0);
r = (*func)(x); r = (*func)(x);
PyFPE_END_PROTECT(r); PyFPE_END_PROTECT(r);
if (Py_IS_NAN(r)) { if (Py_IS_NAN(r) && !Py_IS_NAN(x)) {
if (!Py_IS_NAN(x)) PyErr_SetString(PyExc_ValueError,
errno = EDOM; "math domain error (invalid argument)");
else return NULL;
errno = 0;
} }
else if (Py_IS_INFINITY(r)) { if (Py_IS_INFINITY(r) && Py_IS_FINITE(x)) {
if (Py_IS_FINITE(x)) if (can_overflow)
errno = can_overflow ? ERANGE : EDOM; PyErr_SetString(PyExc_OverflowError,
"math range error (overflow)");
else else
errno = 0; PyErr_SetString(PyExc_ValueError,
"math domain error (singularity)");
return NULL;
} }
/* on most machines, errno should be 0 at this point */
if (errno && is_error(r)) if (errno && is_error(r))
return NULL; return NULL;
else else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment