add sqrt function (#515)

This commit is contained in:
agonist
2023-11-28 15:11:45 +08:00
committed by Asdine El Hrychy
parent cdac7cf754
commit 6b241e2bfa
3 changed files with 41 additions and 9 deletions

View File

@@ -24,6 +24,7 @@ var mathFunctions = Definitions{
"atan": atan,
"atan2": atan2,
"random": random,
"sqrt": sqrt,
}
var floor = &ScalarDefinition{
@@ -175,3 +176,19 @@ var random = &ScalarDefinition{
return types.NewIntegerValue(randomNum), nil
},
}
var sqrt = &ScalarDefinition{
name: "sqrt",
arity: 1,
callFn: func(args ...types.Value) (types.Value, error) {
if args[0].Type() != types.DoubleValue && args[0].Type() != types.IntegerValue {
return types.NewNullValue(), nil
}
v, err := document.CastAs(args[0], types.DoubleValue)
if err != nil {
return nil, err
}
res := math.Sqrt(types.As[float64](v))
return types.NewDoubleValue(res), nil
},
}