all repos — min @ 9c62f002e4bc1b5102678f74bb65e5816a960499

A small but practical concatenative programming language.

Fixed reqTwoSimilarNotSymbol.

Added better floating point support with nan, inf, ninf, and a == comparator for floats
Peter Munch-Ellingsen peterme@peterme.net
Mon, 23 Oct 2017 09:58:49 +0200
commit

9c62f002e4bc1b5102678f74bb65e5816a960499

parent

62239aac39c3d286770c3bd8514cd11f032ce735

3 files changed, 36 insertions(+), 3 deletions(-)

jump to
M core/utils.nimcore/utils.nim

@@ -254,5 +254,5 @@

proc reqTwoSimilarTypesNonSymbol*(i: var MinInterpreter, a, b: var MinValue) = a = i.pop b = i.pop - if not ((a.kind == a.kind or (a.isNumber and a.isNumber)) and not a.isSymbol): + if not ((a.kind == b.kind or (a.isNumber and b.isNumber)) and not a.isSymbol): raiseInvalid("Two non-symbol values of similar type are required on the stack")
M lib/min_logic.nimlib/min_logic.nim

@@ -1,5 +1,6 @@

import - tables + tables, + math import ../core/parser, ../core/value,

@@ -75,7 +76,30 @@

def.symbol("==") do (i: In): var n1, n2: MinValue i.reqTwoSimilarTypesNonSymbol n2, n1 - i.push newVal(n1 == n2) + 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) + else: + i.push newVal(n1 == n2) def.symbol("!=") do (i: In): var n1, n2: MinValue
M lib/min_num.nimlib/min_num.nim

@@ -12,6 +12,15 @@

proc num_module*(i: In)= let def = i.define() + + def.symbol("nan") do (i: In): + i.push newVal(NaN) + + def.symbol("inf") do (i: In): + i.push newVal(Inf) + + def.symbol("ninf") do (i: In): + i.push newVal(NegInf) def.symbol("+") do (i: In): let vals = i.expect("num", "num")