@@ -1059,6 +1059,16 @@ func liftNumeric(f func(float64) float64) func(*interpreter, value) (value, erro
1059
1059
}
1060
1060
}
1061
1061
1062
+ func liftNumericToBoolean (f func (float64 ) bool ) func (* interpreter , value ) (value , error ) {
1063
+ return func (i * interpreter , x value ) (value , error ) {
1064
+ n , err := i .getNumber (x )
1065
+ if err != nil {
1066
+ return nil , err
1067
+ }
1068
+ return makeValueBoolean (f (n .value )), nil
1069
+ }
1070
+ }
1071
+
1062
1072
var builtinSqrt = liftNumeric (math .Sqrt )
1063
1073
var builtinCeil = liftNumeric (math .Ceil )
1064
1074
var builtinFloor = liftNumeric (math .Floor )
@@ -1085,6 +1095,22 @@ var builtinExponent = liftNumeric(func(f float64) float64 {
1085
1095
return float64 (exponent )
1086
1096
})
1087
1097
var builtinRound = liftNumeric (math .Round )
1098
+ var builtinIsEven = liftNumericToBoolean (func (f float64 ) bool {
1099
+ i , _ := math .Modf (f ) // Get the integral part of the float
1100
+ return math .Mod (i , 2 ) == 0
1101
+ })
1102
+ var builtinIsOdd = liftNumericToBoolean (func (f float64 ) bool {
1103
+ i , _ := math .Modf (f ) // Get the integral part of the float
1104
+ return math .Mod (i , 2 ) != 0
1105
+ })
1106
+ var builtinIsInteger = liftNumericToBoolean (func (f float64 ) bool {
1107
+ _ , frac := math .Modf (f ) // Get the fraction part of the float
1108
+ return frac == 0
1109
+ })
1110
+ var builtinIsDecimal = liftNumericToBoolean (func (f float64 ) bool {
1111
+ _ , frac := math .Modf (f ) // Get the fraction part of the float
1112
+ return frac != 0
1113
+ })
1088
1114
1089
1115
func liftBitwise (f func (int64 , int64 ) int64 , positiveRightArg bool ) func (* interpreter , value , value ) (value , error ) {
1090
1116
return func (i * interpreter , xv , yv value ) (value , error ) {
@@ -2394,6 +2420,10 @@ var funcBuiltins = buildBuiltinMap([]builtin{
2394
2420
& unaryBuiltin {name : "mantissa" , function : builtinMantissa , params : ast.Identifiers {"x" }},
2395
2421
& unaryBuiltin {name : "exponent" , function : builtinExponent , params : ast.Identifiers {"x" }},
2396
2422
& unaryBuiltin {name : "round" , function : builtinRound , params : ast.Identifiers {"x" }},
2423
+ & unaryBuiltin {name : "isEven" , function : builtinIsEven , params : ast.Identifiers {"x" }},
2424
+ & unaryBuiltin {name : "isOdd" , function : builtinIsOdd , params : ast.Identifiers {"x" }},
2425
+ & unaryBuiltin {name : "isInteger" , function : builtinIsInteger , params : ast.Identifiers {"x" }},
2426
+ & unaryBuiltin {name : "isDecimal" , function : builtinIsDecimal , params : ast.Identifiers {"x" }},
2397
2427
& binaryBuiltin {name : "pow" , function : builtinPow , params : ast.Identifiers {"x" , "n" }},
2398
2428
& binaryBuiltin {name : "modulo" , function : builtinModulo , params : ast.Identifiers {"x" , "y" }},
2399
2429
& unaryBuiltin {name : "md5" , function : builtinMd5 , params : ast.Identifiers {"s" }},
0 commit comments