Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://stackoverflow.com/a/41447964/5066426
- fn approx_equal(a: f64, b: f64, decimal_places: u8) -> bool {
- let factor = 10.0f64.powi(decimal_places as i32);
- let a = (a * factor).trunc();
- let b = (b * factor).trunc();
- a == b
- }
- fn main() {
- assert!( approx_equal(1.234, 1.235, 1));
- assert!( approx_equal(1.234, 1.235, 2));
- assert!(!approx_equal(1.234, 1.235, 3));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement