Addded missing logic operators like <= >= !=
Peter Munch-Ellingsen peterme@peterme.net
Mon, 23 Oct 2017 10:24:17 +0200
1 files changed,
32 insertions(+),
28 deletions(-)
jump to
M
lib/min_logic.nim
→
lib/min_logic.nim
@@ -9,6 +9,28 @@ ../core/utils
# Comparison operators +proc floatCompare(n1, n2: MinValue): bool = + let + a:float = if n1.kind != minFloat: n1.intVal.float else: n1.floatVal + b:float = if n2.kind != minFloat: n2.intVal.float else: n2.floatVal + if a.classify == fcNan and b.classify == fcNan: + return true + else: + const + FLOAT_MIN_NORMAL = 2e-1022 + FLOAT_MAX_VALUE = (2-2e-52)*2e1023 + epsilon = 0.00001 + let + absA = abs(a) + absB = abs(b) + diff = abs(a - b) + + if a == b: + return true + elif a == 0 or b == 0 or diff < FLOAT_MIN_NORMAL: + return diff < (epsilon * FLOAT_MIN_NORMAL) + else: + return diff / min((absA + absB), FLOAT_MAX_VALUE) < epsilon proc logic_module*(i: In)= let def = i.define()@@ -35,12 +57,12 @@ if n1.isNumber and n2.isNumber:
if n1.isInt and n2.isInt: i.push newVal(n1.intVal >= n2.intVal) elif n1.isInt and n2.isFloat: - i.push newVal(n1.intVal.float >= n2.floatVal) + i.push newVal(n1.intVal.float > n2.floatVal or floatCompare(n1, n2)) elif n1.isFloat and n2.isFloat: - i.push newVal(n1.floatVal >= n2.floatVal) + i.push newVal(n1.floatVal > n2.floatVal or floatCompare(n1, n2)) elif n1.isFloat and n2.isInt: - i.push newVal(n1.floatVal >= n2.intVal.float) - else: + i.push newVal(n1.floatVal > n2.intVal.float or floatCompare(n1, n2)) + else: i.push newVal(n1.strVal >= n2.strVal) def.symbol("<") do (i: In):@@ -65,11 +87,11 @@ if n1.isNumber and n2.isNumber:
if n1.isInt and n2.isInt: i.push newVal(n1.intVal >= n2.intVal) elif n1.isInt and n2.isFloat: - i.push newVal(n1.intVal.float >= n2.floatVal) + i.push newVal(n1.intVal.float > n2.floatVal or floatCompare(n1, n2)) elif n1.isFloat and n2.isFloat: - i.push newVal(n1.floatVal >= n2.floatVal) + i.push newVal(n1.floatVal > n2.floatVal or floatCompare(n1, n2)) elif n1.isFloat and n2.isInt: - i.push newVal(n1.floatVal >= n2.intVal.float) + i.push newVal(n1.floatVal > n2.intVal.float or floatCompare(n1, n2)) else: i.push newVal(n1.strVal >= n2.strVal)@@ -77,33 +99,15 @@ def.symbol("==") do (i: In):
var n1, n2: MinValue i.reqTwoSimilarTypesNonSymbol n2, n1 if n1.kind == minFloat or n2.kind == minFloat: - let - a:float = if n1.kind != minFloat: n1.intVal.float else: n1.floatVal - b:float = if n2.kind != minFloat: n2.intVal.float else: n2.floatVal - if a.classify == fcNan and b.classify == fcNan: - i.push newVal(true) - else: - const - FLOAT_MIN_NORMAL = 2e-1022 - FLOAT_MAX_VALUE = (2-2e-52)*2e1023 - epsilon = 0.00001 - let - absA = abs(a) - absB = abs(b) - diff = abs(a - b) - - if a == b: - i.push newVal(true) - elif a == 0 or b == 0 or diff < FLOAT_MIN_NORMAL: - i.push newVal(diff < (epsilon * FLOAT_MIN_NORMAL)) - else: - i.push newVal(diff / min((absA + absB), FLOAT_MAX_VALUE) < epsilon) + i.push newVal(floatCompare(n1, n2)) else: i.push newVal(n1 == n2) def.symbol("!=") do (i: In): var n1, n2: MinValue i.reqTwoSimilarTypesNonSymbol n2, n1 + if n1.kind == minFloat or n2.kind == minFloat: + i.push newVal(not floatCompare(n1, n2)) i.push newVal(not (n1 == n2)) # Boolean Logic