Skip to content

Commit

Permalink
math: add eq_approx_fXY for easier float equality approximation
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon committed Oct 23, 2023
1 parent 3506a08 commit 29830a4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions mth/math.v
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,27 @@ pub fn reduce_fraction[T](numerator T, denominator T) (T, T) {
g_c_d := gcd(numerator, denominator)
return numerator / g_c_d, denominator / g_c_d
}

// eq_approx_f32 returns whether `x` and `y` are approximately equal within `tolerance`.
pub fn eq_approx_f32(x f32, y f32, tolerance f32) bool {
diff := abs(x - y)
if diff <= tolerance {
return true
}
if diff < max(abs(x), abs(y)) * tolerance {
return true
}
return false
}

// eq_approx_f64 returns whether `x` and `y` are approximately equal within `tolerance`.
pub fn eq_approx_f64(x f64, y f64, tolerance f64) bool {
diff := abs(x - y)
if diff <= tolerance {
return true
}
if diff < max(abs(x), abs(y)) * tolerance {
return true
}
return false
}

0 comments on commit 29830a4

Please sign in to comment.