| 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 provides template functions for mathematical operations. |
| 15 |
package math |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"math" |
| 21 |
"math/rand" |
| 22 |
"reflect" |
| 23 |
|
| 24 |
_math "github.com/gohugoio/hugo/common/math" |
| 25 |
"github.com/gohugoio/hugo/deps" |
| 26 |
"github.com/spf13/cast" |
| 27 |
) |
| 28 |
|
| 29 |
var ( |
| 30 |
errMustTwoNumbersError = errors.New("must provide at least two numbers") |
| 31 |
errMustOneNumberError = errors.New("must provide at least one number") |
| 32 |
) |
| 33 |
|
| 34 |
// New returns a new instance of the math-namespaced template functions. |
| 35 |
func New(d *deps.Deps) *Namespace { |
| 36 |
return &Namespace{ |
| 37 |
d: d, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// Namespace provides template functions for the "math" namespace. |
| 42 |
type Namespace struct { |
| 43 |
d *deps.Deps |
| 44 |
} |
| 45 |
|
| 46 |
// Abs returns the absolute value of n. |
| 47 |
func (ns *Namespace) Abs(n any) (float64, error) { |
| 48 |
af, err := cast.ToFloat64E(n) |
| 49 |
if err != nil { |
| 50 |
return 0, errors.New("the math.Abs function requires a numeric argument") |
| 51 |
} |
| 52 |
|
| 53 |
return math.Abs(af), nil |
| 54 |
} |
| 55 |
|
| 56 |
// Acos returns the arccosine, in radians, of n. |
| 57 |
func (ns *Namespace) Acos(n any) (float64, error) { |
| 58 |
af, err := cast.ToFloat64E(n) |
| 59 |
if err != nil { |
| 60 |
return 0, errors.New("requires a numeric argument") |
| 61 |
} |
| 62 |
return math.Acos(af), nil |
| 63 |
} |
| 64 |
|
| 65 |
// Add adds the multivalued addends n1 and n2 or more values. |
| 66 |
func (ns *Namespace) Add(inputs ...any) (any, error) { |
| 67 |
return ns.doArithmetic(inputs, '+') |
| 68 |
} |
| 69 |
|
| 70 |
// Asin returns the arcsine, in radians, of n. |
| 71 |
func (ns *Namespace) Asin(n any) (float64, error) { |
| 72 |
af, err := cast.ToFloat64E(n) |
| 73 |
if err != nil { |
| 74 |
return 0, errors.New("requires a numeric argument") |
| 75 |
} |
| 76 |
return math.Asin(af), nil |
| 77 |
} |
| 78 |
|
| 79 |
// Atan returns the arctangent, in radians, of n. |
| 80 |
func (ns *Namespace) Atan(n any) (float64, error) { |
| 81 |
af, err := cast.ToFloat64E(n) |
| 82 |
if err != nil { |
| 83 |
return 0, errors.New("requires a numeric argument") |
| 84 |
} |
| 85 |
return math.Atan(af), nil |
| 86 |
} |
| 87 |
|
| 88 |
// Atan2 returns the arc tangent of n/m, using the signs of the two to determine the quadrant of the return value. |
| 89 |
func (ns *Namespace) Atan2(n, m any) (float64, error) { |
| 90 |
afx, err := cast.ToFloat64E(n) |
| 91 |
if err != nil { |
| 92 |
return 0, errors.New("requires numeric arguments") |
| 93 |
} |
| 94 |
afy, err := cast.ToFloat64E(m) |
| 95 |
if err != nil { |
| 96 |
return 0, errors.New("requires numeric arguments") |
| 97 |
} |
| 98 |
return math.Atan2(afx, afy), nil |
| 99 |
} |
| 100 |
|
| 101 |
// Ceil returns the least integer value greater than or equal to n. |
| 102 |
func (ns *Namespace) Ceil(n any) (float64, error) { |
| 103 |
xf, err := cast.ToFloat64E(n) |
| 104 |
if err != nil { |
| 105 |
return 0, errors.New("Ceil operator can't be used with non-float value") |
| 106 |
} |
| 107 |
|
| 108 |
return math.Ceil(xf), nil |
| 109 |
} |
| 110 |
|
| 111 |
// Cos returns the cosine of the radian argument n. |
| 112 |
func (ns *Namespace) Cos(n any) (float64, error) { |
| 113 |
af, err := cast.ToFloat64E(n) |
| 114 |
if err != nil { |
| 115 |
return 0, errors.New("requires a numeric argument") |
| 116 |
} |
| 117 |
return math.Cos(af), nil |
| 118 |
} |
| 119 |
|
| 120 |
// Div divides n1 by n2. |
| 121 |
func (ns *Namespace) Div(inputs ...any) (any, error) { |
| 122 |
return ns.doArithmetic(inputs, '/') |
| 123 |
} |
| 124 |
|
| 125 |
// Floor returns the greatest integer value less than or equal to n. |
| 126 |
func (ns *Namespace) Floor(n any) (float64, error) { |
| 127 |
xf, err := cast.ToFloat64E(n) |
| 128 |
if err != nil { |
| 129 |
return 0, errors.New("Floor operator can't be used with non-float value") |
| 130 |
} |
| 131 |
|
| 132 |
return math.Floor(xf), nil |
| 133 |
} |
| 134 |
|
| 135 |
// Log returns the natural logarithm of the number n. |
| 136 |
func (ns *Namespace) Log(n any) (float64, error) { |
| 137 |
af, err := cast.ToFloat64E(n) |
| 138 |
if err != nil { |
| 139 |
return 0, errors.New("Log operator can't be used with non integer or float value") |
| 140 |
} |
| 141 |
|
| 142 |
return math.Log(af), nil |
| 143 |
} |
| 144 |
|
| 145 |
// Max returns the greater of all numbers in inputs. Any slices in inputs are flattened. |
| 146 |
func (ns *Namespace) Max(inputs ...any) (maximum float64, err error) { |
| 147 |
return ns.applyOpToScalarsOrSlices("Max", math.Max, inputs...) |
| 148 |
} |
| 149 |
|
| 150 |
// MaxInt64 returns the maximum value for a signed 64-bit integer. |
| 151 |
func (ns *Namespace) MaxInt64() int64 { |
| 152 |
return math.MaxInt64 |
| 153 |
} |
| 154 |
|
| 155 |
// Min returns the smaller of all numbers in inputs. Any slices in inputs are flattened. |
| 156 |
func (ns *Namespace) Min(inputs ...any) (minimum float64, err error) { |
| 157 |
return ns.applyOpToScalarsOrSlices("Min", math.Min, inputs...) |
| 158 |
} |
| 159 |
|
| 160 |
// Mod returns n1 % n2. |
| 161 |
func (ns *Namespace) Mod(n1, n2 any) (int64, error) { |
| 162 |
ai, erra := cast.ToInt64E(n1) |
| 163 |
bi, errb := cast.ToInt64E(n2) |
| 164 |
|
| 165 |
if erra != nil || errb != nil { |
| 166 |
return 0, errors.New("modulo operator can't be used with non integer value") |
| 167 |
} |
| 168 |
|
| 169 |
if bi == 0 { |
| 170 |
return 0, errors.New("the number can't be divided by zero at modulo operation") |
| 171 |
} |
| 172 |
|
| 173 |
return ai % bi, nil |
| 174 |
} |
| 175 |
|
| 176 |
// ModBool returns the boolean of n1 % n2. If n1 % n2 == 0, return true. |
| 177 |
func (ns *Namespace) ModBool(n1, n2 any) (bool, error) { |
| 178 |
res, err := ns.Mod(n1, n2) |
| 179 |
if err != nil { |
| 180 |
return false, err |
| 181 |
} |
| 182 |
|
| 183 |
return res == int64(0), nil |
| 184 |
} |
| 185 |
|
| 186 |
// Mul multiplies the multivalued numbers n1 and n2 or more values. |
| 187 |
func (ns *Namespace) Mul(inputs ...any) (any, error) { |
| 188 |
return ns.doArithmetic(inputs, '*') |
| 189 |
} |
| 190 |
|
| 191 |
// Pi returns the mathematical constant pi. |
| 192 |
func (ns *Namespace) Pi() float64 { |
| 193 |
return math.Pi |
| 194 |
} |
| 195 |
|
| 196 |
// Pow returns n1 raised to the power of n2. |
| 197 |
func (ns *Namespace) Pow(n1, n2 any) (float64, error) { |
| 198 |
af, erra := cast.ToFloat64E(n1) |
| 199 |
bf, errb := cast.ToFloat64E(n2) |
| 200 |
|
| 201 |
if erra != nil || errb != nil { |
| 202 |
return 0, errors.New("Pow operator can't be used with non-float value") |
| 203 |
} |
| 204 |
|
| 205 |
return math.Pow(af, bf), nil |
| 206 |
} |
| 207 |
|
| 208 |
// Product returns the product of all numbers in inputs. Any slices in inputs are flattened. |
| 209 |
func (ns *Namespace) Product(inputs ...any) (product float64, err error) { |
| 210 |
fn := func(x, y float64) float64 { |
| 211 |
return x * y |
| 212 |
} |
| 213 |
return ns.applyOpToScalarsOrSlices("Product", fn, inputs...) |
| 214 |
} |
| 215 |
|
| 216 |
// Rand returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0). |
| 217 |
func (ns *Namespace) Rand() float64 { |
| 218 |
return rand.Float64() |
| 219 |
} |
| 220 |
|
| 221 |
// Round returns the integer nearest to n, rounding half away from zero. |
| 222 |
func (ns *Namespace) Round(n any) (float64, error) { |
| 223 |
xf, err := cast.ToFloat64E(n) |
| 224 |
if err != nil { |
| 225 |
return 0, errors.New("Round operator can't be used with non-float value") |
| 226 |
} |
| 227 |
|
| 228 |
return _round(xf), nil |
| 229 |
} |
| 230 |
|
| 231 |
// Sin returns the sine of the radian argument n. |
| 232 |
func (ns *Namespace) Sin(n any) (float64, error) { |
| 233 |
af, err := cast.ToFloat64E(n) |
| 234 |
if err != nil { |
| 235 |
return 0, errors.New("requires a numeric argument") |
| 236 |
} |
| 237 |
return math.Sin(af), nil |
| 238 |
} |
| 239 |
|
| 240 |
// Sqrt returns the square root of the number n. |
| 241 |
func (ns *Namespace) Sqrt(n any) (float64, error) { |
| 242 |
af, err := cast.ToFloat64E(n) |
| 243 |
if err != nil { |
| 244 |
return 0, errors.New("Sqrt operator can't be used with non integer or float value") |
| 245 |
} |
| 246 |
|
| 247 |
return math.Sqrt(af), nil |
| 248 |
} |
| 249 |
|
| 250 |
// Sub subtracts multivalued. |
| 251 |
func (ns *Namespace) Sub(inputs ...any) (any, error) { |
| 252 |
return ns.doArithmetic(inputs, '-') |
| 253 |
} |
| 254 |
|
| 255 |
// Sum returns the sum of all numbers in inputs. Any slices in inputs are flattened. |
| 256 |
func (ns *Namespace) Sum(inputs ...any) (sum float64, err error) { |
| 257 |
fn := func(x, y float64) float64 { |
| 258 |
return x + y |
| 259 |
} |
| 260 |
return ns.applyOpToScalarsOrSlices("Sum", fn, inputs...) |
| 261 |
} |
| 262 |
|
| 263 |
// Tan returns the tangent of the radian argument n. |
| 264 |
func (ns *Namespace) Tan(n any) (float64, error) { |
| 265 |
af, err := cast.ToFloat64E(n) |
| 266 |
if err != nil { |
| 267 |
return 0, errors.New("requires a numeric argument") |
| 268 |
} |
| 269 |
return math.Tan(af), nil |
| 270 |
} |
| 271 |
|
| 272 |
// ToDegrees converts radians into degrees. |
| 273 |
func (ns *Namespace) ToDegrees(n any) (float64, error) { |
| 274 |
af, err := cast.ToFloat64E(n) |
| 275 |
if err != nil { |
| 276 |
return 0, errors.New("requires a numeric argument") |
| 277 |
} |
| 278 |
|
| 279 |
return af * 180 / math.Pi, nil |
| 280 |
} |
| 281 |
|
| 282 |
// ToRadians converts degrees into radians. |
| 283 |
func (ns *Namespace) ToRadians(n any) (float64, error) { |
| 284 |
af, err := cast.ToFloat64E(n) |
| 285 |
if err != nil { |
| 286 |
return 0, errors.New("requires a numeric argument") |
| 287 |
} |
| 288 |
|
| 289 |
return af * math.Pi / 180, nil |
| 290 |
} |
| 291 |
|
| 292 |
func (ns *Namespace) applyOpToScalarsOrSlices(opName string, op func(x, y float64) float64, inputs ...any) (result float64, err error) { |
| 293 |
var i int |
| 294 |
var hasValue bool |
| 295 |
for _, input := range inputs { |
| 296 |
var values []float64 |
| 297 |
var isSlice bool |
| 298 |
values, isSlice, err = ns.toFloatsE(input) |
| 299 |
if err != nil { |
| 300 |
err = fmt.Errorf("%s operator can't be used with non-float values", opName) |
| 301 |
return |
| 302 |
} |
| 303 |
hasValue = hasValue || len(values) > 0 || isSlice |
| 304 |
for _, value := range values { |
| 305 |
i++ |
| 306 |
if i == 1 { |
| 307 |
result = value |
| 308 |
continue |
| 309 |
} |
| 310 |
result = op(result, value) |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if !hasValue { |
| 315 |
err = errMustOneNumberError |
| 316 |
return |
| 317 |
} |
| 318 |
return |
| 319 |
} |
| 320 |
|
| 321 |
func (ns *Namespace) toFloatsE(v any) ([]float64, bool, error) { |
| 322 |
vv := reflect.ValueOf(v) |
| 323 |
switch vv.Kind() { |
| 324 |
case reflect.Slice, reflect.Array: |
| 325 |
var floats []float64 |
| 326 |
for i := range vv.Len() { |
| 327 |
f, err := cast.ToFloat64E(vv.Index(i).Interface()) |
| 328 |
if err != nil { |
| 329 |
return nil, true, err |
| 330 |
} |
| 331 |
floats = append(floats, f) |
| 332 |
} |
| 333 |
return floats, true, nil |
| 334 |
default: |
| 335 |
f, err := cast.ToFloat64E(v) |
| 336 |
if err != nil { |
| 337 |
return nil, false, err |
| 338 |
} |
| 339 |
return []float64{f}, false, nil |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
func (ns *Namespace) doArithmetic(inputs []any, operation rune) (value any, err error) { |
| 344 |
if len(inputs) < 2 { |
| 345 |
return nil, errMustTwoNumbersError |
| 346 |
} |
| 347 |
value = inputs[0] |
| 348 |
for i := 1; i < len(inputs); i++ { |
| 349 |
value, err = _math.DoArithmetic(value, inputs[i], operation) |
| 350 |
if err != nil { |
| 351 |
return |
| 352 |
} |
| 353 |
} |
| 354 |
return |
| 355 |
} |
| 356 |
|
| 357 |
// Counter increments and returns a global counter. |
| 358 |
// This was originally added to be used in tests where now.UnixNano did not |
| 359 |
// have the needed precision (especially on Windows). |
| 360 |
// Note that given the parallel nature of Hugo, you cannot use this to get sequences of numbers, |
| 361 |
// and the counter will reset on new builds. |
| 362 |
// <docsmeta>{"identifiers": ["now.UnixNano"] }</docsmeta> |
| 363 |
func (ns *Namespace) Counter() uint64 { |
| 364 |
return ns.d.Counters.MathCounter.Add(1) |
| 365 |
} |