Skip to content

Commit

Permalink
Merge pull request #188 from pratikmota/mathfunc-part-2
Browse files Browse the repository at this point in the history
[Adding math function] Support for Asin, Asinh, Atan
  • Loading branch information
refaktor authored Apr 8, 2024
2 parents 311bbff + 2e38947 commit 66bdf72
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
48 changes: 48 additions & 0 deletions evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,54 @@ var Builtins_math = map[string]*env.Builtin{
}
},
},
"asin": {
Argsn: 1,
Doc: "Returns the arcsine (inverse sine).",
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: Asin is only defined for -1 <= x <= 1.", "asin")
}
return *env.NewDecimal(math.Asin(float64(val.Value)))
case env.Decimal:
if val.Value < -1.0 || val.Value > 1.0 {
return MakeBuiltinError(ps, "Invalid input: Asin is only defined for -1 <= x <= 1.", "asin")
}
return *env.NewDecimal(math.Asin(val.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "asin")
}
},
},
"asinh": {
Argsn: 1,
Doc: "Returns the inverse hyperbolic sine.",
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.Log(float64(val.Value) + math.Sqrt(float64(val.Value)*float64(val.Value)+1)))
case env.Decimal:
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}, "asinh")
}
},
},
"atan": {
Argsn: 1,
Doc: "Returns the arctangent (inverse tangent).",
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.Atan(float64(val.Value)))
case env.Decimal:
return *env.NewDecimal(math.Atan(val.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "atan")
}
},
},
"pi": {
Argsn: 0,
Doc: "Return Pi constant.",
Expand Down
29 changes: 29 additions & 0 deletions tests/misc.rye
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,35 @@ section "Math functions"
equal { do\in math { acosh 2 } } 1.3169578969248166
}

group "asin"
mold\nowrap ""
{ { string } }
{
equal { do\in math { asin -1 } } -1.5707963267948966
equal { do\in math { asin 0 } } 0
equal { do\in math { asin 1 } } 1.5707963267948966
}

group "asinh"
mold\nowrap ""
{ { string } }
{
equal { do\in math { asinh -1 } } -0.8813735870195428
equal { do\in math { asinh 0 } } 0
equal { do\in math { asinh 1 } } 0.8813735870195429
}


group "atan"
mold\nowrap ""
{ { string } }
{
equal { do\in math { atan -1 } } -0.7853981633974483
equal { do\in math { atan 0 } } 0
equal { do\in math { atan 1 } } 0.7853981633974483
}


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

}
Expand Down

0 comments on commit 66bdf72

Please sign in to comment.