add over.core.m

add over.core.m.compare_float
This commit is contained in:
Martinez 2015-09-21 23:48:08 +02:00
parent 01ef34b519
commit 8d700f5ff1
2 changed files with 47 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#! /usr/bin/env python3
# encoding: utf-8
from . import m
from . import app
from . import aux
from . import cmd

46
core/m.py Normal file
View file

@ -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)