Added copysign(x, y) function to the math module

This commit is contained in:
Christian Heimes 2008-01-03 22:32:26 +00:00
parent 000a074c95
commit eebb79cc69
4 changed files with 28 additions and 1 deletions

View 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*.

View File

@ -229,6 +229,13 @@ def testTanh(self):
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.))

View File

@ -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

View 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},