Skip to content

Commit

Permalink
Add asin() and acos() to runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
thesephist committed Feb 18, 2020
1 parent 8b436c1 commit 8ec1912
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions pkg/ink/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 8ec1912

Please sign in to comment.