Skip to content

Commit

Permalink
acos and acosh math functions (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikmota committed Apr 5, 2024
1 parent 482fe92 commit 621f59d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
40 changes: 40 additions & 0 deletions evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,46 @@ var Builtins_math = map[string]*env.Builtin{
}
},
},
"acos": {
Argsn: 1,
Doc: "Returns the arccosine.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch val := arg0.(type) {
case env.Integer:
if val.Value < -1.0 || val.Value > 1.0 {
return MakeBuiltinError(ps, "Invalid input: Acos is only defined for -1 <= x <= 1.", "acos")
}
return *env.NewDecimal(math.Acos(float64(val.Value)))
case env.Decimal:
if val.Value < -1.0 || val.Value > 1.0 {
return MakeBuiltinError(ps, "Invalid input: Acos is only defined for -1 <= x <= 1.", "acos")
}
return *env.NewDecimal(math.Acos(val.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "acos")
}
},
},
"acosh": {
Argsn: 1,
Doc: "Returns the inverse hyperbolic cosine.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch val := arg0.(type) {
case env.Integer:
if val.Value < 1.0 {
return MakeBuiltinError(ps, " Acosh is only defined for x >= 1.", "acosh")
}
return *env.NewDecimal(math.Log(float64(val.Value) + math.Sqrt(float64(val.Value)*float64(val.Value)-1)))
case env.Decimal:
if val.Value < 1.0 {
return MakeBuiltinError(ps, " Acosh is only defined for x >= 1.", "acosh")
}
return *env.NewDecimal(math.Log(val.Value + math.Sqrt(val.Value*val.Value-1)))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "acosh")
}
},
},
"pi": {
Argsn: 0,
Doc: "Return Pi constant.",
Expand Down
14 changes: 8 additions & 6 deletions tests/basics.rye
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ section "Printing functions"
{
stdout { prns 123 } "123 "
}
group "print\val"
mold\nowrap ?print\val
{ { object } }
{
stdout { print\val 33 "value is: {{}}" } "value is: 33" + newline

;group "print\val"
;mold\nowrap ?print\val
;{ { object } }
;{
; stdout { print\val 33 "value is: {{}}" } "value is: 33" + newline
; stdout { print\val "OK" "value is: {{}}" } "value is: 33" + newline ; TODO-BUG quotes the string
; stdout { { "Jane Austen" } print\val "Hello {{}}!" } "value is: 33" + newline
}
;}

; group "print-ssv"
; mold\nowrap ?print-ssv
; { { function } }
Expand Down
17 changes: 17 additions & 0 deletions tests/misc.rye
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,23 @@ section "Math functions"
equal { do\in math { abs 99.0 } } 99.0
}

group "acos"
mold\nowrap ""
{ { string } }
{
equal { do\in math { acos -1 } } 3.141592653589793
equal { do\in math { acos 0.5 } } 1.0471975511965976
equal { do\in math { acos 1 } } 0.000000
}

group "acosh"
mold\nowrap ""
{ { string } }
{
equal { do\in math { acosh 1 } } 0.000000
equal { do\in math { acosh 2 } } 1.3169578969248166
}

; TODO add sin and cos ... need PI constant

}
Expand Down

0 comments on commit 621f59d

Please sign in to comment.