From d8d85880d3b1f951d5bb6339c0eecf2d43827016 Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Sun, 7 Apr 2024 19:24:00 +0530 Subject: [PATCH 1/2] adding math functions for asin asinh atan --- evaldo/builtins_math.go | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/evaldo/builtins_math.go b/evaldo/builtins_math.go index 760d8371..36878ca1 100644 --- a/evaldo/builtins_math.go +++ b/evaldo/builtins_math.go @@ -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.", From 2e3894737d0eeaca028040fe0dd58fa8e7525f11 Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Sun, 7 Apr 2024 19:48:31 +0530 Subject: [PATCH 2/2] adding test cases --- tests/misc.rye | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/misc.rye b/tests/misc.rye index c2c8d477..5973f9b7 100644 --- a/tests/misc.rye +++ b/tests/misc.rye @@ -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 }