Merge pull request #108 from drkameleon/library-additions-num
Fabio Cevasco h3rald@h3rald.com
Fri, 22 Jan 2021 07:27:26 +0100
3 files changed,
66 insertions(+),
1 deletions(-)
M
minpkg/lib/min_num.nim
→
minpkg/lib/min_num.nim
@@ -135,4 +135,52 @@ 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.finalize("num")
M
site/contents/reference-num.md
→
site/contents/reference-num.md
@@ -55,4 +55,13 @@ {#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. #}+Returns the sum of all items of {{q}}. {{q}} is a quotation of integers. #} + +{#op||product||{{q}}||{{i}}|| +Returns the product of all items of {{q}}. {{q}} is a quotation of integers. #} + +{#op||avg||{{q}}||{{n}}|| +Returns the average of the items of {{q}}. #} + +{#op||med||{{q}}||{{n}}|| +Returns the median of the items of {{q}}. #}
M
tests/num.min
→
tests/num.min
@@ -32,6 +32,14 @@ (1000 random 1000 <) assert
((1 2 3 4 5) sum 15 ==) assert + ((1 2 3 4 5) product 120 ==) assert + + ((1 2 3 4 5) avg 3.0 ==) assert + ((1 2 3 4 5 6) avg 3.5 ==) assert + + ((1 3 5 7) med 4.0 ==) assert + ((1 3 5 7 9) med 5 ==) assert + (0 :c (c 10 <) (c succ @c) while c 10 ==) assert