fix ln and remove unused code

This commit is contained in:
2023-03-11 12:37:02 +00:00
parent 42de489ca5
commit 3a449dec63
8 changed files with 69 additions and 127 deletions

View File

@@ -1,5 +1,7 @@
package main
import "github.com/wadey/go-rounding"
var vars = scope{}
func init() {
@@ -65,11 +67,68 @@ func init() {
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot create array from '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["maths"] = maths
vars["math"] = maths
vars["time"] = ArTime
vars["PI"] = PI
vars["π"] = PI
vars["e"] = e
vars["ln"] = builtinFunc{"ln", ArgonLn}
vars["log"] = builtinFunc{"log", ArgonLog}
vars["logN"] = builtinFunc{"logN", ArgonLogN}
vars["thread"] = builtinFunc{"thread", ArThread}
vars["round"] = builtinFunc{"round", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "round", message: "round takes 1 argument",
EXISTS: true}
}
precision := newNumber()
if len(a) > 1 {
switch x := a[1].(type) {
case number:
if !x.IsInt() {
return nil, ArErr{TYPE: "TypeError", message: "Cannot round to '" + typeof(a[1]) + "'", EXISTS: true}
}
precision = x
default:
return nil, ArErr{TYPE: "TypeError", message: "Cannot round to '" + typeof(a[1]) + "'", EXISTS: true}
}
}
switch x := a[0].(type) {
case number:
return rounding.Round(newNumber().Set(x), int(precision.Num().Int64()), rounding.HalfUp), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot round '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["floor"] = builtinFunc{"floor", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "floor", message: "floor takes 1 argument",
EXISTS: true}
}
switch x := a[0].(type) {
case number:
n := newNumber().Set(x)
if n.Sign() < 0 {
return rounding.Round(n, 0, rounding.Up), ArErr{}
}
return rounding.Round(n, 0, rounding.Down), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot floor '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["ceil"] = builtinFunc{"ceil", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "ceil", message: "ceil takes 1 argument",
EXISTS: true}
}
switch x := a[0].(type) {
case number:
n := newNumber().Set(x)
if n.Sign() < 0 {
return rounding.Round(n, 0, rounding.Down), ArErr{}
}
return rounding.Round(n, 0, rounding.Up), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot ceil '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["sqrt"] = builtinFunc{"sqrt", ArgonSqrt}
}