-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
math_roundhalfdown.go
56 lines (48 loc) · 1.62 KB
/
math_roundhalfdown.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Code generated by __generator__/interpreter.go at once
package builtin
import (
"math"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/interpreter/value"
)
const Math_roundhalfdown_Name = "math.roundhalfdown"
var Math_roundhalfdown_ArgumentTypes = []value.Type{value.FloatType}
func Math_roundhalfdown_Validate(args []value.Value) error {
if len(args) != 1 {
return errors.ArgumentNotEnough(Math_roundhalfdown_Name, 1, args)
}
for i := range args {
if args[i].Type() != Math_roundhalfdown_ArgumentTypes[i] {
return errors.TypeMismatch(Math_roundhalfdown_Name, i+1, Math_roundhalfdown_ArgumentTypes[i], args[i].Type())
}
}
return nil
}
// Fastly built-in function implementation of math.roundhalfdown
// Arguments may be:
// - FLOAT
// Reference: https://developer.fastly.com/reference/vcl/functions/math-rounding/math-roundhalfdown/
func Math_roundhalfdown(ctx *context.Context, args ...value.Value) (value.Value, error) {
// Argument validations
if err := Math_roundhalfdown_Validate(args); err != nil {
return value.Null, err
}
x := value.Unwrap[*value.Float](args[0])
switch {
case x.IsNAN:
return &value.Float{IsNAN: true}, nil
case x.IsNegativeInf:
return &value.Float{IsNegativeInf: true}, nil
case x.IsPositiveInf:
return &value.Float{IsPositiveInf: true}, nil
case x.Value == 0:
return &value.Float{Value: x.Value}, nil
default:
t := math.Trunc(x.Value)
if d := math.Abs(x.Value - t); d <= 0.5 {
return &value.Float{Value: t}, nil
}
return &value.Float{Value: t + math.Copysign(1, x.Value)}, nil
}
}