Skip to content

Instantly share code, notes, and snippets.

@CD3
Created January 18, 2018 16:37
Show Gist options
  • Save CD3/b3cf45c02623d3eb4fa9012a2e23ada2 to your computer and use it in GitHub Desktop.
Save CD3/b3cf45c02623d3eb4fa9012a2e23ada2 to your computer and use it in GitHub Desktop.
An Approx class for unit testing float values similar to Catch2 (https://github.com/catchorg/Catch2/blob/master/docs/assertions.md#floating-point-comparisons)
class Approx(object):
def __init__(self,val):
self._val = val
self._epsilon = 0.01
def epsilon(self,epsilon):
self._epsilon = epsilon
return self
def __eq__(self,other):
return abs(other - self._val) <= self._epsilon*abs(other + self._val)/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment