Merge remote-tracking branch 'refs/remotes/origin/master'

This commit is contained in:
Martinez 2015-11-05 10:17:48 +01:00
commit 6d854fefc6
2 changed files with 40 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

39
core/m.py Normal file
View file

@ -0,0 +1,39 @@
#! /usr/bin/env python3
# encoding: utf-8
class compare_float:
"""
Sets .lt, .eq or .gt iff A is less than, equal or greather than B.
"""
def __init__(self, A, B, epsilon=1e-12):
self.lt = False
self.le = True
self.eq = False
self.ge = True
self.gt = False
if A < (B - epsilon):
self.lt = True
self.ge = False
elif A > (B + epsilon):
self.gt = True
self.le = False
if abs(A - B) < epsilon:
self.eq = True
# 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)