Advertisement
NLinker

Different presicion asserts

Nov 19th, 2019
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.40 KB | None | 0 0
  1. // https://stackoverflow.com/a/41447964/5066426
  2.  
  3. fn approx_equal(a: f64, b: f64, decimal_places: u8) -> bool {
  4.     let factor = 10.0f64.powi(decimal_places as i32);
  5.     let a = (a * factor).trunc();
  6.     let b = (b * factor).trunc();
  7.     a == b
  8. }
  9.  
  10. fn main() {
  11.     assert!( approx_equal(1.234, 1.235, 1));
  12.     assert!( approx_equal(1.234, 1.235, 2));
  13.     assert!(!approx_equal(1.234, 1.235, 3));
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement