astro_float_num/
lib.rs

1//! Astro-float (astronomically large floating point numbers) is a library that implements arbitrary precision floating point numbers.
2//!
3//! See main crate [docs](https://docs.rs/astro-float/latest/astro_float/).
4
5#![cfg_attr(not(feature = "std"), no_std)]
6#![deny(missing_docs)]
7#![deny(clippy::suspicious)]
8#![allow(clippy::comparison_chain)]
9#![allow(clippy::collapsible_else_if)]
10#![allow(clippy::collapsible_if)]
11
12#[cfg(not(feature = "std"))]
13extern crate alloc;
14
15mod common;
16mod conv;
17pub mod ctx;
18mod defs;
19mod ext;
20mod mantissa;
21mod num;
22mod ops;
23mod parser;
24mod strop;
25
26#[cfg(feature = "std")]
27mod for_3rd;
28
29#[doc(hidden)]
30pub mod macro_util;
31
32pub use crate::defs::Error;
33pub use crate::defs::Exponent;
34pub use crate::defs::Radix;
35pub use crate::defs::RoundingMode;
36pub use crate::defs::Sign;
37pub use crate::defs::Word;
38pub use crate::ext::BigFloat;
39pub use crate::ext::FromExt;
40pub use crate::ext::INF_NEG;
41pub use crate::ext::INF_POS;
42pub use crate::ext::NAN;
43pub use crate::ops::consts::Consts;
44
45pub use crate::defs::EXPONENT_BIT_SIZE;
46pub use crate::defs::EXPONENT_MAX;
47pub use crate::defs::EXPONENT_MIN;
48pub use crate::defs::WORD_BASE;
49pub use crate::defs::WORD_BIT_SIZE;
50pub use crate::defs::WORD_MAX;
51pub use crate::defs::WORD_SIGNIFICANT_BIT;
52
53#[cfg(test)]
54mod tests {
55
56    #[test]
57    fn test_bigfloat() {
58        use crate::BigFloat;
59        use crate::Consts;
60        use crate::RoundingMode;
61
62        // Precision with some space for error.
63        let p = 1024 + 8;
64
65        // Rounding of all operations
66        let rm = RoundingMode::ToEven;
67
68        // Initialize mathematical constants cache
69        let mut cc = Consts::new().expect("An error occured when initializing constants");
70
71        // Compute pi: pi = 6*arctan(1/sqrt(3))
72        let six = BigFloat::from_word(6, 1);
73        let three = BigFloat::from_word(3, p);
74
75        let n = three.sqrt(p, rm);
76        let n = n.reciprocal(p, rm);
77        let n = n.atan(p, rm, &mut cc);
78        let mut pi = six.mul(&n, p, rm);
79
80        // Reduce precision to 1024
81        pi.set_precision(1024, rm).expect("Precision updated");
82
83        // Use library's constant for verifying the result
84        let pi_lib = cc.pi_num(1024, rm).unwrap().into();
85
86        // Compare computed constant with library's constant
87        assert_eq!(pi.cmp(&pi_lib), Some(0));
88
89        // Print using decimal radix.
90        //println!("{}", pi);
91    }
92}