Library for multiple precision floating point numbers

A minimalistic implementation for multiple precision floating point numbers.
Numbers can hold up-to 40 decimal positions with decimal exponent in range -128..127. Crate includes basic functions, like sqrt, pow, ln, sin, cos.

Link: https://crates.io/crates/num-bigfloat

1 Like

Out of curiosity, how does this compare to rust_recimal other than the differences between floats and decimals themselves?

1 Like

Well, you said the difference. Decimal has more limited range of values compared to float.

Here is an example actually of how num-bigfloat is essentially different. Lets calculate sin and cos. Then to verify the precision sin * sin + cos * cos = 1. And here is what we have:

// rust_decimal
pub fn test_decimal() {
    let d1 = Decimal::from_f64_retain(2.100000000000000005551115123).unwrap();
    let s = d1.sin();
    let c = d1.cos();
    println!("{}", s*s + c*c);
}
// prints: 0.9999999999982223922390866348
// num-bigfloat
pub fn test_bigfloat() {
   let d1 = BigFloat::from_f64(2.100000000000000005551115123).unwrap();
   let s = d1.sin().unwrap();
   let c = d1.cos().unwrap();      
   let r = s.mul(&s).unwrap().add(&c.mul(&c).unwrap()).unwrap();
   println!("{:?}", r.to_f64());                                      
}  
// prints: 1.0                                              

And this is not a problem of rust_decimal crate, but is a problem of any decimal numbers in any language, i.e. you can try sin and cos using python's decimals: decimal — Decimal fixed point and floating point arithmetic — Python 3.10.5 documentation
So, if you want to calculate something with good precision use floats, not decimals.