Add workaround for log1p(-0.0) on platforms where it's broken.
authorMark Dickinson <mdickinson@enthought.com>
Sat, 18 Aug 2012 11:31:34 +0000 (12:31 +0100)
committerMark Dickinson <mdickinson@enthought.com>
Sat, 18 Aug 2012 11:31:34 +0000 (12:31 +0100)
Misc/NEWS
Modules/_math.c
Modules/_math.h

index 611dc6f97b4c866dd4c1da806abeec34a0363ef6..190f8f4e3693bfe301e8ad5aba2b9e5ca4c2929c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -95,6 +95,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #15477: In cmath and math modules, add workaround for platforms whose
+  system-supplied log1p function doesn't respect signs of zeros.
+
 - Issue #11062: Fix adding a message from file to Babyl mailbox.
 
 - Issue #15646: Prevent equivalent of a fork bomb when using
index 2fef48107f1ac76aef05944f64a331dc2d01ec45..fe75a36ec5171ea78364cd2e265b849af4c228ae 100644 (file)
@@ -189,6 +189,27 @@ _Py_expm1(double x)
    significant loss of precision that arises from direct evaluation when x is
    small. */
 
+#ifdef HAVE_LOG1P
+
+double
+_Py_log1p(double x)
+{
+    /* Some platforms supply a log1p function but don't respect the sign of
+       zero:  log1p(-0.0) gives 0.0 instead of the correct result of -0.0.
+
+       To save fiddling with configure tests and platform checks, we handle the
+       special case of zero input directly on all platforms.
+    */
+    if (x == 0.0) {
+        return x;
+    }
+    else {
+        return log1p(x);
+    }
+}
+
+#else
+
 double
 _Py_log1p(double x)
 {
@@ -230,3 +251,5 @@ _Py_log1p(double x)
         return log(1.+x);
     }
 }
+
+#endif /* ifdef HAVE_LOG1P */
index c0ceece662600990ffd5a520efbb8f3c80efecce..cf079ad770dc27669212a865c3f231ddd233b73c 100644 (file)
@@ -36,10 +36,6 @@ double _Py_log1p(double x);
 #define m_expm1 _Py_expm1
 #endif
 
-#ifdef HAVE_LOG1P
-#define m_log1p log1p
-#else
-/* if the system doesn't have log1p, use the substitute
-   function defined in Modules/_math.c. */
+/* Use the substitute from _math.c on all platforms:
+   it includes workarounds for buggy handling of zeros. */
 #define m_log1p _Py_log1p
-#endif