github.com/fighterlyt/hugo@v0.47.1/tpl/math/math.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package math
    15  
    16  import (
    17  	"errors"
    18  	"math"
    19  
    20  	_math "github.com/gohugoio/hugo/common/math"
    21  
    22  	"github.com/spf13/cast"
    23  )
    24  
    25  // New returns a new instance of the math-namespaced template functions.
    26  func New() *Namespace {
    27  	return &Namespace{}
    28  }
    29  
    30  // Namespace provides template functions for the "math" namespace.
    31  type Namespace struct{}
    32  
    33  // Add adds two numbers.
    34  func (ns *Namespace) Add(a, b interface{}) (interface{}, error) {
    35  	return _math.DoArithmetic(a, b, '+')
    36  }
    37  
    38  // Ceil returns the least integer value greater than or equal to x.
    39  func (ns *Namespace) Ceil(x interface{}) (float64, error) {
    40  	xf, err := cast.ToFloat64E(x)
    41  	if err != nil {
    42  		return 0, errors.New("Ceil operator can't be used with non-float value")
    43  	}
    44  
    45  	return math.Ceil(xf), nil
    46  }
    47  
    48  // Div divides two numbers.
    49  func (ns *Namespace) Div(a, b interface{}) (interface{}, error) {
    50  	return _math.DoArithmetic(a, b, '/')
    51  }
    52  
    53  // Floor returns the greatest integer value less than or equal to x.
    54  func (ns *Namespace) Floor(x interface{}) (float64, error) {
    55  	xf, err := cast.ToFloat64E(x)
    56  	if err != nil {
    57  		return 0, errors.New("Floor operator can't be used with non-float value")
    58  	}
    59  
    60  	return math.Floor(xf), nil
    61  }
    62  
    63  // Log returns the natural logarithm of a number.
    64  func (ns *Namespace) Log(a interface{}) (float64, error) {
    65  	af, err := cast.ToFloat64E(a)
    66  
    67  	if err != nil {
    68  		return 0, errors.New("Log operator can't be used with non integer or float value")
    69  	}
    70  
    71  	return math.Log(af), nil
    72  }
    73  
    74  // Mod returns a % b.
    75  func (ns *Namespace) Mod(a, b interface{}) (int64, error) {
    76  	ai, erra := cast.ToInt64E(a)
    77  	bi, errb := cast.ToInt64E(b)
    78  
    79  	if erra != nil || errb != nil {
    80  		return 0, errors.New("Modulo operator can't be used with non integer value")
    81  	}
    82  
    83  	if bi == 0 {
    84  		return 0, errors.New("The number can't be divided by zero at modulo operation")
    85  	}
    86  
    87  	return ai % bi, nil
    88  }
    89  
    90  // ModBool returns the boolean of a % b.  If a % b == 0, return true.
    91  func (ns *Namespace) ModBool(a, b interface{}) (bool, error) {
    92  	res, err := ns.Mod(a, b)
    93  	if err != nil {
    94  		return false, err
    95  	}
    96  
    97  	return res == int64(0), nil
    98  }
    99  
   100  // Mul multiplies two numbers.
   101  func (ns *Namespace) Mul(a, b interface{}) (interface{}, error) {
   102  	return _math.DoArithmetic(a, b, '*')
   103  }
   104  
   105  // Round returns the nearest integer, rounding half away from zero.
   106  func (ns *Namespace) Round(x interface{}) (float64, error) {
   107  	xf, err := cast.ToFloat64E(x)
   108  	if err != nil {
   109  		return 0, errors.New("Round operator can't be used with non-float value")
   110  	}
   111  
   112  	return _round(xf), nil
   113  }
   114  
   115  // Sub subtracts two numbers.
   116  func (ns *Namespace) Sub(a, b interface{}) (interface{}, error) {
   117  	return _math.DoArithmetic(a, b, '-')
   118  }