8000 gh-135853: add `math.signbit` (#135877) · python/cpython@42ccac2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 42ccac2

Browse files
authored
gh-135853: add math.signbit (#135877)
1 parent ff7b5d4 commit 42ccac2

File tree

6 files changed

+81
-3
lines changed

6 files changed

+81
-3
lines changed

Doc/library/math.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ noted otherwise, all return values are floats.
5959
:func:`isnan(x) <isnan>` Check if *x* is a NaN (not a number)
6060
:func:`ldexp(x, i) <ldexp>` ``x * (2**i)``, inverse of function :func:`frexp`
6161
:func:`nextafter(x, y, steps) <nextafter>` Floating-point value *steps* steps after *x* towards *y*
62+
:func:`signbit(x) <signbit>` Check if *x* is a negative number
6263
:func:`ulp(x) <ulp>` Value of the least significant bit of *x*
6364

6465
**Power, exponential and logarithmic functions**
@@ -431,6 +432,15 @@ Floating point manipulation functions
431432
Added the *steps* argument.
432433

433434

435+
.. function:: signbit(x)
436+
437+
Return ``True`` if the sign of *x* is negative and ``False`` otherwise.
438+
439+
This is useful to detect the sign bit of zeroes, infinities and NaNs.
440+
441+
.. versionadded:: next
442+
443+
434444
.. function:: ulp(x)
435445

436446
Return the value of the least significant bit of the float *x*:

Doc/whatsnew/3.15.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ math
115115
* Add :func:`math.isnormal` and :func:`math.issubnormal` functions.
116116
(Contributed by Sergey B Kirpichev in :gh:`132908`.)
117117

118+
* Add :func:`math.signbit` function.
119+
(Contributed by Bénédikt Tran in :gh:`135853`.)
120+
118121

119122
os.path
120123
-------

Lib/test/test_math.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,19 @@ def testCopysign(self):
475475
# similarly, copysign(2., NAN) could be 2. or -2.
476476
self.assertEqual(abs(math.copysign(2., NAN)), 2.)
477477

478+
def test_signbit(self):
479+
self.assertRaises(TypeError, math.signbit)
480+
self.assertRaises(TypeError, math.signbit, '1.0')
481+
482+
# C11, §7.12.3.6 requires signbit() to return a nonzero value
483+
# if and only if the sign of its argument value is negative,
484+
# but in practice, we are only interested in a boolean value.
485+
self.assertIsInstance(math.signbit(1.0), bool)
486+
487+
for arg in [0., 1., INF, NAN]:
488+
self.assertFalse(math.signbit(arg))
489+
self.assertTrue(math.signbit(-arg))
490+
478491
def testCos(self):
479492
self.assertRaises(TypeError, math.cos)
480493
self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0, abs_tol=math.ulp(1))
@@ -1387,7 +1400,6 @@ def __rmul__(self, other):
13871400
args = ((-5, -5, 10), (1.5, 4611686018427387904, 2305843009213693952))
13881401
self.assertEqual(sumprod(*args), 0.0)
13891402

1390-
13911403
@requires_IEEE_754
13921404
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
13931405
"sumprod() accuracy not guaranteed on machines with double rounding")
@@ -2486,7 +2498,6 @@ def test_nextafter(self):
24862498
with self.assertRaises(ValueError):
24872499
math.nextafter(1.0, INF, steps=-1)
24882500

2489-
24902501
@requires_IEEE_754
24912502
def test_ulp(self):
24922503
self.assertEqual(math.ulp(1.0), sys.float_info.epsilon)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`math`: expose C99 :func:`~math.signbit` function to determine whether
2+
the sign bit of a floating-point value is set. Patch by Bénédikt Tran.

Modules/clinic/mathmodule.c.h

Lines changed: 35 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/mathmodule.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,23 @@ FUNC2(remainder, m_remainder,
12331233
"Return x - n*y where n*y is the closest integer multiple of y.\n"
12341234
"In the case where x is exactly halfway between two multiples of\n"
12351235
"y, the nearest even value of n is used. The result is always exact.")
1236+
1237+
/*[clinic input]
1238+
math.signbit
1239+
1240+
x: double
1241+
/
1242+
1243+
Return True if the sign of x is negative and False otherwise.
1244+
[clinic start generated code]*/
1245+
1246+
static PyObject *
1247+
math_signbit_impl(PyObject *module, double x)
1248+
/*[clinic end generated code: output=20c5f20156a9b871 input=3d3493fbcb5bdb3e]*/
1249+
{
1250+
return PyBool_FromLong(signbit(x));
1251+
}
1252+
12361253
FUNC1D(sin, sin, 0,
12371254
"sin($module, x, /)\n--\n\n"
12381255
"Return the sine of x (measured in radians).",
@@ -4199,6 +4216,7 @@ static PyMethodDef math_methods[] = {
41994216
MATH_POW_METHODDEF
42004217
MATH_RADIANS_METHODDEF
42014218
{"remainder", _PyCFunction_CAST(math_remainder), METH_FASTCALL, math_remainder_doc},
4219+
MATH_SIGNBIT_METHODDEF
42024220
{"sin", math_sin, METH_O, math_sin_doc},
42034221
{"sinh", math_sinh, METH_O, math_sinh_doc},
42044222
{"sqrt", math_sqrt, METH_O, math_sqrt_doc},

0 commit comments

Comments
 (0)
0