Skip to main content

zenith_float_num/
lib.rs

1//! zenith-float implements arbitrary-precision software floating-point numbers.
2
3#![cfg_attr(not(feature = "std"), no_std)]
4#![deny(missing_docs)]
5#![deny(unused)]
6#![deny(clippy::suspicious)]
7#![deny(clippy::float_arithmetic)]
8#![allow(clippy::manual_div_ceil)]
9#![allow(clippy::manual_is_multiple_of)]
10#![allow(clippy::neg_cmp_op_on_partial_ord)]
11#![allow(clippy::len_zero)]
12#![allow(clippy::slow_vector_initialization)]
13#![allow(clippy::comparison_chain)]
14#![allow(clippy::collapsible_else_if)]
15#![allow(clippy::collapsible_if)]
16
17extern crate alloc;
18
19mod ball;
20mod binfmt;
21mod chebyshev;
22mod common;
23mod complex;
24mod complex_airy;
25mod complex_bessel;
26mod complex_ei;
27mod complex_elliptic;
28mod complex_hypergeom;
29mod complex_special;
30mod conv;
31mod csvfmt;
32pub mod ctx;
33mod defs;
34mod dist;
35mod dsp;
36mod ext;
37mod hash;
38mod ieee_soft;
39mod integer;
40mod mantissa;
41mod modular;
42mod num;
43mod ode;
44mod ops;
45mod orthopoly;
46mod parser;
47mod poly;
48mod quadrature;
49mod radix_float;
50#[cfg(any(test, feature = "random"))]
51mod random_dist;
52mod rational;
53mod roots;
54mod strop;
55
56#[cfg(feature = "std")]
57mod for_3rd;
58
59#[doc(hidden)]
60pub mod macro_util;
61
62pub use crate::ball::ziv_round;
63pub use crate::ball::ziv_round_vec;
64pub use crate::ball::Ball;
65pub use crate::ball::ComplexBall;
66pub use crate::binfmt::InlineBinaryBuffer;
67pub use crate::binfmt::BINARY_FLAG_ARRAY;
68pub use crate::binfmt::BINARY_FLAG_HEAP_NEG;
69pub use crate::binfmt::BINARY_FLAG_HEAP_POS;
70pub use crate::binfmt::BINARY_FLAG_INF_NEG;
71pub use crate::binfmt::BINARY_FLAG_INF_POS;
72pub use crate::binfmt::BINARY_FLAG_NAN_BARE;
73pub use crate::binfmt::BINARY_FLAG_NAN_DIV0;
74pub use crate::binfmt::BINARY_FORMAT_VERSION;
75pub use crate::binfmt::BINARY_HEADER_LEN;
76pub use crate::binfmt::BINARY_INLINE_LEN;
77pub use crate::binfmt::BINARY_INLINE_MANT_BITS;
78pub use crate::binfmt::BINARY_INLINE_U32_WORDS;
79pub use crate::binfmt::BINARY_MAX_ELEMS;
80pub use crate::binfmt::BINARY_MAX_U32;
81pub use crate::chebyshev::chebyshev_coeffs;
82pub use crate::chebyshev::chebyshev_error_bound;
83pub use crate::chebyshev::chebyshev_eval;
84pub use crate::chebyshev::clenshaw;
85pub use crate::chebyshev::CHEBYSHEV_MAX_DEGREE;
86pub use crate::common::buf::INLINE_WORDS;
87pub use crate::complex::ExactComplex;
88pub use crate::defs::Error;
89pub use crate::defs::Exponent;
90pub use crate::defs::Radix;
91pub use crate::defs::RoundingMode;
92pub use crate::defs::Sign;
93pub use crate::defs::Word;
94pub use crate::dsp::blackman_window;
95pub use crate::dsp::dct;
96pub use crate::dsp::dst;
97pub use crate::dsp::fft_real;
98pub use crate::dsp::hamming_window;
99pub use crate::dsp::hann_window;
100pub use crate::dsp::idct;
101pub use crate::dsp::idst;
102pub use crate::dsp::ifft_real;
103pub use crate::dsp::kaiser_window;
104pub use crate::dsp::rectangular_window;
105pub use crate::dsp::DSP_MAX_POINTS;
106pub use crate::ext::ExactNum;
107pub use crate::ext::FromExt;
108pub use crate::ext::INF_NEG;
109pub use crate::ext::INF_POS;
110pub use crate::ext::NAN;
111pub use crate::hash::constant_time_eq;
112pub use crate::hash::hmac_sha256;
113pub use crate::hash::sha256;
114pub use crate::hash::sha512;
115pub use crate::ieee_soft::{
116    ExactNumArray, Ieee32, Ieee32Array, Ieee64, Ieee64Array, IEEE_SIMD_LANE_WIDTH,
117};
118pub use crate::integer::ExactInt;
119pub use crate::modular::miller_rabin;
120pub use crate::modular::mod_inv;
121pub use crate::modular::mod_pow;
122pub use crate::modular::pollard_rho;
123pub use crate::modular::POLLARD_RHO_ITER_MAX;
124pub use crate::ode::euler;
125pub use crate::ode::ode_min_step;
126pub use crate::ode::rk4;
127pub use crate::ode::rk45_adaptive;
128pub use crate::ode::ODE_MAX_STEPS;
129pub use crate::ode::ODE_MIN_STEP;
130pub use crate::ops::consts::CachedFBig;
131pub use crate::ops::consts::ConstCache;
132pub use crate::ops::consts::ConstCacheInfo;
133pub use crate::ops::JACOBI_AGM_MAX;
134pub use crate::ops::consts::Consts;
135#[cfg(feature = "std")]
136pub use crate::ops::consts::SharedConsts;
137pub use crate::orthopoly::ORTHOPOLY_N_MAX;
138pub use crate::poly::ExactNumPoly;
139pub use crate::poly::POLY_COMPANION_CLOSED_DEG;
140pub use crate::quadrature::gauss_hermite;
141pub use crate::quadrature::gauss_laguerre;
142pub use crate::quadrature::gauss_legendre;
143pub use crate::quadrature::tanh_sinh;
144pub use crate::quadrature::QUADRATURE_MAX_NODES;
145pub use crate::quadrature::TANH_SINH_LEVELS_MAX;
146pub use crate::radix_float::RadixFloat;
147#[cfg(any(test, feature = "random"))]
148pub use crate::random_dist::RandomDist;
149pub use crate::rational::ExactRational;
150pub use crate::roots::bisect;
151pub use crate::roots::brent;
152pub use crate::roots::illinois;
153pub use crate::roots::newton;
154pub use crate::roots::root_default_tol;
155pub use crate::roots::ROOT_DEFAULT_TOL;
156pub use crate::roots::ROOT_MAX_ITER;
157
158pub use crate::defs::PROPTEST_CASES;
159pub use crate::defs::EXPONENT_BIT_SIZE;
160pub use crate::defs::EXPONENT_MAX;
161pub use crate::defs::EXPONENT_MIN;
162pub use crate::defs::WORD_BASE;
163pub use crate::defs::WORD_BIT_SIZE;
164pub use crate::defs::WORD_MAX;
165pub use crate::defs::WORD_SIGNIFICANT_BIT;
166
167pub use crate::common::util::MAX_PREC_RETRY;
168pub use crate::csvfmt::CSV_MAX_COLS;
169pub use crate::csvfmt::CSV_MAX_ROWS;
170
171#[cfg(feature = "random")]
172pub use crate::common::test_rng::{random_seed, reseed_random, seeded_random, DEFAULT_RANDOM_SEED};
173
174#[cfg(test)]
175mod tests {
176
177    #[test]
178    fn test_bigfloat() {
179        use crate::Consts;
180        use crate::ExactNum;
181        use crate::RoundingMode;
182
183        // Precision with some space for error.
184        let p = 1024 + 8;
185
186        // Rounding of all operations
187        let rm = RoundingMode::ToEven;
188
189        // Initialize mathematical constants cache
190        let mut cc = Consts::new().expect("An error occured when initializing constants");
191
192        // Compute pi: pi = 6*arctan(1/sqrt(3))
193        let six = ExactNum::from_word(6, 1);
194        let three = ExactNum::from_word(3, p);
195
196        let n = three.sqrt(p, rm);
197        let n = n.reciprocal(p, rm);
198        let n = n.atan(p, rm, &mut cc);
199        let mut pi = six.mul(&n, p, rm);
200
201        // Reduce precision to 1024
202        pi.set_precision(1024, rm).expect("Precision updated");
203
204        // Use library's constant for verifying the result
205        let pi_lib = cc.pi_num(1024, rm).unwrap().into();
206
207        // Compare computed constant with library's constant
208        assert_eq!(pi.cmp(&pi_lib), Some(0));
209
210        // Print using decimal radix.
211        //println!("{}", pi);
212    }
213}