]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Added copysign(x, y) function to the math module
authorChristian Heimes <christian@cheimes.de>
Thu, 3 Jan 2008 22:32:26 +0000 (22:32 +0000)
committerChristian Heimes <christian@cheimes.de>
Thu, 3 Jan 2008 22:32:26 +0000 (22:32 +0000)
Doc/library/math.rst
Lib/test/test_math.py
Misc/NEWS
Modules/mathmodule.c

index d0b03ed1b7127cea38395a63a4c5c2d0706d9b08..36cdbceb7dae927b5dea3a7fd30e7858358e198d 100644 (file)
@@ -31,6 +31,14 @@ Number-theoretic and representation functions:
    should return an :class:`Integral` value.
 
 
+.. function:: copysign(x, y)
+
+   Return *x* with the sign of *y*. ``copysign`` copies the sign bit of an IEEE
+   754 float, ``copysign(1, -0.0)`` returns *-1.0*.
+
+   ..versionadded:: 2.6
+
+
 .. function:: fabs(x)
 
    Return the absolute value of *x*.
index d2da59bb966fcba07c38d6d97dd3a3d1eda6310e..f5bf1a3f98355116339e975fd57f74ab08df7c60 100644 (file)
@@ -229,6 +229,13 @@ class MathTests(unittest.TestCase):
         self.ftest('tanh(0)', math.tanh(0), 0)
         self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
 
+    def testCopysign(self):
+        self.assertEqual(math.copysign(1, 42), 1.0)
+        self.assertEqual(math.copysign(0., 42), 0.0)
+        self.assertEqual(math.copysign(1., -42), -1.0)
+        self.assertEqual(math.copysign(3, 0.), 3.0)
+        self.assertEqual(math.copysign(4., -0.), -4.0)
+
     def testIsnan(self):
         self.assert_(math.isnan(float("nan")))
         self.assert_(math.isnan(float("inf")* 0.))
index ee0bec2966b33555a0540a2c7f3a1b183ce6edf1..2a4b72223f2155a012846a78ef3e768ae76d3839 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,7 +12,8 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
-- Issue #1640: Added math.isinf() and math.isnan() functions.
+- Issue #1640: Added math.isinf(x), math.isnan(x) and math.copysign(x, y)
+  functions.
 
 - Issue #1635: Platform independent creation and representation of NaN
   and INF. float("nan"), float("inf") and float("-inf") now work on every
index 7540b5d04f3d295f2179a0c1017c80600f34c2be..2c6262d89442a4f19f43e92efcca89fac60625c4 100644 (file)
@@ -133,6 +133,14 @@ FUNC1(cos, cos,
       "cos(x)\n\nReturn the cosine of x (measured in radians).")
 FUNC1(cosh, cosh,
       "cosh(x)\n\nReturn the hyperbolic cosine of x.")
+#if defined(MS_WINDOWS) || defined(HAVE_COPYSIGN)
+#ifdef MS_WINDOWS
+FUNC2(copysign, _copysign,
+#else
+FUNC2(copysign, copysign,
+#endif
+    "copysign(x,y)\n\nReturn x with the sign of y.");
+#endif
 FUNC1(exp, exp,
       "exp(x)\n\nReturn e raised to the power of x.")
 FUNC1(fabs, fabs,
@@ -375,6 +383,9 @@ static PyMethodDef math_methods[] = {
        {"atan",        math_atan,      METH_O,         math_atan_doc},
        {"atan2",       math_atan2,     METH_VARARGS,   math_atan2_doc},
        {"ceil",        math_ceil,      METH_O,         math_ceil_doc},
+#if defined(MS_WINDOWS) || defined(HAVE_COPYSIGN)
+       {"copysign",    math_copysign,  METH_VARARGS,   math_copysign_doc},
+#endif
        {"cos",         math_cos,       METH_O,         math_cos_doc},
        {"cosh",        math_cosh,      METH_O,         math_cosh_doc},
        {"degrees",     math_degrees,   METH_O,         math_degrees_doc},