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(), None), Ok(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#![deny(missing_docs)]
66#![deny(clippy::dbg_macro)]
67#![deny(clippy::undocumented_unsafe_blocks)]
68#![deny(clippy::let_underscore_must_use)]
69
70extern crate alloc;
71
72mod add;
73mod cmp;
74mod convert;
75mod div;
76mod error;
77mod exp;
78mod fbig;
79mod fbig_cached;
80mod fbig_cached_ops;
81mod fmt;
82mod helper_macros;
83mod iter;
84mod log;
85pub mod math;
86mod mul;
87pub mod ops;
88mod parse;
89mod repr;
90mod root;
91pub mod round;
92mod round_ops;
93mod shift;
94mod sign;
95mod third_party;
96mod utils;
97
98// All the public items from third_party will be exposed
99#[allow(unused_imports)]
100pub use third_party::*;
101
102pub use fbig::FBig;
103pub use fbig_cached::CachedFBig;
104pub use math::cache::ConstCache;
105pub use repr::{Context, Repr};
106pub use {crate::error::FpError, crate::math::FpResult};
107
108/// Multi-precision float number with decimal exponent and [HalfAway][round::mode::HalfAway] rounding mode
109pub type DBig = FBig<round::mode::HalfAway, 10>;
110
111#[doc(hidden)]
112pub use dashu_int::Word; // for macros
113
114// Infinities are now first-class *terminal* values: operations produce them as results
115// (1/0 → +inf, ln(0) → -inf, …) but reject infinite *inputs* (FpError::InfiniteInput / panic),
116// which structurally avoids the NaN-producing indeterminate forms (inf−inf, inf/inf, 0·inf).
117// IEEE signed zero is supported (`-0`), so 1/-0 = -inf is well-defined.