tpl/math: Add log function

It might be very useful for building tag clouds.
This commit is contained in:
Artem Sidorenko
2017-07-03 00:20:49 +02:00
committed by Bjørn Erik Pedersen
parent 41805dca9e
commit 34c566773a
4 changed files with 64 additions and 1 deletions

View File

@@ -15,7 +15,10 @@ package math
import (
"errors"
"math"
"reflect"
"github.com/spf13/cast"
)
// New returns a new instance of the math-namespaced template functions.
@@ -34,6 +37,16 @@ func (ns *Namespace) Div(a, b interface{}) (interface{}, error) {
return DoArithmetic(a, b, '/')
}
func (ns *Namespace) Log(a interface{}) (float64, error) {
af, err := cast.ToFloat64E(a)
if err != nil {
return 0, errors.New("Log operator can't be used with non integer or float value")
}
return math.Log(af), nil
}
// Mod returns a % b.
func (ns *Namespace) Mod(a, b interface{}) (int64, error) {
av := reflect.ValueOf(a)