Expand description
This is the main data structure of this library. It represents an arbitrary-precision floating-point number. The data structure is generic and accepts the EXPONENT and MANTISSA constants, that represent the encoding number of bits that are dedicated to storing these values.
Implementations§
source§impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
sourcepub fn add_with_rm(a: Self, b: Self, rm: RoundingMode) -> Self
pub fn add_with_rm(a: Self, b: Self, rm: RoundingMode) -> Self
Computes a+b using the rounding mode rm.
sourcepub fn sub_with_rm(a: Self, b: Self, rm: RoundingMode) -> Self
pub fn sub_with_rm(a: Self, b: Self, rm: RoundingMode) -> Self
Computes a-b using the rounding mode rm.
source§impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
sourcepub fn mul_with_rm(a: Self, b: Self, rm: RoundingMode) -> Self
pub fn mul_with_rm(a: Self, b: Self, rm: RoundingMode) -> Self
Compute a*b using the rounding mode rm.
source§impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
sourcepub fn div_with_rm(a: Self, b: Self, rm: RoundingMode) -> Self
pub fn div_with_rm(a: Self, b: Self, rm: RoundingMode) -> Self
Compute a/b, with the rounding mode rm.
source§impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
sourcepub fn from_u64(val: u64) -> Self
pub fn from_u64(val: u64) -> Self
Load the integer val into the float. Notice that the number may
overflow, or rounded to the nearest even integer.
Examples found in repository?
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
fn main() {
// https://en.wikipedia.org/wiki/Chudnovsky_algorithm
let iterations = 5;
// Constants:
let c1 = FP::from_u64(10005).sqrt();
let c2 = FP::from_u64(545140134);
let c3 = FP::from_i64(-262537412640768000);
let c16 = FP::from_u64(16);
let c12 = FP::from_u64(12);
// Initial state.
let mut kc = FP::from_u64(6);
let mut m = FP::from_u64(1);
let mut l = FP::from_u64(13591409);
let mut x = FP::from_u64(1);
let mut s = FP::from_u64(13591409);
for q in 1..iterations + 1 {
let q3 = FP::from_u64(q * q * q);
let k3 = kc * kc * kc;
m = (k3 - (kc * c16)) * m / q3;
l = l + c2;
x = x * c3;
s = s + (m * l / x);
kc = kc + c12;
}
let pi = FP::from_u64(426880) * (c1 / s);
println!("pi = {}", pi);
assert_eq!(pi.as_f64(), std::f64::consts::PI);
}sourcepub fn from_i64(val: i64) -> Self
pub fn from_i64(val: i64) -> Self
Load the integer val into the float. Notice that the number may
overflow, or rounded to the nearest even integer.
Examples found in repository?
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
fn main() {
// https://en.wikipedia.org/wiki/Chudnovsky_algorithm
let iterations = 5;
// Constants:
let c1 = FP::from_u64(10005).sqrt();
let c2 = FP::from_u64(545140134);
let c3 = FP::from_i64(-262537412640768000);
let c16 = FP::from_u64(16);
let c12 = FP::from_u64(12);
// Initial state.
let mut kc = FP::from_u64(6);
let mut m = FP::from_u64(1);
let mut l = FP::from_u64(13591409);
let mut x = FP::from_u64(1);
let mut s = FP::from_u64(13591409);
for q in 1..iterations + 1 {
let q3 = FP::from_u64(q * q * q);
let k3 = kc * kc * kc;
m = (k3 - (kc * c16)) * m / q3;
l = l + c2;
x = x * c3;
s = s + (m * l / x);
kc = kc + c12;
}
let pi = FP::from_u64(426880) * (c1 / s);
println!("pi = {}", pi);
assert_eq!(pi.as_f64(), std::f64::consts::PI);
}sourcepub fn to_i64(&self, rm: RoundingMode) -> i64
pub fn to_i64(&self, rm: RoundingMode) -> i64
Converts and returns the rounded integral part.
sourcepub fn trunc(&self) -> Self
pub fn trunc(&self) -> Self
Returns a value that is rounded to the nearest integer that’s not larger in magnitude than this float.
sourcepub fn cast_with_rm<const E: usize, const M: usize>(
&self,
rm: RoundingMode
) -> Float<E, M>
pub fn cast_with_rm<const E: usize, const M: usize>(
&self,
rm: RoundingMode
) -> Float<E, M>
Cast to another float using the rounding mode rm.
sourcepub fn cast<const E: usize, const M: usize>(&self) -> Float<E, M>
pub fn cast<const E: usize, const M: usize>(&self) -> Float<E, M>
Convert from one float format to another.
pub fn as_f32(&self) -> f32
sourcepub fn as_f64(&self) -> f64
pub fn as_f64(&self) -> f64
Examples found in repository?
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
fn main() {
// https://en.wikipedia.org/wiki/Chudnovsky_algorithm
let iterations = 5;
// Constants:
let c1 = FP::from_u64(10005).sqrt();
let c2 = FP::from_u64(545140134);
let c3 = FP::from_i64(-262537412640768000);
let c16 = FP::from_u64(16);
let c12 = FP::from_u64(12);
// Initial state.
let mut kc = FP::from_u64(6);
let mut m = FP::from_u64(1);
let mut l = FP::from_u64(13591409);
let mut x = FP::from_u64(1);
let mut s = FP::from_u64(13591409);
for q in 1..iterations + 1 {
let q3 = FP::from_u64(q * q * q);
let k3 = kc * kc * kc;
m = (k3 - (kc * c16)) * m / q3;
l = l + c2;
x = x * c3;
s = s + (m * l / x);
kc = kc + c12;
}
let pi = FP::from_u64(426880) * (c1 / s);
println!("pi = {}", pi);
assert_eq!(pi.as_f64(), std::f64::consts::PI);
}pub fn from_f32(float: f32) -> Self
pub fn from_f64(float: f64) -> Self
source§impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
sourcepub fn new(sign: bool, exp: i64, mantissa: BigInt<8>) -> Self
pub fn new(sign: bool, exp: i64, mantissa: BigInt<8>) -> Self
Create a new normal floating point number.
sourcepub fn raw(sign: bool, exp: i64, mantissa: BigInt<8>, category: Category) -> Self
pub fn raw(sign: bool, exp: i64, mantissa: BigInt<8>, category: Category) -> Self
Create a new normal floating point number.
sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if the Float is negative
sourcepub fn set_sign(&mut self, sign: bool)
pub fn set_sign(&mut self, sign: bool)
Update the sign of the float to sign. True means negative.
sourcepub fn get_mantissa(&self) -> BigInt<8>
pub fn get_mantissa(&self) -> BigInt<8>
Returns the mantissa of the float.
sourcepub fn get_category(&self) -> Category
pub fn get_category(&self) -> Category
Returns the category of the float.
sourcepub fn get_exp_bounds() -> (i64, i64)
pub fn get_exp_bounds() -> (i64, i64)
Returns the upper and lower bounds of the exponent.
source§impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA>
sourcepub fn sqrt(&self) -> Self
pub fn sqrt(&self) -> Self
Calculate the square root of the number using the Newton Raphson
Examples found in repository?
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
fn main() {
// https://en.wikipedia.org/wiki/Chudnovsky_algorithm
let iterations = 5;
// Constants:
let c1 = FP::from_u64(10005).sqrt();
let c2 = FP::from_u64(545140134);
let c3 = FP::from_i64(-262537412640768000);
let c16 = FP::from_u64(16);
let c12 = FP::from_u64(12);
// Initial state.
let mut kc = FP::from_u64(6);
let mut m = FP::from_u64(1);
let mut l = FP::from_u64(13591409);
let mut x = FP::from_u64(1);
let mut s = FP::from_u64(13591409);
for q in 1..iterations + 1 {
let q3 = FP::from_u64(q * q * q);
let k3 = kc * kc * kc;
m = (k3 - (kc * c16)) * m / q3;
l = l + c2;
x = x * c3;
s = s + (m * l / x);
kc = kc + c12;
}
let pi = FP::from_u64(426880) * (c1 / s);
println!("pi = {}", pi);
assert_eq!(pi.as_f64(), std::f64::consts::PI);
}Trait Implementations§
source§impl<const EXPONENT: usize, const MANTISSA: usize> Add<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Add<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
source§impl<const EXPONENT: usize, const MANTISSA: usize> Div<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Div<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
source§impl<const EXPONENT: usize, const MANTISSA: usize> Mul<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> Mul<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
source§impl<const EXPONENT: usize, const MANTISSA: usize> PartialEq<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> PartialEq<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
source§impl<const EXPONENT: usize, const MANTISSA: usize> PartialOrd<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
impl<const EXPONENT: usize, const MANTISSA: usize> PartialOrd<Float<EXPONENT, MANTISSA>> for Float<EXPONENT, MANTISSA>
Page 66. Chapter 3. Floating-Point Formats and Environment Table 3.8: Comparison predicates and the four relations. and IEEE 754-2019 section 5.10 - totalOrder.
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more