rustev_core/math.rs
1//! `exp` and `ln` for every pure path (spec 047).
2//!
3//! The platform's math library differs between targets (glibc, musl, Apple,
4//! Microsoft and Rust's own on WebAssembly) in the last bit of some results,
5//! so the pure crates never call `f64::exp` or `f64::ln`. These functions
6//! are one pure Rust implementation, the `libm` crate exact-pinned without
7//! default features, and give the same bits on every target. They are not
8//! correctly rounded, so they stay outside exact computation (spec 002,
9//! 3.4.6); a test (`tests/platform_math.rs`) refuses a direct call.
10
11/// e raised to `x`.
12pub fn exp(x: f64) -> f64 {
13 libm::exp(x)
14}
15
16/// The natural logarithm of `x`.
17pub fn ln(x: f64) -> f64 {
18 libm::log(x)
19}
20
21#[cfg(test)]
22mod tests {
23 use super::*;
24
25 #[test]
26 fn special_values() {
27 assert_eq!(exp(0.0), 1.0);
28 assert_eq!(exp(f64::NEG_INFINITY), 0.0);
29 assert_eq!(exp(-746.0), 0.0);
30 assert_eq!(ln(1.0), 0.0);
31 assert_eq!(ln(0.0), f64::NEG_INFINITY);
32 assert!(ln(-1.0).is_nan());
33 }
34
35 #[test]
36 fn known_bits() {
37 // Pinned results, so a pin change that moves a bit is seen here
38 // before the differential corpus (spec 027, 3.11.3) runs.
39 assert_eq!(exp(-1.0).to_bits(), 0x3fd7_8b56_362c_ef38);
40 assert_eq!(ln(0.5).to_bits(), 0xbfe6_2e42_fefa_39ef);
41 assert_eq!(ln(f64::MIN_POSITIVE).to_bits(), 0xc086_232b_dd7a_bcd2);
42 }
43}