#decimal-number #floating-point #big-decimal #decimal #arbitrary-precision

no-std dashu-float

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

9 releases

0.4.3 Feb 3, 2024
0.4.1 Dec 17, 2023
0.4.0 Sep 12, 2023
0.3.2 Mar 4, 2023
0.3.0 Nov 10, 2022

#730 in Math

Download history 409/week @ 2024-07-20 402/week @ 2024-07-27 508/week @ 2024-08-03 482/week @ 2024-08-10 1040/week @ 2024-08-17 1319/week @ 2024-08-24 1009/week @ 2024-08-31 1549/week @ 2024-09-07 1552/week @ 2024-09-14 2529/week @ 2024-09-21 5483/week @ 2024-09-28 6189/week @ 2024-10-05 4613/week @ 2024-10-12 4151/week @ 2024-10-19 2402/week @ 2024-10-26 3482/week @ 2024-11-02

15,581 downloads per month
Used in 41 crates (4 directly)

MIT/Apache

1MB
20K SLoC

dashu-float

Arbitrary precision floating point number implementation as a part of the dashu library. See Docs.rs for the full documentation.

Features

  • Supports no_std and written in pure Rust.
  • Support arbitrary base and arbitrary rounding mode.
  • Support efficient base conversion.
  • Small float numbers are inlined on stack.
  • Efficient float number parsing and printing with base 2~36.
  • Supports the hexadecimal float format used by C++.
  • Developer friendly debug printing for float numbers.

Optional dependencies

  • std (default): enable std support for dependencies.

Performance

Relevant benchmark will be implemented in the built-in benchmark.

License

See the top-level readme.


lib.rs:

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.

Examples

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.

Dependencies

~0.3–1.7MB
~35K SLoC