diff --git a/core/__init__.py b/core/__init__.py index da0dc54..116ef0e 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -1,6 +1,7 @@ #! /usr/bin/env python3 # encoding: utf-8 +from . import m from . import app from . import aux from . import cmd diff --git a/core/m.py b/core/m.py new file mode 100644 index 0000000..fdbcbbc --- /dev/null +++ b/core/m.py @@ -0,0 +1,46 @@ +#! /usr/bin/env python3 +# encoding: utf-8 + +import sys + +class compare_float: + """ + Sets .lt, .eq or .gt iff A is less than, equal (within epsilon) or greather than B. + """ + + def __init__(self, A, B, epsilon=sys.float_info.epsilon): + self.lt = False + self.le = True + self.eq = False + self.ge = True + self.gt = False + + if A < B: + self.lt = True + self.le = True + self.ge = False + elif A > B: + self.gt = True + self.le = False + self.ge = True + + if abs(A - B) < epsilon: + self.eq = True + else: + self.le = False + self.ge = False + + # convenience + self.less = self.lt + self.less_or_equal = self.le + self.equal = self.eq + self.greater_or_equal = self.ge + self.greater = self.gt + +if __name__ == '__main__': + x = compare_float(1, 2, 0.5) + assert x.lt and x.less and not (x.le or x.ge) + x = compare_float(2, 2, 0.5) + assert x.eq and x.le and x.ge and not (x.lt or x.gt) + x = compare_float(3, 2, 0.5) + assert x.gt and not (x.ge or x.eq or x.lt)