summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-01-03 22:32:26 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-01-03 22:32:26 (GMT)
commiteebb79cc690e8ab02cb7f630cfbc046df2c0f4da (patch)
treee6eae18d02015f5adb8535f3b69cf33e1ba9eb51
parent000a074c955a1964959ee908300ef49b41170a06 (diff)
downloadcpython-eebb79cc690e8ab02cb7f630cfbc046df2c0f4da.zip
cpython-eebb79cc690e8ab02cb7f630cfbc046df2c0f4da.tar.gz
cpython-eebb79cc690e8ab02cb7f630cfbc046df2c0f4da.tar.bz2
Added copysign(x, y) function to the math module
-rw-r--r--Doc/library/math.rst8
-rw-r--r--Lib/test/test_math.py7
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/mathmodule.c11
4 files changed, 28 insertions, 1 deletions
diff --git a/Doc/library/math.rst b/Doc/library/math.rst
index d0b03ed..36cdbce 100644
--- a/Doc/library/math.rst
+++ b/Doc/library/math.rst
@@ -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*.
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index d2da59b..f5bf1a3 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -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.))
diff --git a/Misc/NEWS b/Misc/NEWS
index ee0bec2..2a4b722 100644
--- 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
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 7540b5d..2c6262d 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -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},