dashu_float/lib.rs
1// Copyright (c) 2022 Jacob Zhong
2//
3// Licensed under either of
4//
5// * Apache License, Version 2.0
6// (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
7// * MIT license
8// (LICENSE-MIT or https://opensource.org/licenses/MIT)
9//
10// at your option.
11//
12// Unless you explicitly state otherwise, any contribution intentionally submitted
13// for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
14// dual licensed as above, without any additional terms or conditions.
15
16//! A big float library supporting arbitrary precision, arbitrary base and arbitrary rounding mode.
17//!
18//! The library implements efficient large floating point arithmetic in pure Rust.
19//!
20//! The main type is [FBig] representing the arbitrary precision floating point numbers, the [DBig] type
21//! is an alias supporting decimal floating point numbers.
22//!
23//! To construct big floats from literals, please use the [`dashu-macro`](https://docs.rs/dashu-macros/latest/dashu_macros/)
24//! crate for your convenience.
25//!
26//! # Examples
27//!
28//! ```
29//! # use dashu_base::ParseError;
30//! use core::str::FromStr;
31//! use core::convert::TryFrom;
32//! use dashu_float::DBig;
33//!
34//! // due to the limit of rust generics, the default float type
35//! // need to be instantiate explicitly
36//! type FBig = dashu_float::FBig;
37//!
38//! let a = FBig::try_from(-12.34_f32).unwrap();
39//! let b = DBig::from_str("6.022e23")?;
40//! let c = DBig::from_parts(271828.into(), -5);
41//! let d: DBig = "-0.0123456789".parse()?;
42//! let e = 2 * b.ln() + DBig::ONE;
43//! let f = &c * d.powi(10.into()) / 7;
44//!
45//! assert_eq!(a.precision(), 24); // IEEE 754 single has 24 significant bits
46//! assert_eq!(b.precision(), 4); // 4 decimal digits
47//!
48//! assert!(b > c); // comparison is limited in the same base
49//! assert!(a.to_decimal().value() < d);
50//! assert_eq!(c.to_string(), "2.71828");
51//!
52//! // use associated functions of the context to get full result
53//! use dashu_base::Approximation::*;
54//! use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
55//! let ctxt = Context::<HalfAway>::new(6);
56//! assert_eq!(ctxt.exp(DBig::ONE.repr()), Inexact(c, NoOp));
57//! # Ok::<(), ParseError>(())
58//! ```
59//!
60//! # Optional dependencies
61//!
62//! * `std` (*default*): enable `std` for dependencies.
63
64#![cfg_attr(not(feature = "std"), no_std)]
65
66mod add;
67mod cmp;
68mod convert;
69mod div;
70mod error;
71mod exp;
72mod fbig;
73mod fmt;
74mod helper_macros;
75mod iter;
76mod log;
77mod mul;
78pub mod ops;
79mod parse;
80mod repr;
81mod root;
82pub mod round;
83mod round_ops;
84mod shift;
85mod sign;
86mod third_party;
87mod utils;
88
89// All the public items from third_party will be exposed
90#[allow(unused_imports)]
91pub use third_party::*;
92
93pub use fbig::FBig;
94pub use repr::{Context, Repr};
95
96/// Multi-precision float number with decimal exponent and [HalfAway][round::mode::HalfAway] rounding mode
97pub type DBig = FBig<round::mode::HalfAway, 10>;
98
99#[doc(hidden)]
100pub use dashu_int::Word; // for macros
101
102// TODO: allow operations with inf, but only panic when the result is nan (inf - inf and inf / inf)
103// for division with zero (and other functions that has different limits at zero),
104// we might forbidden it because we don't want to support negative zero in this library.