mirror of
https://github.com/chaisql/chai.git
synced 2025-10-28 01:41:35 +08:00
add sqrt function (#515)
This commit is contained in:
committed by
Asdine El Hrychy
parent
cdac7cf754
commit
6b241e2bfa
@@ -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
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user