Skip to content

Commit

Permalink
feat: Add more math functions (google#702)
Browse files Browse the repository at this point in the history
Co-authored-by: Dave Cunningham <[email protected]>
  • Loading branch information
2 people authored and vhata committed Aug 30, 2024
1 parent 53bd8fd commit 331e71c
Show file tree
Hide file tree
Showing 26 changed files with 50 additions and 0 deletions.
30 changes: 30 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,16 @@ func liftNumeric(f func(float64) float64) func(*interpreter, value) (value, erro
}
}

func liftNumericToBoolean(f func(float64) bool) func(*interpreter, value) (value, error) {
return func(i *interpreter, x value) (value, error) {
n, err := i.getNumber(x)
if err != nil {
return nil, err
}
return makeValueBoolean(f(n.value)), nil
}
}

var builtinSqrt = liftNumeric(math.Sqrt)
var builtinCeil = liftNumeric(math.Ceil)
var builtinFloor = liftNumeric(math.Floor)
Expand All @@ -1125,6 +1135,22 @@ var builtinExponent = liftNumeric(func(f float64) float64 {
return float64(exponent)
})
var builtinRound = liftNumeric(math.Round)
var builtinIsEven = liftNumericToBoolean(func(f float64) bool {
i, _ := math.Modf(f) // Get the integral part of the float
return math.Mod(i, 2) == 0
})
var builtinIsOdd = liftNumericToBoolean(func(f float64) bool {
i, _ := math.Modf(f) // Get the integral part of the float
return math.Mod(i, 2) != 0
})
var builtinIsInteger = liftNumericToBoolean(func(f float64) bool {
_, frac := math.Modf(f) // Get the fraction part of the float
return frac == 0
})
var builtinIsDecimal = liftNumericToBoolean(func(f float64) bool {
_, frac := math.Modf(f) // Get the fraction part of the float
return frac != 0
})

func liftBitwise(f func(int64, int64) int64, positiveRightArg bool) func(*interpreter, value, value) (value, error) {
return func(i *interpreter, xv, yv value) (value, error) {
Expand Down Expand Up @@ -2467,6 +2493,10 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&unaryBuiltin{name: "mantissa", function: builtinMantissa, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "exponent", function: builtinExponent, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "round", function: builtinRound, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "isEven", function: builtinIsEven, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "isOdd", function: builtinIsOdd, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "isInteger", function: builtinIsInteger, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "isDecimal", function: builtinIsDecimal, params: ast.Identifiers{"x"}},
&binaryBuiltin{name: "pow", function: builtinPow, params: ast.Identifiers{"x", "n"}},
&binaryBuiltin{name: "modulo", function: builtinModulo, params: ast.Identifiers{"x", "y"}},
&unaryBuiltin{name: "md5", function: builtinMd5, params: ast.Identifiers{"s"}},
Expand Down
4 changes: 4 additions & 0 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func prepareStdlib(g *typeGraph) {
"isNumber": g.newSimpleFuncType(boolType, "v"),
"isObject": g.newSimpleFuncType(boolType, "v"),
"isString": g.newSimpleFuncType(boolType, "v"),
"isEven": g.newSimpleFuncType(boolType, "x"),
"isOdd": g.newSimpleFuncType(boolType, "x"),
"isInteger": g.newSimpleFuncType(boolType, "x"),
"isDecimal": g.newSimpleFuncType(boolType, "x"),

// Mathematical utilities
"abs": g.newSimpleFuncType(numberType, "n"),
Expand Down
1 change: 1 addition & 0 deletions testdata/builtinIsDecimal.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinIsDecimal.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isDecimal(1.1)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsDecimal2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinIsDecimal2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isDecimal(1)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsEven.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinIsEven.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isEven(10)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsEven2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinIsEven2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isEven(5)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsInteger.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinIsInteger.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isInteger(1)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsInteger2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinIsInteger2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isInteger(1.1)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsOdd.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinIsOdd.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isOdd(5)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsOdd2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinIsOdd2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isOdd(10)
Empty file.

0 comments on commit 331e71c

Please sign in to comment.