astro_float/lib.rs
1//! Astro-float (astronomically large floating point numbers) is a library that implements arbitrary precision floating point numbers.
2//!
3//! ## Introduction
4//!
5//! **Numbers**
6//!
7//!
8//! The number is defined by the data type `BigFloat`.
9//! Each finite number consists of an array of words representing the mantissa, exponent, and sign.
10//! `BigFloat` can also be `Inf` (positive infinity), `-Inf` (negative infinity) or `NaN` (not-a-number).
11//!
12//!
13//! `BigFloat` creation operations take bit precision as an argument.
14//! Precision is always rounded up to the nearest word.
15//! For example, if you specify a precision of 1 bit, then it will be converted to 64 bits when one word has a size of 64 bits.
16//! If you specify a precision of 65 bits, the resulting precision will be 128 bits (2 words), and so on.
17//!
18//!
19//! Most operations take the rounding mode as an argument.
20//! The operation will typically internally result in a number with more precision than necessary.
21//! Before the result is returned to the user, the result is rounded according to the rounding mode and reduced to the expected precision.
22//!
23//!
24//! The result of an operation is marked as inexact if some of the bits were rounded when producing the result,
25//! or if any of the operation's arguments were marked as inexact. The information about exactness is used to achieve correct rounding.
26//!
27//!
28//! `BigFloat` can be parsed from a string and formatted into a string using binary, octal, decimal, or hexadecimal representation.
29//!
30//!
31//! Numbers can be subnormal. Usually any number is normalized: the most significant bit of the mantissa is set to 1.
32//! If the result of the operation has the smallest possible exponent, then normalization cannot be performed,
33//! and some significant bits of the mantissa may become 0. This allows for a more gradual transition to zero.
34//!
35//! **Error handling**
36//!
37//! In case of an error, such as memory allocation error, `BigFloat` takes the value `NaN`.
38//! `BigFloat::err()` can be used to get the associated error in this situation.
39//!
40//! **Constants**
41//!
42//! Constants such as pi or the Euler number have arbitrary precision and are evaluated lazily and then cached in the constants cache.
43//! Some functions expect constants cache as parameter.
44//!
45//! **Correctness**
46//!
47//! Results of all arithmetic operations, mathematical functions, radix conversion, and constant values are correctly rounded
48//! (a correctly rounded number is a number that is identical to a number computed to infinite precision and then rounded, reflecting no information loss during rounding).
49//!
50//! ## Examples
51//!
52//! The example below computes value of Pi with precision 1024 rounded to the nearest even number using `expr!` macro.
53//! Macro simplifies syntax and takes care of the error.
54//!
55//! ```
56//! use astro_float::Consts;
57//! use astro_float::RoundingMode;
58//! use astro_float::ctx::Context;
59//! use astro_float::expr;
60//!
61//! // Create a context with precision 1024, rounding to the nearest even,
62//! // and exponent range from -100000 to 100000.
63//! let mut ctx = Context::new(1024, RoundingMode::ToEven,
64//! Consts::new().expect("Constants cache initialized"),
65//! -100000, 100000);
66//!
67//! // Compute pi: pi = 6*arctan(1/sqrt(3))
68//! let pi = expr!(6 * atan(1 / sqrt(3)), &mut ctx);
69//!
70//! // Use library's constant value for verifying the result.
71//! let pi_lib = ctx.const_pi();
72//!
73//! // Compare computed constant with library's constant
74//! assert_eq!(pi.cmp(&pi_lib), Some(0));
75//!
76//! // Print using decimal radix.
77//! #[cfg(feature="std")]
78//! println!("{}", pi);
79//!
80//! // output: 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458699748e+0
81//! ```
82//!
83//! The example below computes value of Pi with precision 1024 rounded to the nearest even number using `BigFloat` directly.
84//! We will take care of the error in this case.
85//!
86//! ``` rust
87//! use astro_float::BigFloat;
88//! use astro_float::Consts;
89//! use astro_float::RoundingMode;
90//!
91//! // Precision with some space for error.
92//! let p = 1024 + 8;
93//!
94//! // The results of computations will not be rounded.
95//! // That will be more performant, even though it may give an incorrectly rounded result.
96//! let rm = RoundingMode::None;
97//!
98//! // Initialize mathematical constants cache
99//! let mut cc = Consts::new().expect("An error occured when initializing constants");
100//!
101//! // Compute pi: pi = 6*arctan(1/sqrt(3))
102//! let six = BigFloat::from_word(6, 1);
103//! let three = BigFloat::from_word(3, p);
104//!
105//! let n = three.sqrt(p, rm);
106//! let n = n.reciprocal(p, rm);
107//! let n = n.atan(p, rm, &mut cc);
108//! let mut pi = six.mul(&n, p, rm);
109//!
110//! // Reduce precision to 1024 and round to the nearest even number.
111//! pi.set_precision(1024, RoundingMode::ToEven).expect("Precision updated");
112//!
113//! // Use library's constant for verifying the result
114//! let pi_lib = cc.pi(1024, RoundingMode::ToEven);
115//!
116//! // Compare computed constant with library's constant
117//! assert_eq!(pi.cmp(&pi_lib), Some(0));
118//!
119//! // Print using decimal radix.
120//! #[cfg(feature="std")]
121//! println!("{}", pi);
122//!
123//! // output: 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458699748e+0
124//! ```
125//!
126//! ## Performance recommendations
127//!
128//! When small error is acceptable because of rounding it is recommended to do all computations with `RoundingMode::None`, and use `BigFloat::set_precision` or `BigFloat::round` with a specific rounding mode just once for the final result.
129//!
130//! ## no_std
131//!
132//! The library can work without the standard library provided there is a memory allocator. The standard library dependency is activated by the feature `std`.
133//! The feature `std` is active by default and must be excluded when specifying dependency, e.g.:
134//!
135//! ``` toml
136//! [dependencies]
137//! astro-float = { version = "0.9.2", default-features = false }
138//! ```
139//!
140
141#![deny(missing_docs)]
142#![deny(clippy::suspicious)]
143#![cfg_attr(not(feature = "std"), no_std)]
144
145#[cfg(not(feature = "std"))]
146extern crate alloc;
147
148/// Computes an expression with the specified precision and rounding mode.
149///
150/// Macro takes into account 2 aspects.
151///
152/// 1. Code simplification. Macro simplifies code and improves its readability by allowing to specify simple and concise expression
153/// and process input arguments transparently.
154///
155/// 2. Error compensation. Macro compensates error caused by [catastrophic cancellation](https://en.wikipedia.org/wiki/Catastrophic_cancellation)
156/// and some other situations where precision can be lost by automatically increasing the working precision internally.
157///
158/// The macro does not take care of correct rounding, because the completion of the rounding algorithm in finite time depends on the macro's input.
159///
160/// The macro accepts an expression to compute and a context.
161/// The expression can include:
162///
163/// - Path expressions: variable names, constant names, etc.
164/// - Integer literals, e.g. `123`, `-5`.
165/// - Floating point literals, e.g. `1.234e-567`.
166/// - String literals, e.g. `"-1.234_e-567"`.
167/// - Binary operators.
168/// - Unary `-` operator.
169/// - Mathematical functions.
170/// - Grouping with `(` and `)`.
171/// - Constants `pi`, `e`, `ln_2`, and `ln_10`.
172///
173/// Binary operators:
174///
175/// - `+`: addition.
176/// - `-`: subtraction.
177/// - `*`: multiplication.
178/// - `/`: division.
179/// - `%`: modular division.
180///
181/// Mathematical functions:
182///
183/// - `recip(x)`: reciprocal of `x`.
184/// - `sqrt(x)`: square root of `x`.
185/// - `cbrt(x)`: cube root of `x`.
186/// - `ln(x)`: natural logarithm of `x`.
187/// - `log2(x)`: logarithm base 2 of `x`.
188/// - `log10(x)`: logarithm base 10 of `x`.
189/// - `log(x, b)`: logarithm with base `b` of `x`.
190/// - `exp(x)`: `e` to the power of `x`.
191/// - `pow(b, x)`: `b` to the power of `x`.
192/// - `sin(x)`: sine of `x`.
193/// - `cos(x)`: cosine of `x`.
194/// - `tan(x)`: tangent of `x`.
195/// - `asin(x)`: arcsine of `x`.
196/// - `acos(x)`: arccosine of `x`.
197/// - `atan(x)`: arctangent of `x`.
198/// - `sinh(x)`: hyperbolic sine of `x`.
199/// - `cosh(x)`: hyperbolic cosine of `x`.
200/// - `tanh(x)`: hyperbolic tangent of `x`.
201/// - `asinh(x)`: hyperbolic arcsine of `x`.
202/// - `acosh(x)`: hyperbolic arccosine of `x`.
203/// - `atanh(x)`: hyperbolic arctangent of `x`.
204///
205/// Constants:
206/// - `pi`: pi number.
207/// - `e`: Euler number.
208/// - `ln_2`: natural logarithm of 2.
209/// - `ln_10`: natural logarithm of 10.
210///
211/// The context determines the precision, the rounding mode of the result, and also contains the cache of constants.
212///
213/// Also, the macro uses minimum and maximum exponent values from the context to limit possible exponent range of the result and to set the limit of precision required for error compensation.
214/// It is recommended to set the smallest exponent range to increase the performance of computations (the internal precision may be as large as the exponent of a number).
215///
216/// A tuple `(usize, RoundingMode, &mut Consts)`, or `(usize, RoundingMode, &mut Consts, Exponent, Exponent)` can be used as a temporary context (see examples below).
217///
218/// Any input argument in the expression is interpreted as exact
219/// (i.e. if an argument of an expression has type BigFloat and it is an inexact result of a previous computation).
220///
221/// ## Examples
222///
223/// ```
224/// # use astro_float_macro::expr;
225/// # use astro_float::RoundingMode;
226/// # use astro_float::Consts;
227/// # use astro_float::BigFloat;
228/// # use astro_float::ctx::Context;
229/// // Precision, rounding mode, constants cache, and exponent range.
230/// let p = 128;
231/// let rm = RoundingMode::Up;
232/// let mut cc = Consts::new().expect("Failed to allocate constants cache");
233/// let emin = -10000;
234/// let emax = 10000;
235///
236/// // Create a context.
237/// let mut ctx = Context::new(p, rm, cc, emin, emax);
238///
239/// let x = 123;
240/// let y = "2345";
241/// let z = 234.5e+1;
242///
243/// // Compute an expression.
244/// let ret = expr!(x + y / z - ("120" - 120), &mut ctx);
245///
246/// assert_eq!(ret, BigFloat::from(124));
247///
248/// // Destructure context.
249/// let (p, rm, mut cc, emin, emax) = ctx.to_raw_parts();
250///
251/// // Compute an expression using a temporary context.
252/// let ret = expr!(x + y / z, (p, rm, &mut cc, emin, emax));
253///
254/// assert_eq!(ret, BigFloat::from(124));
255/// ```
256pub use astro_float_macro::expr;
257
258pub use astro_float_num::*;