Scientific! in the crate
scientific-macro to create constant numbers.
use scientific_macro::Scientific; let n1 = Scientific!(1e100); let n2 = Scientific!(1e80); assert_eq!(&n1 + &n2, Scientific!(1.00000000000000000001e100)); // An f64 has only a precision of about 15.9 digits, this are already 21.
All functions expect a reference to the Scientific number. (See example above.)
TryFrom traits for conversion between Scientific and integers, floats and strings.
Converting a number with decimals to an integer will fail.
There is a
FromStr instance (which clones the
str and calls Scientific::from_string).
Most functions work in truly arbitrary precision, please be aware of this.
For example: adding 1e1000 and 1e-1000, which both have only one byte of mantissa, results in 2001 bytes of mantissa.
It can be specified as Decimals or Digits. When using decimals specify the number of decimal places to
0.01 as the smallest number,
100). When using digits specify
the number of digits in the mantissa (using <= 0 digits will always result in zero).
The shifting operators do shift by one digit (and not one bit as you may expected).
serde: Enable De-/Serialization with serde.
std: If activated the library requires
stdand the std::error::Error trait is implemented for all error types. Without it the library is
arc: Use of Arc instead of Rc, which enables Send and Sync for Scientific. Though Arc is more expensive, but since it’s only used during create/clone/drop of the Scientific number it’s probably not that much.
debug: Enabled tracking of pointer operations and some more checks. Very helpful during development of this lib.
The exponent is represented as an isize. It is expected that it will never under-/overflow, even when smaller numbers are added/subtracted, like e.g. the length of the mantissa.
This is not checked!
pub use crate::Precision::Decimals;
pub use crate::Precision::Digits;
pub use crate::Round::RoundAwayFromZero;
pub use crate::Round::RoundDown;
pub use crate::Round::RoundHalfAwayFromZero;
pub use crate::Round::RoundHalfDown;
pub use crate::Round::RoundHalfToEven;
pub use crate::Round::RoundHalfToOdd;
pub use crate::Round::RoundHalfTowardsZero;
pub use crate::Round::RoundHalfUp;
pub use crate::Round::RoundUp;
Arbitrary precision scientific number
Also known as round towards zero, please see Rounding for an overview.
Trait to implement different rounding methods.