mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-22 21:42:50 +02:00
tpl/math: Add log function
It might be very useful for building tag clouds.
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
41805dca9e
commit
34c566773a
@@ -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)
|
||||
|
Reference in New Issue
Block a user