Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[math function] Adding acos and acosh math functions #186

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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