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 Asin, Asinh, Atan #188

Merged
merged 2 commits into from
Apr 8, 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
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