Skip to content

Commit ac867f1

Browse files
ajthrblurb-it[bot]mdickinson
authored
bpo-44357:Add math.cbrt() function: Cube Root (GH-26622)
* Add math.cbrt() function: Cube Root Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Mark Dickinson <[email protected]>
1 parent 90cd433 commit ac867f1

File tree

6 files changed

+36
-0
lines changed

6 files changed

+36
-0
lines changed

Doc/library/math.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@ necessarily has no fractional bits.
342342
Power and logarithmic functions
343343
-------------------------------
344344

345+
.. function:: cbrt(x)
346+
347+
Return the cube root of *x*.
348+
349+
.. versionadded:: 3.11
350+
351+
345352
.. function:: exp(x)
346353

347354
Return *e* raised to the power *x*, where *e* = 2.718281... is the base

Doc/whatsnew/3.11.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ Support :PEP:`515`-style initialization of :class:`~fractions.Fraction` from
9393
string. (Contributed by Sergey B Kirpichev in :issue:`44258`.)
9494

9595

96+
math
97+
----
98+
99+
Add :func:`math.cbrt()`: return the cube root of x.
100+
(Contributed by Ajith Ramachandran in :issue:`44357`.)
101+
102+
96103
Removed
97104
=======
98105
* :class:`smtpd.MailmanProxy` is now removed as it is unusable without

Lib/test/test_math.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,22 @@ def testAtan2(self):
377377
self.assertTrue(math.isnan(math.atan2(NAN, INF)))
378378
self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
379379

380+
def testCbrt(self):
381+
self.assertRaises(TypeError, math.cbrt)
382+
self.ftest('cbrt(0)', math.cbrt(0), 0)
383+
self.ftest('cbrt(1)', math.cbrt(1), 1)
384+
self.ftest('cbrt(8)', math.cbrt(8), 2)
385+
self.ftest('cbrt(0.0)', math.cbrt(0.0), 0.0)
386+
self.ftest('cbrt(-0.0)', math.cbrt(-0.0), -0.0)
387+
self.ftest('cbrt(1.2)', math.cbrt(1.2), 1.062658569182611)
388+
self.ftest('cbrt(-2.6)', math.cbrt(-2.6), -1.375068867074141)
389+
self.ftest('cbrt(27)', math.cbrt(27), 3)
390+
self.ftest('cbrt(-1)', math.cbrt(-1), -1)
391+
self.ftest('cbrt(-27)', math.cbrt(-27), -3)
392+
self.assertEqual(math.cbrt(INF), INF)
393+
self.assertEqual(math.cbrt(NINF), NINF)
394+
self.assertTrue(math.isnan(math.cbrt(NAN)))
395+
380396
def testCeil(self):
381397
self.assertRaises(TypeError, math.ceil)
382398
self.assertEqual(int, type(math.ceil(0.5)))

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ Jérôme Radix
14151415
Burton Radons
14161416
Abhilash Raj
14171417
Shorya Raj
1418+
Ajith Ramachandran
14181419
Dhushyanth Ramasamy
14191420
Ashwin Ramaswami
14201421
Jeff Ramnani
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a function that returns cube root of the given number :func:`math.cbrt`

Modules/mathmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,9 @@ FUNC2(atan2, m_atan2,
11821182
FUNC1(atanh, m_atanh, 0,
11831183
"atanh($module, x, /)\n--\n\n"
11841184
"Return the inverse hyperbolic tangent of x.")
1185+
FUNC1(cbrt, cbrt, 0,
1186+
"cbrt($module, x, /)\n--\n\n"
1187+
"Return the cube root of x.")
11851188

11861189
/*[clinic input]
11871190
math.ceil
@@ -3550,6 +3553,7 @@ static PyMethodDef math_methods[] = {
35503553
{"atan", math_atan, METH_O, math_atan_doc},
35513554
{"atan2", (PyCFunction)(void(*)(void))math_atan2, METH_FASTCALL, math_atan2_doc},
35523555
{"atanh", math_atanh, METH_O, math_atanh_doc},
3556+
{"cbrt", math_cbrt, METH_O, math_cbrt_doc},
35533557
MATH_CEIL_METHODDEF
35543558
{"copysign", (PyCFunction)(void(*)(void))math_copysign, METH_FASTCALL, math_copysign_doc},
35553559
{"cos", math_cos, METH_O, math_cos_doc},

0 commit comments

Comments
 (0)