summaryrefslogtreecommitdiffstats
path: root/tpl/math/math.go
diff options
context:
space:
mode:
authorOleksandr Redko <Oleksandr_Redko@epam.com>2023-05-16 19:32:07 +0300
committerGitHub <noreply@github.com>2023-05-16 18:32:07 +0200
commitbda082c98c7a0abdb019f12bb21cbaa7b0e8f60e (patch)
tree264dad483cd16238fa1864c7c3f127d4f72d24f4 /tpl/math/math.go
parent241b21b0fd34d91fccb2ce69874110dceae6f926 (diff)
tpl: Add math.Abs
Fixes #10941.
Diffstat (limited to 'tpl/math/math.go')
-rw-r--r--tpl/math/math.go10
1 files changed, 10 insertions, 0 deletions
diff --git a/tpl/math/math.go b/tpl/math/math.go
index 67c6d06c5..d73f212a6 100644
--- a/tpl/math/math.go
+++ b/tpl/math/math.go
@@ -35,6 +35,16 @@ func New() *Namespace {
// Namespace provides template functions for the "math" namespace.
type Namespace struct{}
+// Abs returns the absolute value of n.
+func (ns *Namespace) Abs(n any) (float64, error) {
+ af, err := cast.ToFloat64E(n)
+ if err != nil {
+ return 0, errors.New("the math.Abs function requires a numeric argument")
+ }
+
+ return math.Abs(af), nil
+}
+
// Add adds the multivalued addends n1 and n2 or more values.
func (ns *Namespace) Add(inputs ...any) (any, error) {
return ns.doArithmetic(inputs, '+')