const_soft_float/
lib.rs

1//! # Rust float-point in constant context
2//!
3//! Features:
4//! * `no_std`
5//! * `const_trait_impl`
6//! * `const_mut_refs`
7//!
8//! work in `stable`:
9//! ```
10//! # use const_soft_float::soft_f32::SoftF32;
11//! const fn const_f32_add(a: f32, b: f32) -> f32 {
12//!     SoftF32(a).add(SoftF32(b)).to_f32()
13//! }
14//! ```
15//!
16//!
17//! with `const_trait_impl` usage (requires `nightly`):
18//! ```
19//! # cfg_if::cfg_if! {
20//! # if #[cfg(nightly)] {
21//! # #![feature(const_trait_impl)]
22//! # use const_soft_float::soft_f32::SoftF32;
23//! const fn const_f32_add(a: f32, b: f32) -> f32 {
24//!     (SoftF32(a) + SoftF32(b)).to_f32()
25//! }
26//! # }
27//! # }
28//! ```
29//!
30//! with `const_mut_refs` usage (requires `nightly`):
31//! ```
32//! # cfg_if::cfg_if! {
33//! # if #[cfg(nightly)] {
34//! # #![feature(const_trait_impl)]
35//! # #![feature(const_mut_refs)]
36//! # use const_soft_float::soft_f32::SoftF32;
37//! const fn const_f32_add(a: f32, b: f32) -> f32 {
38//!     let mut x = SoftF32(a);
39//!     x += SoftF32(b);
40//!     x.to_f32()
41//! }
42//! # }
43//! # }
44//! ```
45//!
46//!
47
48#![cfg_attr(feature = "no_std", no_std)]
49#![cfg_attr(feature = "const_trait_impl", feature(const_trait_impl))]
50#![cfg_attr(feature = "const_mut_refs", feature(const_mut_refs))]
51
52pub mod soft_f32;
53pub mod soft_f64;
54
55const fn abs_diff(a: i32, b: i32) -> u32 {
56    a.wrapping_sub(b).wrapping_abs() as u32
57}
58
59#[cfg(test)]
60mod tests {
61    use crate::soft_f32::SoftF32;
62    use crate::soft_f64::SoftF64;
63
64    const RANGE: core::ops::Range<i32> = -1000..1000;
65    const F32_FACTOR: f32 = 10.0;
66    const F64_FACTOR: f64 = 1000.0;
67
68    #[test]
69    fn f32_add() {
70        for a in RANGE {
71            let a = a as f32 * F32_FACTOR;
72            for b in RANGE {
73                let b = b as f32 * F32_FACTOR;
74                assert_eq!(SoftF32(a).add(SoftF32(b)).0, a + b);
75            }
76        }
77    }
78
79    #[test]
80    fn f32_sub() {
81        for a in RANGE {
82            let a = a as f32 * F32_FACTOR;
83            for b in RANGE {
84                let b = b as f32 * F32_FACTOR;
85                assert_eq!(SoftF32(a).sub(SoftF32(b)).0, a - b);
86            }
87        }
88    }
89
90    #[test]
91    fn f32_mul() {
92        for a in RANGE {
93            let a = a as f32 * F32_FACTOR;
94            for b in RANGE {
95                let b = b as f32 * F32_FACTOR;
96                assert_eq!(SoftF32(a).mul(SoftF32(b)).0, a * b);
97            }
98        }
99    }
100
101    #[test]
102    fn f32_div() {
103        for a in RANGE {
104            let a = a as f32 * F32_FACTOR;
105            for b in RANGE {
106                let b = b as f32 * F32_FACTOR;
107                let x = SoftF32(a).div(SoftF32(b)).0;
108                let y = a / b;
109                assert!(x == y || x.is_nan() && y.is_nan())
110            }
111        }
112    }
113
114    #[test]
115    fn f64_add() {
116        for a in RANGE {
117            let a = a as f64 * F64_FACTOR;
118            for b in RANGE {
119                let b = b as f64 * F64_FACTOR;
120                assert_eq!(SoftF64(a).sub(SoftF64(b)).0, a - b);
121            }
122        }
123    }
124
125    #[test]
126    fn f64_sub() {
127        for a in RANGE {
128            let a = a as f64 * F64_FACTOR;
129            for b in RANGE {
130                let b = b as f64 * F64_FACTOR;
131                assert_eq!(SoftF64(a).sub(SoftF64(b)).0, a - b);
132            }
133        }
134    }
135
136    #[test]
137    fn f64_mul() {
138        for a in RANGE {
139            let a = a as f64 * F64_FACTOR;
140            for b in RANGE {
141                let b = b as f64 * F64_FACTOR;
142                assert_eq!(SoftF64(a).mul(SoftF64(b)).0, a * b);
143            }
144        }
145    }
146
147    #[test]
148    fn f64_div() {
149        for a in RANGE {
150            let a = a as f64 * F64_FACTOR;
151            for b in RANGE {
152                let b = b as f64 * F64_FACTOR;
153                let x = SoftF64(a).div(SoftF64(b)).0;
154                let y = a / b;
155                assert!(x == y || x.is_nan() && y.is_nan())
156            }
157        }
158    }
159}