Module malachite_base::num::arithmetic::sqrt
source · Expand description
Traits for taking the square root of a number.
The traits are FloorSqrt
, FloorSqrtAssign
,
CeilingSqrt
, CeilingSqrtAssign
,
CheckedSqrt
, SqrtRem
,
SqrtAssignRem
, and SqrtAssign
.
§floor_sqrt
use malachite_base::num::arithmetic::traits::FloorSqrt;
assert_eq!(99u8.floor_sqrt(), 9);
assert_eq!(100u8.floor_sqrt(), 10);
assert_eq!(101u8.floor_sqrt(), 10);
assert_eq!(1000000000i32.floor_sqrt(), 31622);
assert_eq!(10000000000i64.floor_sqrt(), 100000);
§floor_sqrt_assign
use malachite_base::num::arithmetic::traits::FloorSqrtAssign;
let mut x = 99u8;
x.floor_sqrt_assign();
assert_eq!(x, 9);
let mut x = 100u8;
x.floor_sqrt_assign();
assert_eq!(x, 10);
let mut x = 101u8;
x.floor_sqrt_assign();
assert_eq!(x, 10);
let mut x = 1000000000i32;
x.floor_sqrt_assign();
assert_eq!(x, 31622);
let mut x = 10000000000i64;
x.floor_sqrt_assign();
assert_eq!(x, 100000);
§ceiling_sqrt
use malachite_base::num::arithmetic::traits::CeilingSqrt;
assert_eq!(99u8.ceiling_sqrt(), 10);
assert_eq!(100u8.ceiling_sqrt(), 10);
assert_eq!(101u8.ceiling_sqrt(), 11);
assert_eq!(1000000000u32.ceiling_sqrt(), 31623);
assert_eq!(10000000000u64.ceiling_sqrt(), 100000);
§ceiling_sqrt_assign
use malachite_base::num::arithmetic::traits::CeilingSqrtAssign;
let mut x = 99u8;
x.ceiling_sqrt_assign();
assert_eq!(x, 10);
let mut x = 100u8;
x.ceiling_sqrt_assign();
assert_eq!(x, 10);
let mut x = 101u8;
x.ceiling_sqrt_assign();
assert_eq!(x, 11);
let mut x = 1000000000i32;
x.ceiling_sqrt_assign();
assert_eq!(x, 31623);
let mut x = 10000000000i64;
x.ceiling_sqrt_assign();
assert_eq!(x, 100000);
§checked_sqrt
use malachite_base::num::arithmetic::traits::CheckedSqrt;
assert_eq!(99u8.checked_sqrt(), None);
assert_eq!(100u8.checked_sqrt(), Some(10));
assert_eq!(101u8.checked_sqrt(), None);
assert_eq!(1000000000i32.checked_sqrt(), None);
assert_eq!(10000000000i64.checked_sqrt(), Some(100000));
§sqrt_rem
use malachite_base::num::arithmetic::traits::SqrtRem;
assert_eq!(99u8.sqrt_rem(), (9, 18));
assert_eq!(100u8.sqrt_rem(), (10, 0));
assert_eq!(101u8.sqrt_rem(), (10, 1));
assert_eq!(1000000000u32.sqrt_rem(), (31622, 49116));
assert_eq!(10000000000u64.sqrt_rem(), (100000, 0));
§sqrt_assign_rem
use malachite_base::num::arithmetic::traits::SqrtAssignRem;
let mut x = 99u8;
assert_eq!(x.sqrt_assign_rem(), 18);
assert_eq!(x, 9);
let mut x = 100u8;
assert_eq!(x.sqrt_assign_rem(), 0);
assert_eq!(x, 10);
let mut x = 101u8;
assert_eq!(x.sqrt_assign_rem(), 1);
assert_eq!(x, 10);
let mut x = 1000000000u32;
assert_eq!(x.sqrt_assign_rem(), 49116);
assert_eq!(x, 31622);
let mut x = 10000000000u64;
assert_eq!(x.sqrt_assign_rem(), 0);
assert_eq!(x, 100000);
§sqrt_assign
use malachite_base::num::arithmetic::traits::SqrtAssign;
use malachite_base::num::float::NiceFloat;
let mut x = 4.0f64;
x.sqrt_assign();
assert_eq!(NiceFloat(x), NiceFloat(2.0));
let mut x = 2.0f64;
x.sqrt_assign();
assert_eq!(NiceFloat(x), NiceFloat(std::f64::consts::SQRT_2));