Skip to content
Permalink
Browse files

Add asin() and acos() to runtime

  • Loading branch information
thesephist committed Feb 18, 2020
1 parent 8b436c1 commit 8ec19120e1238a9872a0923e250458284bc8949b
Showing with 40 additions and 0 deletions.
  1. +2 −0 SPEC.md
  2. +38 −0 pkg/ink/runtime.go
@@ -173,6 +173,8 @@ These are the right primitives, but we can build much more sophisticated systems

- `sin(number) => number`: sine
- `cos(number) => number`: cosine
- `asin(number) => number`: arcsine (inverse sin)
- `acos(number) => number`: arccosine (inverse cos)
- `pow(number, number) => number`: power, also stands in for finding roots with exponent < 1
- `ln(number) => number`: natural log
- `floor(number) => number`: floor / truncation
@@ -69,6 +69,8 @@ func (ctx *Context) LoadEnvironment() {
// math
ctx.LoadFunc("sin", inkSin)
ctx.LoadFunc("cos", inkCos)
ctx.LoadFunc("asin", inkAsin)
ctx.LoadFunc("acos", inkAcos)
ctx.LoadFunc("pow", inkPow)
ctx.LoadFunc("ln", inkLn)
ctx.LoadFunc("floor", inkFloor)
@@ -1388,6 +1390,42 @@ func inkCos(ctx *Context, in []Value) (Value, error) {
return NumberValue(math.Cos(float64(inNum))), nil
}

func inkAsin(ctx *Context, in []Value) (Value, error) {
if len(in) != 1 {
return nil, Err{
ErrRuntime,
"sin() takes exactly one number argument",
}
}
inNum, isNum := in[0].(NumberValue)
if !isNum {
return nil, Err{
ErrRuntime,
fmt.Sprintf("sin() takes a number argument, got %s", in[0]),
}
}

return NumberValue(math.Asin(float64(inNum))), nil
}

func inkAcos(ctx *Context, in []Value) (Value, error) {
if len(in) != 1 {
return nil, Err{
ErrRuntime,
"cos() takes exactly one number argument",
}
}
inNum, isNum := in[0].(NumberValue)
if !isNum {
return nil, Err{
ErrRuntime,
fmt.Sprintf("cos() takes a number argument, got %s", in[0]),
}
}

return NumberValue(math.Acos(float64(inNum))), nil
}

func inkPow(ctx *Context, in []Value) (Value, error) {
if len(in) != 2 {
return nil, Err{

0 comments on commit 8ec1912

Please sign in to comment.
You can’t perform that action at this time.