cffdrs/lib.rs
1pub mod fbp_system;
2pub mod fwi_system;
3pub mod weather;
4
5#[cfg(test)]
6pub mod test_util {
7 /// Round to significant digits (rather than digits after the decimal).
8 ///
9 /// Not implemented for `f32`, because such an implementation showed precision
10 /// glitches (e.g. `precision_f32(12300.0, 2) == 11999.999`), so for `f32`
11 /// floats, convert to `f64` for this function and back as needed.
12 ///
13 /// Examples:
14 /// ```
15 /// precision_f64(1.2300, 2) // 1.2<f64>
16 /// precision_f64(1.2300_f64, 2) // 1.2<f64>
17 /// precision_f64(1.2300_f32 as f64, 2) // 1.2<f64>
18 /// precision_f64(1.2300_f32 as f64, 2) as f32 // 1.2<f32>
19 /// ```
20 pub fn precision_f64(x: f64, decimals: u32) -> f64 {
21 if x == 0. || decimals == 0 {
22 0.
23 } else {
24 let decimals = decimals.max(x.abs().log10().ceil() as u32 + 2);
25 let shift = decimals as i32 - x.abs().log10().ceil() as i32;
26 let shift_factor = 10_f64.powi(shift);
27
28 (x * shift_factor).round() / shift_factor
29 }
30 }
31}