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

[Adding math function] Support for Ceil, Cbrt functions #193

Merged
merged 3 commits into from
Apr 21, 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
28 changes: 28 additions & 0 deletions evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,34 @@ var Builtins_math = map[string]*env.Builtin{
}
},
},
"ceil": {
Argsn: 1,
Doc: "Returns the least integer value greater than or equal to x.",
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:
return *env.NewDecimal(float64(val.Value))
case env.Decimal:
return *env.NewDecimal(math.Ceil(val.Value))
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "ceil")
}
},
},
"cbrt": {
Argsn: 1,
Doc: "Returns returns the cube root of x.",
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:
return *env.NewDecimal(math.Cbrt(float64(val.Value)))
case env.Decimal:
return *env.NewDecimal(math.Cbrt(val.Value))
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "cbrt")
}
},
},
"pi": {
Argsn: 0,
Doc: "Return Pi constant.",
Expand Down
16 changes: 16 additions & 0 deletions tests/misc.rye
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,22 @@ group "atanh"
{
equal { do\in math { atanh 0.5 } } 0.5493061443340548
}

group "ceil"
mold\nowrap ""
{ { string } }
{
equal { do\in math { ceil 5.7 } } 6.000000
equal { do\in math { ceil 4.9 } } 5.000000
}

group "cbrt"
mold\nowrap ""
{ { string } }
{
equal { do\in math { cbrt 27 } } 3.000000
equal { do\in math { cbrt 12.3 } } 2.308350239753609
}

}

Expand Down