[Python-checkins] r62400 - python/trunk/Modules/mathmodule.c

mark.dickinson python-checkins at python.org
Sat Apr 19 21:41:52 CEST 2008


Author: mark.dickinson
Date: Sat Apr 19 21:41:52 2008
New Revision: 62400

Log:
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).


Modified:
   python/trunk/Modules/mathmodule.c

Modified: python/trunk/Modules/mathmodule.c
==============================================================================
--- python/trunk/Modules/mathmodule.c	(original)
+++ python/trunk/Modules/mathmodule.c	Sat Apr 19 21:41:52 2008
@@ -521,9 +521,19 @@
 	y = PyFloat_AsDouble(oy);
 	if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
 		return NULL;
-	/* 1**x and x**0 return 1., even if x is a NaN or infinity. */
-	if (x == 1.0 || y == 0.0)
+
+	/* deal directly with various special cases, to cope with problems on
+	   various platforms whose semantics don't exactly match C99 */
+
+	/* 1**x, x**0, and (-1)**(+-infinity) return 1., even if x is NaN or
+	   an infinity. */
+	if (x == 1. || y == 0. || (x == -1. && Py_IS_INFINITY(y)))
 	        return PyFloat_FromDouble(1.);
+	/* otherwise, return a NaN if either input was a NaN */
+	if (Py_IS_NAN(x))
+		return PyFloat_FromDouble(x);
+	if (Py_IS_NAN(y))
+		return PyFloat_FromDouble(y);
 	/* inf ** (nonzero, non-NaN) is one of +-0, +-infinity */
 	if (Py_IS_INFINITY(x) && !Py_IS_NAN(y)) {
 		y_is_odd = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0;
@@ -539,10 +549,7 @@
 	r = pow(x, y);
 	PyFPE_END_PROTECT(r);
 	if (Py_IS_NAN(r)) {
-		if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
-			errno = EDOM;
-		else
-			errno = 0;
+		errno = EDOM;
 	}
 	/* an infinite result arises either from:
 


More information about the Python-checkins mailing list