-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
56: Implement Roots for BigInt and BigUint r=cuviper a=mancabizjak Supersedes #51 . Since there is now a `Roots` trait with `sqrt`, `cbrt` and `nth_root` methods in the `num-integer` crate, this PR implements it for `BigInt` and `BigUint` types. I also added inherent methods on both types to allow the users access to all these functions without having to import `Roots`. PS: `nth_root` currently uses `num_traits::pow`. Should we perhaps wait for #54 to get merged, and then replace the call to use the new `pow::Pow` implementation on `BigUint`? Co-authored-by: Manca Bizjak <[email protected]>
- Loading branch information
Showing
5 changed files
with
276 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
extern crate num_bigint; | ||
extern crate num_integer; | ||
extern crate num_traits; | ||
|
||
mod biguint { | ||
use num_bigint::BigUint; | ||
use num_traits::pow; | ||
use std::str::FromStr; | ||
|
||
fn check(x: u64, n: u32) { | ||
let big_x = BigUint::from(x); | ||
let res = big_x.nth_root(n); | ||
|
||
if n == 2 { | ||
assert_eq!(&res, &big_x.sqrt()) | ||
} else if n == 3 { | ||
assert_eq!(&res, &big_x.cbrt()) | ||
} | ||
|
||
assert!(pow(res.clone(), n as usize) <= big_x); | ||
assert!(pow(res.clone() + 1u32, n as usize) > big_x); | ||
} | ||
|
||
#[test] | ||
fn test_sqrt() { | ||
check(99, 2); | ||
check(100, 2); | ||
check(120, 2); | ||
} | ||
|
||
#[test] | ||
fn test_cbrt() { | ||
check(8, 3); | ||
check(26, 3); | ||
} | ||
|
||
#[test] | ||
fn test_nth_root() { | ||
check(0, 1); | ||
check(10, 1); | ||
check(100, 4); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_nth_root_n_is_zero() { | ||
check(4, 0); | ||
} | ||
|
||
#[test] | ||
fn test_nth_root_big() { | ||
let x = BigUint::from_str("123_456_789").unwrap(); | ||
let expected = BigUint::from(6u32); | ||
|
||
assert_eq!(x.nth_root(10), expected); | ||
} | ||
} | ||
|
||
mod bigint { | ||
use num_bigint::BigInt; | ||
use num_traits::{Signed, pow}; | ||
|
||
fn check(x: i64, n: u32) { | ||
let big_x = BigInt::from(x); | ||
let res = big_x.nth_root(n); | ||
|
||
if n == 2 { | ||
assert_eq!(&res, &big_x.sqrt()) | ||
} else if n == 3 { | ||
assert_eq!(&res, &big_x.cbrt()) | ||
} | ||
|
||
if big_x.is_negative() { | ||
assert!(pow(res.clone() - 1u32, n as usize) < big_x); | ||
assert!(pow(res.clone(), n as usize) >= big_x); | ||
} else { | ||
assert!(pow(res.clone(), n as usize) <= big_x); | ||
assert!(pow(res.clone() + 1u32, n as usize) > big_x); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_nth_root() { | ||
check(-100, 3); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_nth_root_x_neg_n_even() { | ||
check(-100, 4); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_sqrt_x_neg() { | ||
check(-4, 2); | ||
} | ||
|
||
#[test] | ||
fn test_cbrt() { | ||
check(8, 3); | ||
check(-8, 3); | ||
} | ||
} |