Moved num symbols to global.
@@ -15,7 +15,6 @@ ../lib/[min_global,
min_stack, min_seq, min_dict, - min_num, min_str, min_logic, min_time,@@ -45,7 +44,6 @@ i.stack_module
i.seq_module i.dict_module i.logic_module - i.num_module i.str_module i.time_module i.sys_module
@@ -6,6 +6,8 @@ json,
parseopt, algorithm, streams, + random, + bitops, nre, os, logging]@@ -162,7 +164,7 @@ CACHEDMODULES[f] = newDict(i2.scope)
CACHEDMODULES[f].objType = "module" mdl = CACHEDMODULES[f] for key, value in i2.scope.symbols.pairs: - # We need to set the mdl field of minOperators + # We need to set the mdl field of minOperators # In case of modules, or internal calls will not work var v = value v.mdl = mdl@@ -612,7 +614,7 @@
def.symbol("symbol-help") do (i: In): let vals = i.expect("'sym") let s = vals[0].getString - let sym = i.scope.getSymbol(s) + let sym = i.scope.getSymbol(s) if not sym.isNull: if not sym.doc.isNil and sym.doc.kind == JObject: var doc = i.fromJson(sym.doc)@@ -1189,8 +1191,288 @@ let key = vals[0]
let value = vals[1] key.getString.putEnv value.getString - def.symbol("$") do (i: In): - i.pushSym("get-env") + # Numeric operations + + def.symbol("nan") do (i: In): + i.push newVal(NaN) + + def.symbol("inf") do (i: In): + i.push newVal(Inf) + + def.symbol("-inf") do (i: In): + i.push newVal(NegInf) + + def.symbol("+") do (i: In): + let vals = i.expect("num", "num") + let a = vals[0] + let b = vals[1] + if a.isInt: + if b.isInt: + i.push newVal(a.intVal + b.intVal) + else: + i.push newVal(a.intVal.float + b.floatVal) + else: + if b.isFloat: + i.push newVal(a.floatVal + b.floatVal) + else: + i.push newVal(a.floatVal + b.intVal.float) + + def.symbol("-") do (i: In): + let vals = i.expect("num", "num") + let a = vals[0] + let b = vals[1] + if a.isInt: + if b.isInt: + i.push newVal(b.intVal - a.intVal) + else: + i.push newVal(b.floatVal - a.intVal.float) + else: + if b.isFloat: + i.push newVal(b.floatVal - a.floatVal) + else: + i.push newVal(b.intVal.float - a.floatVal) + + def.symbol("*") do (i: In): + let vals = i.expect("num", "num") + let a = vals[0] + let b = vals[1] + if a.isInt: + if b.isInt: + i.push newVal(a.intVal * b.intVal) + else: + i.push newVal(a.intVal.float * b.floatVal) + else: + if b.isFloat: + i.push newVal(a.floatVal * b.floatVal) + else: + i.push newVal(a.floatVal * b.intVal.float) + + def.symbol("/") do (i: In): + let vals = i.expect("num", "num") + let a = vals[0] + let b = vals[1] + if a.isInt: + if b.isInt: + i.push newVal(b.intVal.int / a.intVal.int) + else: + i.push newVal(b.floatVal / a.intVal.float) + else: + if b.isFloat: + i.push newVal(b.floatVal / a.floatVal) + else: + i.push newVal(b.intVal.float / a.floatVal) + + def.symbol("randomize") do (i: In): + randomize() + + def.symbol("random") do (i: In): + let vals = i.expect("int") + let n = vals[0] + i.push n.intVal.int.rand.newVal + + def.symbol("div") do (i: In): + let vals = i.expect("int", "int") + let b = vals[0] + let a = vals[1] + i.push(newVal(a.intVal div b.intVal)) + + def.symbol("mod") do (i: In): + let vals = i.expect("int", "int") + let b = vals[0] + let a = vals[1] + i.push(newVal(a.intVal mod b.intVal)) + + def.symbol("succ") do (i: In): + let vals = i.expect("int") + let n = vals[0] + i.push newVal(n.intVal + 1) + + def.symbol("pred") do (i: In): + let vals = i.expect("int") + let n = vals[0] + i.push newVal(n.intVal - 1) + + def.symbol("even?") do (i: In): + let vals = i.expect("int") + let n = vals[0] + i.push newVal(n.intVal mod 2 == 0) + + def.symbol("odd?") do (i: In): + let vals = i.expect("int") + let n = vals[0] + i.push newVal(n.intVal mod 2 != 0) + + def.symbol("bitnot") do (i: In): + let vals = i.expect("int") + let a = vals[0] + i.push newVal(not a.intVal) + + def.symbol("shl") do (i: In): + let vals = i.expect("int", "int") + let b = vals[0] + let a = vals[1] + i.push newVal(a.intVal shl b.intVal) + + def.symbol("shr") do (i: In): + let vals = i.expect("int", "int") + let b = vals[0] + let a = vals[1] + i.push newVal(a.intVal shr b.intVal) + + def.symbol("sum") do (i: In): + var s: MinValue + i.reqQuotationOfNumbers s + var c = 0.float + var isInt = true + for n in s.qVal: + if n.isFloat: + isInt = false + c = + n.floatVal + else: + c = c + n.intVal.float + if isInt: + i.push c.int.newVal + else: + i.push c.newVal + + def.symbol("product") do (i: In): + var s: MinValue + i.reqQuotationOfNumbers s + var c = 1.float + var isInt = true + for n in s.qVal: + if n.isFloat: + isInt = false + c = c * n.floatVal + else: + c = c * n.intVal.float + if isInt: + i.push c.int.newVal + else: + i.push c.newVal + + def.symbol("avg") do (i: In): + var s: MinValue + i.reqQuotationOfNumbers s + var c = 0.float + for n in s.qVal: + if n.isFloat: + c = + n.floatVal + else: + c = c + n.intVal.float + c = c / len(s.qVal).float + i.push c.newVal + + def.symbol("med") do (i: In): + var s: MinValue + i.reqQuotationOfNumbers s + let first = s.qVal[(s.qVal.len-1) div 2] + let second = s.qVal[((s.qVal.len-1) div 2)+1] + if s.qVal.len mod 2 == 1: + i.push first + else: + if first.isFloat: + if second.isFloat: + i.push ((first.floatVal+second.floatVal)/2).newVal + else: + i.push ((first.floatVal+second.intVal.float)/2).newVal + else: + if second.isFloat: + i.push ((first.intVal.float+second.floatVal)/2).newVal + else: + i.push ((first.intVal+second.intVal).float/2).newVal + + def.symbol("range") do (i: In): + var s: MinValue + i.reqQuotationOfIntegers s + var a = s.qVal[0] + var b = s.qVal[1] + var step = 1.newVal + var res = newSeq[MinValue](0) + if len(s.qVal) == 3: + a = s.qVal[0] + b = s.qVal[1] + step = s.qVal[2] + var j = a + if a.intVal < b.intVal: + while j.intVal <= b.intVal: + res.add j + j = (j.intVal + step.intVal).newVal + else: + while j.intVal >= b.intVal: + res.add j + j = (j.intVal - step.intVal).newVal + i.push res.newVal + + def.symbol("base") do (i: In): + let vals = i.expect("'sym") + let base = vals[0].getString + if not ["dec", "hex", "oct", "bin"].contains(base): + raiseInvalid("[base] Invalid base '$#'. Expected one of: 'dec', 'oct', 'hex', 'bin'" % + [base]) + case base: + of "dec": + NUMBASE = baseDec + of "oct": + NUMBASE = baseOct + of "hex": + NUMBASE = baseHex + of "bin": + NUMBASE = baseBin + + def.symbol("base?") do (i: In): + case NUMBASE: + of baseDec: + i.push "dec".newVal + of baseOct: + i.push "oct".newVal + of baseHex: + i.push "hex".newVal + of baseBin: + i.push "bin".newVal + + def.symbol("bitand") do (i: In): + let args = i.expect("int", "int") + i.push (bitand(args[0].intVal, args[1].intVal)).newVal + + def.symbol("bitor") do (i: In): + let args = i.expect("int", "int") + i.push (bitor(args[0].intVal, args[1].intVal)).newVal + + def.symbol("bitxor") do (i: In): + let args = i.expect("int", "int") + i.push (bitxor(args[1].intVal, args[0].intVal)).newVal + + def.symbol("bitclear") do (i: In): + var q: MinValue + i.reqQuotationOfIntegers(q) + var vals = i.expect("int") + var val = vals[0].intVal + for n in q.qVal: + val.clearBits(n.intVal) + i.push val.newVal + + def.symbol("bitset") do (i: In): + var q: MinValue + i.reqQuotationOfIntegers(q) + var vals = i.expect("int") + var val = vals[0].intVal + for n in q.qVal: + val.setBits(n.intVal) + i.push val.newVal + + def.symbol("bitflip") do (i: In): + var q: MinValue + i.reqQuotationOfIntegers(q) + var vals = i.expect("int") + var val = vals[0].intVal + for n in q.qVal: + val.flipBits(n.intVal) + i.push val.newVal + + def.symbol("bitparity") do (i: In): + let args = i.expect("int") + i.push (args[0].intVal.parityBits).newVal # Sigils@@ -1219,6 +1501,9 @@ def.sigil("$") do (i: In):
i.pushSym("get-env") # Shorthand symbol aliases + + def.symbol("$") do (i: In): + i.pushSym("get-env") def.symbol("=-=") do (i: In): i.pushSym("expect-empty-stack")
@@ -1,296 +0,0 @@
-import - std/[random, - strutils, - bitops] -import - ../core/parser, - ../core/value, - ../core/interpreter, - ../core/utils - -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("-inf") do (i: In): - i.push newVal(NegInf) - - def.symbol("+") do (i: In): - let vals = i.expect("num", "num") - let a = vals[0] - let b = vals[1] - if a.isInt: - if b.isInt: - i.push newVal(a.intVal + b.intVal) - else: - i.push newVal(a.intVal.float + b.floatVal) - else: - if b.isFloat: - i.push newVal(a.floatVal + b.floatVal) - else: - i.push newVal(a.floatVal + b.intVal.float) - - def.symbol("-") do (i: In): - let vals = i.expect("num", "num") - let a = vals[0] - let b = vals[1] - if a.isInt: - if b.isInt: - i.push newVal(b.intVal - a.intVal) - else: - i.push newVal(b.floatVal - a.intVal.float) - else: - if b.isFloat: - i.push newVal(b.floatVal - a.floatVal) - else: - i.push newVal(b.intVal.float - a.floatVal) - - def.symbol("*") do (i: In): - let vals = i.expect("num", "num") - let a = vals[0] - let b = vals[1] - if a.isInt: - if b.isInt: - i.push newVal(a.intVal * b.intVal) - else: - i.push newVal(a.intVal.float * b.floatVal) - else: - if b.isFloat: - i.push newVal(a.floatVal * b.floatVal) - else: - i.push newVal(a.floatVal * b.intVal.float) - - def.symbol("/") do (i: In): - let vals = i.expect("num", "num") - let a = vals[0] - let b = vals[1] - if a.isInt: - if b.isInt: - i.push newVal(b.intVal.int / a.intVal.int) - else: - i.push newVal(b.floatVal / a.intVal.float) - else: - if b.isFloat: - i.push newVal(b.floatVal / a.floatVal) - else: - i.push newVal(b.intVal.float / a.floatVal) - - def.symbol("randomize") do (i: In): - randomize() - - def.symbol("random") do (i: In): - let vals = i.expect("int") - let n = vals[0] - i.push n.intVal.int.rand.newVal - - def.symbol("div") do (i: In): - let vals = i.expect("int", "int") - let b = vals[0] - let a = vals[1] - i.push(newVal(a.intVal div b.intVal)) - - def.symbol("mod") do (i: In): - let vals = i.expect("int", "int") - let b = vals[0] - let a = vals[1] - i.push(newVal(a.intVal mod b.intVal)) - - def.symbol("succ") do (i: In): - let vals = i.expect("int") - let n = vals[0] - i.push newVal(n.intVal + 1) - - def.symbol("pred") do (i: In): - let vals = i.expect("int") - let n = vals[0] - i.push newVal(n.intVal - 1) - - def.symbol("even?") do (i: In): - let vals = i.expect("int") - let n = vals[0] - i.push newVal(n.intVal mod 2 == 0) - - def.symbol("odd?") do (i: In): - let vals = i.expect("int") - let n = vals[0] - i.push newVal(n.intVal mod 2 != 0) - - def.symbol("bitnot") do (i: In): - let vals = i.expect("int") - let a = vals[0] - i.push newVal(not a.intVal) - - def.symbol("shl") do (i: In): - let vals = i.expect("int", "int") - let b = vals[0] - let a = vals[1] - i.push newVal(a.intVal shl b.intVal) - - def.symbol("shr") do (i: In): - let vals = i.expect("int", "int") - let b = vals[0] - let a = vals[1] - i.push newVal(a.intVal shr b.intVal) - - def.symbol("sum") do (i: In): - var s: MinValue - i.reqQuotationOfNumbers s - var c = 0.float - var isInt = true - for n in s.qVal: - if n.isFloat: - isInt = false - c = + n.floatVal - else: - c = c + n.intVal.float - if isInt: - i.push c.int.newVal - else: - i.push c.newVal - - def.symbol("product") do (i: In): - var s: MinValue - i.reqQuotationOfNumbers s - var c = 1.float - var isInt = true - for n in s.qVal: - if n.isFloat: - isInt = false - c = c * n.floatVal - else: - c = c * n.intVal.float - if isInt: - i.push c.int.newVal - else: - i.push c.newVal - - def.symbol("avg") do (i: In): - var s: MinValue - i.reqQuotationOfNumbers s - var c = 0.float - for n in s.qVal: - if n.isFloat: - c = + n.floatVal - else: - c = c + n.intVal.float - c = c / len(s.qVal).float - i.push c.newVal - - def.symbol("med") do (i: In): - var s: MinValue - i.reqQuotationOfNumbers s - let first = s.qVal[(s.qVal.len-1) div 2] - let second = s.qVal[((s.qVal.len-1) div 2)+1] - if s.qVal.len mod 2 == 1: - i.push first - else: - if first.isFloat: - if second.isFloat: - i.push ((first.floatVal+second.floatVal)/2).newVal - else: - i.push ((first.floatVal+second.intVal.float)/2).newVal - else: - if second.isFloat: - i.push ((first.intVal.float+second.floatVal)/2).newVal - else: - i.push ((first.intVal+second.intVal).float/2).newVal - - def.symbol("range") do (i: In): - var s: MinValue - i.reqQuotationOfIntegers s - var a = s.qVal[0] - var b = s.qVal[1] - var step = 1.newVal - var res = newSeq[MinValue](0) - if len(s.qVal) == 3: - a = s.qVal[0] - b = s.qVal[1] - step = s.qVal[2] - var j = a - if a.intVal < b.intVal: - while j.intVal <= b.intVal: - res.add j - j = (j.intVal + step.intVal).newVal - else: - while j.intVal >= b.intVal: - res.add j - j = (j.intVal - step.intVal).newVal - i.push res.newVal - - def.symbol("base") do (i: In): - let vals = i.expect("'sym") - let base = vals[0].getString - if not ["dec", "hex", "oct", "bin"].contains(base): - raiseInvalid("[base] Invalid base '$#'. Expected one of: 'dec', 'oct', 'hex', 'bin'" % - [base]) - case base: - of "dec": - NUMBASE = baseDec - of "oct": - NUMBASE = baseOct - of "hex": - NUMBASE = baseHex - of "bin": - NUMBASE = baseBin - - def.symbol("base?") do (i: In): - case NUMBASE: - of baseDec: - i.push "dec".newVal - of baseOct: - i.push "oct".newVal - of baseHex: - i.push "hex".newVal - of baseBin: - i.push "bin".newVal - - def.symbol("bitand") do (i: In): - let args = i.expect("int", "int") - i.push (bitand(args[0].intVal, args[1].intVal)).newVal - - def.symbol("bitor") do (i: In): - let args = i.expect("int", "int") - i.push (bitor(args[0].intVal, args[1].intVal)).newVal - - def.symbol("bitxor") do (i: In): - let args = i.expect("int", "int") - i.push (bitxor(args[1].intVal, args[0].intVal)).newVal - - def.symbol("bitclear") do (i: In): - var q: MinValue - i.reqQuotationOfIntegers(q) - var vals = i.expect("int") - var val = vals[0].intVal - for n in q.qVal: - val.clearBits(n.intVal) - i.push val.newVal - - def.symbol("bitset") do (i: In): - var q: MinValue - i.reqQuotationOfIntegers(q) - var vals = i.expect("int") - var val = vals[0].intVal - for n in q.qVal: - val.setBits(n.intVal) - i.push val.newVal - - def.symbol("bitflip") do (i: In): - var q: MinValue - i.reqQuotationOfIntegers(q) - var vals = i.expect("int") - var val = vals[0].intVal - for n in q.qVal: - val.flipBits(n.intVal) - i.push val.newVal - - def.symbol("bitparity") do (i: In): - let args = i.expect("int") - i.push (args[0].intVal.parityBits).newVal - - def.finalize("num")
@@ -1,5 +1,7 @@
### BREAKING CHANGES +- All symbols defined in the **num** module have been moved to the **global** module. + ### New Features ### Fixes and Improvements
@@ -1,7 +1,6 @@
; Imports 'str import 'logic import -'num import 'stack import 'seq import 'dict import
@@ -42,7 +42,6 @@ ; Imports
'str import 'io import 'logic import -'num import 'sys import 'stack import 'seq import
@@ -47,14 +47,62 @@ {#sig||^||lambda#}
{#alias||^||lambda#} +{#op||+||{{n1}} {{n2}}||{{n3}}|| +Sums {{n1}} and {{n2}}. #} + +{#op||-||{{n1}} {{n2}}||{{n3}}|| +Subtracts {{n2}} from {{n1}}. #} + +{#op||-inf||{{none}}||{{n}}|| +Returns negative infinity. #} + +{#op||*||{{n1}} {{n2}}||{{n3}}|| +Multiplies {{n1}} by {{n2}}. #} + +{#op||/||{{n1}} {{n2}}||{{n3}}|| +Divides {{n1}} by {{n2}}. #} + {#op||apply||{{q}}||({{a0p}})|| Returns a new quotation obtained by evaluating each element of {{q}} in a separate stack. #} {#op||args||{{none}}||{{q}}|| Returns a list of all arguments passed to the current program.#} + +{#op||avg||{{q}}||{{n}}|| +Returns the average of the items of {{q}}. #} + +{#op||base||["dec"|"hex"|"oct"|"bin"](class:kwd)||{{none}}|| +Sets the numeric base used to represent integers. #} + +{#op||base?||{{none}}||["dec"|"hex"|"oct"|"bin"](class:kwd)|| +Returns the numeric base currently used to represent integers (default: ["dec"](class:kwd)). #} {#op||bind||{{any}} {{sl}}||{{none}}|| Binds the specified value (auto-quoted) to an existing symbol {{sl}}.#} + +{#op||bitand||{{i1}} {{i2}}||{{i3}}|| +Computes the bitwise *and* of integer {{i1}} and {{i2}}.#} + +{#op||bitclear||{{i1}} {{q}}||{{i2}}|| +Sets the bytes specified via their position in {{i1}} through {{q}} to 0. #} + +{#op||bitflip||{{i1}} {{q}}||{{i2}}|| +Flips the bytes specified via their position in {{i1}} through {{q}}. #} + +{#op||bitnot||{{i1}}||{{i2}}|| +Computes the bitwise *complement* of {{i1}}.#} + +{#op||bitor||{{i1}} {{i2}}||{{i3}}|| +Computes the bitwise *or* of integers {{i1}} and {{i2}}.#} + +{#op||bitparity||{{i1}}||{{i2}}|| +Calculate the bit parity in {{i1}}. If the number of 1-bits is odd, the parity is 1, otherwise 0.#} + +{#op||bitset||{{i1}} {{q}}||{{i2}}|| +Sets the bytes specified via their position in {{i1}} through {{q}} to 0. #} + +{#op||bitxor||{{i1}} {{i2}}||{{i3}}|| +Computes the bitwise *xor* of integers {{i1}} and {{i2}}.#} {#op||bool||{{any}}||{{b}}|| > Converts {{any}} to a boolean value based on the following rules:@@ -116,8 +164,14 @@
{#op||dev?||{{none}}||{{b}}|| Returns {{t}} if the current program is being executed in development mode.#} +{#op||div||{{i1}} {{i2}}||{{i3}}|| +Divides {{i1}} by {{i2}} (integer division). #} + {#op||eval||{{s}}||{{a0p}}|| Parses and interprets {{s}}. #} + +{#op||even?||{{i}}||{{b}}|| +Returns {{t}} if {{i}} is even, {{f}} otherwise. #} {#op||exit||{{i}}||{{none}}|| Exits the program or shell with {{i}} as return code. #}@@ -198,6 +252,9 @@ If {{q1}} evaluates to {{t}} then evaluates {{q2}}, otherwise evaluates {{q3}}.#}
{#op||import||{{sl}}||{{none}}|| Imports the a previously-loaded module {{sl}}, defining all its symbols in the current scope. #} + +{#op||inf||{{none}}||{{n}}|| +Returns infinity. #} {#op||infix-dequote||{{q}}||{{any}}|| > Dequotes {{q}} using infix notation.@@ -280,6 +337,18 @@
{#op||loglevel?||{{none}}||{{s}}|| Returns the current log level (debug, info, notice, warn, error or fatal). #} +{#op||med||{{q}}||{{n}}|| +Returns the median of the items of {{q}}. #} + +{#op||mod||{{i1}} {{i2}}||{{i3}}|| +Returns the integer module of {{i1}} divided by {{i2}}. #} + +{#op||nan||{{none}}||nan|| +Returns **NaN** (not a number). #} + +{#op||odd?||{{i}}||{{b}}|| +Returns {{t}} if {{i}} is odd, {{f}} otherwise. #} + {#op||operator||{{q}}||{{a0p}}|| > Provides a way to define a new operator (symbol, sigil, or typeclass) on the current scope performing additional checks (compared to `define` and `define-sigil`), and automatically mapping inputs and outputs. >@@ -356,6 +425,12 @@ > > Example
> > > Publish symbol [my-local-symbol](class:kwd) to [global](class:kwd) scope: > > `'my-local-symbol global publish` #} + +{#op||pred||{{i1}}||{{i2}}|| +Returns the predecessor of {{i1}}.#} + +{#op||product||{{q}}||{{i}}|| +Returns the product of all items of {{q}}. {{q}} is a quotation of integers. #} {#op||puts||{{any}}||{{any}}|| Prints {{any}} and a new line to STDOUT.#}@@ -375,6 +450,19 @@
{#op||quotesym||{{s}}||({{sym}})|| Creates a symbol with the value of {{s}} and wraps it in a quotation. #} +{#op||random||{{i1}}||{{i2}}|| +> Returns a random number {{i2}} between 0 and {{i1}}-1. +> +> > %note% +> > Note +> > +> > You must call `randomize` to initialize the random number generator, otherwise the same sequence of numbers will be returned.#} + +{#op||randomize||{{none}}||{{null}|| +Initializes the random number generator using a seed based on the current timestamp. #} + +{#op||range||{{q2}}||{{q2}}|| +Takes a quotation {{q1}} of two or three integers in the form of *start*, *end* and an optional *step* (1 if not specified) and generates the sequence and returns the resulting quotation of integers {{q2}}. #} {#op||raise||{{e}}||{{none}}|| Raises the error specified via the dictionary {{e}}.#}@@ -438,6 +526,12 @@
{#op||sealed-sigil?||{{sl}}||{{b}}|| Returns {{t}} if the sigil {{sl}} is sealed, {{f}} otherwise.#} +{#op||shl||{{i1}} {{i2}}||{{i3}}|| +Computes the *shift left* operation of {{i1}} and {{i2}}.#} + +{#op||shr||{{i1}} {{i2}}||{{i3}}|| +Computes the *shift right* operation of {{i1}} and {{i2}}.#} + {#op||sigil-help||{{sl}}||{{help}}|{{null}}|| Returns the help dictionary for the sigil {{sl}}, if available, {{null}} otherwise. #}@@ -449,6 +543,12 @@ Display the source code of symbol {{sl}} (if it has been implemented a {{m}} quotation). #}
{#op||string||{{any}}||{{s}}|| Converts {{any}} to its string representation.#} + +{#op||succ||{{i1}}||{{i2}}|| +Returns the successor of {{i1}}.#} + +{#op||sum||{{q}}||{{i}}|| +Returns the sum of all items of {{q}}. {{q}} is a quotation of integers. #} {#op||symbols||{{none}}||({{s0p}})|| Returns a list of all symbols defined in the [global](class:kwd) scope.#}
@@ -1,107 +0,0 @@
------ -content-type: "page" -title: "num Module" ------ -{@ _defs_.md || 0 @} - -{#op||+||{{n1}} {{n2}}||{{n3}}|| -Sums {{n1}} and {{n2}}. #} - -{#op||-||{{n1}} {{n2}}||{{n3}}|| -Subtracts {{n2}} from {{n1}}. #} - -{#op||-inf||{{none}}||{{n}}|| -Returns negative infinity. #} - -{#op||*||{{n1}} {{n2}}||{{n3}}|| -Multiplies {{n1}} by {{n2}}. #} - -{#op||/||{{n1}} {{n2}}||{{n3}}|| -Divides {{n1}} by {{n2}}. #} - -{#op||avg||{{q}}||{{n}}|| -Returns the average of the items of {{q}}. #} - -{#op||base||["dec"|"hex"|"oct"|"bin"](class:kwd)||{{none}}|| -Sets the numeric base used to represent integers. #} - -{#op||base?||{{none}}||["dec"|"hex"|"oct"|"bin"](class:kwd)|| -Returns the numeric base currently used to represent integers (default: ["dec"](class:kwd)). #} - -{#op||bitand||{{i1}} {{i2}}||{{i3}}|| -Computes the bitwise *and* of integer {{i1}} and {{i2}}.#} - -{#op||bitclear||{{i1}} {{q}}||{{i2}}|| -Sets the bytes specified via their position in {{i1}} through {{q}} to 0. #} - -{#op||bitflip||{{i1}} {{q}}||{{i2}}|| -Flips the bytes specified via their position in {{i1}} through {{q}}. #} - -{#op||bitnot||{{i1}}||{{i2}}|| -Computes the bitwise *complement* of {{i1}}.#} - -{#op||bitor||{{i1}} {{i2}}||{{i3}}|| -Computes the bitwise *or* of integers {{i1}} and {{i2}}.#} - -{#op||bitparity||{{i1}}||{{i2}}|| -Calculate the bit parity in {{i1}}. If the number of 1-bits is odd, the parity is 1, otherwise 0.#} - -{#op||bitset||{{i1}} {{q}}||{{i2}}|| -Sets the bytes specified via their position in {{i1}} through {{q}} to 0. #} - -{#op||bitxor||{{i1}} {{i2}}||{{i3}}|| -Computes the bitwise *xor* of integers {{i1}} and {{i2}}.#} - -{#op||even?||{{i}}||{{b}}|| -Returns {{t}} if {{i}} is even, {{f}} otherwise. #} - -{#op||div||{{i1}} {{i2}}||{{i3}}|| -Divides {{i1}} by {{i2}} (integer division). #} - -{#op||inf||{{none}}||{{n}}|| -Returns infinity. #} - -{#op||med||{{q}}||{{n}}|| -Returns the median of the items of {{q}}. #} - -{#op||mod||{{i1}} {{i2}}||{{i3}}|| -Returns the integer module of {{i1}} divided by {{i2}}. #} - -{#op||nan||{{none}}||nan|| -Returns **NaN** (not a number). #} - -{#op||odd?||{{i}}||{{b}}|| -Returns {{t}} if {{i}} is odd, {{f}} otherwise. #} - -{#op||pred||{{i1}}||{{i2}}|| -Returns the predecessor of {{i1}}.#} - -{#op||product||{{q}}||{{i}}|| -Returns the product of all items of {{q}}. {{q}} is a quotation of integers. #} - -{#op||random||{{i1}}||{{i2}}|| -> Returns a random number {{i2}} between 0 and {{i1}}-1. -> -> > %note% -> > Note -> > -> > You must call `randomize` to initialize the random number generator, otherwise the same sequence of numbers will be returned.#} - -{#op||randomize||{{none}}||{{null}|| -Initializes the random number generator using a seed based on the current timestamp. #} - -{#op||range||{{q2}}||{{q2}}|| -Takes a quotation {{q1}} of two or three integers in the form of *start*, *end* and an optional *step* (1 if not specified) and generates the sequence and returns the resulting quotation of integers {{q2}}. #} - -{#op||shl||{{i1}} {{i2}}||{{i3}}|| -Computes the *shift left* operation of {{i1}} and {{i2}}.#} - -{#op||shr||{{i1}} {{i2}}||{{i3}}|| -Computes the *shift right* operation of {{i1}} and {{i2}}.#} - -{#op||succ||{{i1}}||{{i2}}|| -Returns the successor of {{i1}}.#} - -{#op||sum||{{q}}||{{i}}|| -Returns the sum of all items of {{q}}. {{q}} is a quotation of integers. #} -
@@ -26,8 +26,6 @@ {#link-module||str#}
: Provides operators to perform operations on strings, use regular expressions, interpolation, etc. {#link-module||sys#} : Provides operators to use as basic shell commands, access environment variables, and execute external commands. -{#link-module||num#} -: Provides operators to perform simple mathematical operations on integer and floating point numbers. {#link-module||time#} : Provides a few basic operators to manage dates, times, and timestamps. {#link-module||crypto#}
@@ -16,7 +16,6 @@ 'io load
'global load 'logic load 'math load -'num load 'seq load 'stack load 'str load
@@ -374,6 +374,90 @@ (dup *) ^square
3 obj.square 9 == ) test.assert + ;; Numeric operations + + + (2 2 + 4 ==) test.assert + (1 3.0 + 4 ==) test.assert + (3.1 3.9 + 7 ==) test.assert + (3 -2.1 + 0.8999999999999999 ==) test.assert + + (3 3 - 0 ==) test.assert + (-5 -4 - -1 ==) test.assert + (-4 3.7 - -7.7 ==) test.assert + + (-2 4 * -8 ==) test.assert + (-2.5 -2 * 5 ==) test.assert + (3 3 * 9 ==) test.assert + + (5 2 / 2.5 ==) test.assert + (1 3 / 0.3333333333333333 ==) test.assert + (-3 2 / -1.5 ==) test.assert + + (5 2 div 2 ==) test.assert + (1 3 div 0 ==) test.assert + (-3 2 div -1 ==) test.assert + + (5 2 mod 1 ==) test.assert + (4 2 mod 0 ==) test.assert + (-3 2 mod -1 ==) test.assert + + (1000 random 1000 <) test.assert + + ((1 2 3 4 5) sum 15 ==) test.assert + + ((1 2 3 4 5) product 120 ==) test.assert + + ((1 2 3 4 5) avg 3.0 ==) test.assert + ((1 2 3 4 5 6) avg 3.5 ==) test.assert + + ((1 3 5 7) med 4.0 ==) test.assert + ((1 3 5 7 9) med 5 ==) test.assert + + ((1 5) range (1 2 3 4 5) ==) test.assert + ((5 1) range (5 4 3 2 1) ==) test.assert + ((4 7) range (4 5 6 7) ==) test.assert + ((7 4) range (7 6 5 4) ==) test.assert + ((1 6 2) range (1 3 5) ==) test.assert + ((1 6 3) range (1 4) ==) test.assert + ((0 6 2) range (0 2 4 6) ==) test.assert + ((6 1 2) range (6 4 2) ==) test.assert + ((6 1 3) range (6 3) ==) test.assert + ((6 0 2) range (6 4 2 0) ==) test.assert + + (2 3 bitand 2 ==) test.assert + + (123 bitnot -124 ==) test.assert + + (2 3 bitor 3 ==) test.assert + + (2 3 bitxor 1 ==) test.assert + + (2 3 shl 16 ==) test.assert + + (16 3 shr 2 ==) test.assert + + (0 :c + (c 10 <) (c succ @c) while + c 10 ==) test.assert + + ((1 2 3 4 5) (even?) filter (2 4) ==) test.assert + + ((1 2 3 4 5) (even?) any?) test.assert + + ((2 4 6 8) (even?) all?) test.assert + + (base? "dec" ==) test.assert + ('bin base "bin" base? == 'dec base) test.assert + (0b10010 18 ==) test.assert + (0b101010 0b010101 bitand 0b000000 ==) test.assert + (0b101010 0b010101 bitor 0b111111 ==) test.assert + (0b101010 0b010101 bitxor 0b111111 ==) test.assert + (0b111000 (0 2) bitflip 0b111101 ==) test.assert + (0b111001 (0) bitclear 0b111000 ==) test.assert + (0b111000 (0 1) bitset 0b111011 ==) test.assert + (0b111000 bitparity 1 ==) test.assert + test.report ;; Tidy up clear-stack
@@ -1,88 +0,0 @@
-'min-test require :test -;;; - -"num" test.describe - - (2 2 + 4 ==) test.assert - (1 3.0 + 4 ==) test.assert - (3.1 3.9 + 7 ==) test.assert - (3 -2.1 + 0.8999999999999999 ==) test.assert - - (3 3 - 0 ==) test.assert - (-5 -4 - -1 ==) test.assert - (-4 3.7 - -7.7 ==) test.assert - - (-2 4 * -8 ==) test.assert - (-2.5 -2 * 5 ==) test.assert - (3 3 * 9 ==) test.assert - - (5 2 / 2.5 ==) test.assert - (1 3 / 0.3333333333333333 ==) test.assert - (-3 2 / -1.5 ==) test.assert - - (5 2 div 2 ==) test.assert - (1 3 div 0 ==) test.assert - (-3 2 div -1 ==) test.assert - - (5 2 mod 1 ==) test.assert - (4 2 mod 0 ==) test.assert - (-3 2 mod -1 ==) test.assert - - (1000 random 1000 <) test.assert - - ((1 2 3 4 5) sum 15 ==) test.assert - - ((1 2 3 4 5) product 120 ==) test.assert - - ((1 2 3 4 5) avg 3.0 ==) test.assert - ((1 2 3 4 5 6) avg 3.5 ==) test.assert - - ((1 3 5 7) med 4.0 ==) test.assert - ((1 3 5 7 9) med 5 ==) test.assert - - ((1 5) range (1 2 3 4 5) ==) test.assert - ((5 1) range (5 4 3 2 1) ==) test.assert - ((4 7) range (4 5 6 7) ==) test.assert - ((7 4) range (7 6 5 4) ==) test.assert - ((1 6 2) range (1 3 5) ==) test.assert - ((1 6 3) range (1 4) ==) test.assert - ((0 6 2) range (0 2 4 6) ==) test.assert - ((6 1 2) range (6 4 2) ==) test.assert - ((6 1 3) range (6 3) ==) test.assert - ((6 0 2) range (6 4 2 0) ==) test.assert - - (2 3 bitand 2 ==) test.assert - - (123 bitnot -124 ==) test.assert - - (2 3 bitor 3 ==) test.assert - - (2 3 bitxor 1 ==) test.assert - - (2 3 shl 16 ==) test.assert - - (16 3 shr 2 ==) test.assert - - (0 :c - (c 10 <) (c succ @c) while - c 10 ==) test.assert - - ((1 2 3 4 5) (even?) filter (2 4) ==) test.assert - - ((1 2 3 4 5) (even?) any?) test.assert - - ((2 4 6 8) (even?) all?) test.assert - - (base? "dec" ==) test.assert - ('bin base "bin" base? == 'dec base) test.assert - (0b10010 18 ==) test.assert - (0b101010 0b010101 bitand 0b000000 ==) test.assert - (0b101010 0b010101 bitor 0b111111 ==) test.assert - (0b101010 0b010101 bitxor 0b111111 ==) test.assert - (0b111000 (0 2) bitflip 0b111101 ==) test.assert - (0b111001 (0) bitclear 0b111000 ==) test.assert - (0b111000 (0 1) bitset 0b111011 ==) test.assert - (0b111000 bitparity 1 ==) test.assert - - test.report - clear-stack