Crate dashu_float

source ·
Expand description

A big float library supporting arbitrary precision, arbitrary base and arbitrary rounding mode.

The library implements efficient large floating point arithmetic in pure Rust.

The main type is FBig representing the arbitrary precision floating point numbers, the DBig type is an alias supporting decimal floating point numbers.

To construct big floats from literals, please use the dashu-macro crate for your convenience.


use core::str::FromStr;
use core::convert::TryFrom;
use dashu_float::DBig;

// due to the limit of rust generics, the default float type
// need to be instantiate explicitly
type FBig = dashu_float::FBig;

let a = FBig::try_from(-12.34_f32).unwrap();
let b = DBig::from_str("6.022e23")?;
let c = DBig::from_parts(271828.into(), -5);
let d: DBig = "-0.0123456789".parse()?;
let e = 2 * b.ln() + DBig::ONE;
let f = &c * d.powi(10.into()) / 7;

assert_eq!(a.precision(), 24); // IEEE 754 single has 24 significant bits
assert_eq!(b.precision(), 4); // 4 decimal digits

assert!(b > c); // comparison is limited in the same base
assert!(a.to_decimal().value() < d);
assert_eq!(c.to_string(), "2.71828");

// use associated functions of the context to get full result
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let ctxt = Context::<HalfAway>::new(6);
assert_eq!(ctxt.exp(DBig::ONE.repr()), Inexact(c, NoOp));

§Optional dependencies

  • std (default): enable std for dependencies.


  • Re-exported relevant operator traits from dashu-base
  • Random floating point number generation with the rand crate.
  • Traits and implementations for rounding during operations.


  • The context containing runtime information for the floating point number and its operations.
  • An arbitrary precision floating point number with arbitrary base and rounding mode.
  • Underlying representation of an arbitrary precision floating number.

Type Aliases§

  • Multi-precision float number with decimal exponent and HalfAway rounding mode