diff --git a/evaldo/builtins_math.go b/evaldo/builtins_math.go index e72ca2a8..760d8371 100644 --- a/evaldo/builtins_math.go +++ b/evaldo/builtins_math.go @@ -183,6 +183,46 @@ var Builtins_math = map[string]*env.Builtin{ } }, }, + "acos": { + Argsn: 1, + Doc: "Returns the arccosine.", + 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: Acos is only defined for -1 <= x <= 1.", "acos") + } + return *env.NewDecimal(math.Acos(float64(val.Value))) + case env.Decimal: + if val.Value < -1.0 || val.Value > 1.0 { + return MakeBuiltinError(ps, "Invalid input: Acos is only defined for -1 <= x <= 1.", "acos") + } + return *env.NewDecimal(math.Acos(val.Value)) + default: + return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "acos") + } + }, + }, + "acosh": { + Argsn: 1, + Doc: "Returns the inverse hyperbolic cosine.", + 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 { + return MakeBuiltinError(ps, " Acosh is only defined for x >= 1.", "acosh") + } + return *env.NewDecimal(math.Log(float64(val.Value) + math.Sqrt(float64(val.Value)*float64(val.Value)-1))) + case env.Decimal: + if val.Value < 1.0 { + return MakeBuiltinError(ps, " Acosh is only defined for x >= 1.", "acosh") + } + 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}, "acosh") + } + }, + }, "pi": { Argsn: 0, Doc: "Return Pi constant.", diff --git a/tests/basics.rye b/tests/basics.rye index 36261941..3bcd9733 100644 --- a/tests/basics.rye +++ b/tests/basics.rye @@ -22,14 +22,16 @@ section "Printing functions" { stdout { prns 123 } "123 " } - group "print\val" - mold\nowrap ?print\val - { { object } } - { - stdout { print\val 33 "value is: {{}}" } "value is: 33" + newline + + ;group "print\val" + ;mold\nowrap ?print\val + ;{ { object } } + ;{ + ; stdout { print\val 33 "value is: {{}}" } "value is: 33" + newline ; stdout { print\val "OK" "value is: {{}}" } "value is: 33" + newline ; TODO-BUG quotes the string ; stdout { { "Jane Austen" } print\val "Hello {{}}!" } "value is: 33" + newline - } + ;} + ; group "print-ssv" ; mold\nowrap ?print-ssv ; { { function } } diff --git a/tests/misc.rye b/tests/misc.rye index c421d19d..c2c8d477 100644 --- a/tests/misc.rye +++ b/tests/misc.rye @@ -338,6 +338,23 @@ section "Math functions" equal { do\in math { abs 99.0 } } 99.0 } + group "acos" + mold\nowrap "" + { { string } } + { + equal { do\in math { acos -1 } } 3.141592653589793 + equal { do\in math { acos 0.5 } } 1.0471975511965976 + equal { do\in math { acos 1 } } 0.000000 + } + + group "acosh" + mold\nowrap "" + { { string } } + { + equal { do\in math { acosh 1 } } 0.000000 + equal { do\in math { acosh 2 } } 1.3169578969248166 + } + ; TODO add sin and cos ... need PI constant }