Skip to content

Commit

Permalink
[Adding math function] Support for Ceil, Cbrt functions (#193)
Browse files Browse the repository at this point in the history
* Adding Ceil and Cbrt math functions

* Adding test cases for Ceil and Cbrt math functions

* fixing to remove static lint error
  • Loading branch information
pratikmota authored Apr 21, 2024
1 parent dd1ce34 commit ffd3642
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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

0 comments on commit ffd3642

Please sign in to comment.