Skip to content

Commit fbd2ac4

Browse files
committed
Add math.cbrt
1 parent f51b130 commit fbd2ac4

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

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

stdlib/src/math.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod math {
1010
use num_bigint::BigInt;
1111
use num_traits::{One, Signed, Zero};
1212
use rustpython_common::float_ops;
13+
use rustpython_vm::protocol::PyNumber;
1314
use std::cmp::Ordering;
1415

1516
// Constants
@@ -536,6 +537,11 @@ mod math {
536537
math_perf_arb_len_int_op(args, |x, y| x.lcm(y.as_bigint()), BigInt::one())
537538
}
538539

540+
#[pyfunction]
541+
fn cbrt(x: ArgIntoFloat) -> f64 {
542+
x.cbrt()
543+
}
544+
539545
#[pyfunction]
540546
fn fsum(seq: ArgIterable<ArgIntoFloat>, vm: &VirtualMachine) -> PyResult<f64> {
541547
let mut partials = vec![];

0 commit comments

Comments
 (0)