Skip to content

Commit

Permalink
Merge pull request #217 from pratikmota/mathfunc-part-6
Browse files Browse the repository at this point in the history
[Adding math functions] Support Erf, Erfc, Erfcinv and Erfinv math functions
  • Loading branch information
refaktor authored May 28, 2024
2 parents e3928b7 + fa9af19 commit 55354ec
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
56 changes: 56 additions & 0 deletions evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,62 @@ var Builtins_math = map[string]*env.Builtin{
}
},
},
"erf": {
Argsn: 1,
Doc: "Returns the error function of value.",
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.Erf(float64(val.Value)))
case env.Decimal:
return *env.NewDecimal(math.Erf(val.Value))
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erf")
}
},
},
"erfc": {
Argsn: 1,
Doc: "Returns the complementary error function of value.",
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.Erfc(float64(val.Value)))
case env.Decimal:
return *env.NewDecimal(math.Erfc(val.Value))
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erfc")
}
},
},
"erfcinv": {
Argsn: 1,
Doc: "Returns the inverse of erfc(x) function.",
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.Erfcinv(float64(val.Value)))
case env.Decimal:
return *env.NewDecimal(math.Erfcinv(val.Value))
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erfcinv")
}
},
},
"erfinv": {
Argsn: 1,
Doc: "Returns the inverse error function of value.",
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.Erfinv(float64(val.Value)))
case env.Decimal:
return *env.NewDecimal(math.Erfinv(val.Value))
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erfinv")
}
},
},
"pi": {
Argsn: 0,
Doc: "Return Pi constant.",
Expand Down
34 changes: 34 additions & 0 deletions tests/misc.rye
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,40 @@ section "Math functions"
equal { do\in math { dim 8 -1 } } 9.000000
equal { do\in math { dim -4 -2 } } 0.000000
}

group "erf"
mold\nowrap ""
{ { string } }
{
equal { do\in math { erf 1.5 } } 0.9661051464753108
}

group "erfc"
mold\nowrap ""
{ { string } }
{
equal { do\in math { erfc 1.5 } } 0.033894853524689274
}

group "erfcinv"
mold\nowrap ""
{ { string } }
{
equal { do\in math { erfcinv 1.5 } } -0.4769362762044699
equal { do\in math { erfcinv 0 } } "+Inf"
equal { do\in math { erfcinv 2 } } "-Inf"
equal { do\in math { erfcinv 2.2 } } "NaN"
}

group "erfinv"
mold\nowrap ""
{ { string } }
{
equal { do\in math { erfcinv 0.5 } } 0.4769362762044699
equal { do\in math { erfcinv 1 } } "+Inf"
equal { do\in math { erfcinv -1 } } "-Inf"
equal { do\in math { erfcinv 1.5 } } "NaN"
}

}

Expand Down

0 comments on commit 55354ec

Please sign in to comment.