Advertisement
Guest User

floatsAlmEq (are floats almost equal)

a guest
Apr 19th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. # Floating point math fun!  Since equality tests on floats are a crap shoot,
  2. # instead check if floats are almost equal (is the first float within a
  3. # certain tolerance amount of the second).
  4. # Note, this function may fail in certain circumstances depending on the
  5. # number of significant figures. If comparisons become problematic, you can
  6. # try a different power of ten for the "tol" value (eg 0.01 or 0.00001)
  7. def floatsAlmEq(f1,f2):
  8.     tol = 0.0001
  9.     return f1 > (f2 - tol) and f1 < (f2 + tol)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement