Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 05ad40c

Browse filesBrowse files
CEL Dev Teamcopybara-github
CEL Dev Team
authored andcommitted
Introduce math.sqrt to CEL C++ math extension
PiperOrigin-RevId: 753155213
1 parent 3c7689d commit 05ad40c
Copy full SHA for 05ad40c

File tree

Expand file treeCollapse file tree

3 files changed

+28
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+28
-0
lines changed

‎extensions/math_ext.cc

Copy file name to clipboardExpand all lines: extensions/math_ext.cc
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ double RoundDouble(double value) { return std::round(value); }
205205

206206
double TruncDouble(double value) { return std::trunc(value); }
207207

208+
double SqrtDouble(double value) { return std::sqrt(value); }
209+
210+
double SqrtInt(int64_t value) { return std::sqrt(value); }
211+
212+
double SqrtUint(uint64_t value) { return std::sqrt(value); }
213+
208214
bool IsInfDouble(double value) { return std::isinf(value); }
209215

210216
bool IsNaNDouble(double value) { return std::isnan(value); }
@@ -364,6 +370,15 @@ absl::Status RegisterMathExtensionFunctions(FunctionRegistry& registry,
364370
CEL_RETURN_IF_ERROR(
365371
(UnaryFunctionAdapter<double, double>::RegisterGlobalOverload(
366372
"math.round", RoundDouble, registry)));
373+
CEL_RETURN_IF_ERROR(
374+
(UnaryFunctionAdapter<double, double>::RegisterGlobalOverload(
375+
"math.sqrt", SqrtDouble, registry)));
376+
CEL_RETURN_IF_ERROR(
377+
(UnaryFunctionAdapter<double, int64_t>::RegisterGlobalOverload(
378+
"math.sqrt", SqrtInt, registry)));
379+
CEL_RETURN_IF_ERROR(
380+
(UnaryFunctionAdapter<double, uint64_t>::RegisterGlobalOverload(
381+
"math.sqrt", SqrtUint, registry)));
367382
CEL_RETURN_IF_ERROR(
368383
(UnaryFunctionAdapter<double, double>::RegisterGlobalOverload(
369384
"math.trunc", TruncDouble, registry)));

‎extensions/math_ext_decls.cc

Copy file name to clipboardExpand all lines: extensions/math_ext_decls.cc
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,26 @@ absl::Status AddMinMaxDecls(TypeCheckerBuilder& builder) {
121121
absl::Status AddSignednessDecls(TypeCheckerBuilder& builder) {
122122
const Type kNumerics[] = {IntType(), DoubleType(), UintType()};
123123

124+
FunctionDecl sqrt_decl;
125+
sqrt_decl.set_name("math.sqrt");
126+
124127
FunctionDecl sign_decl;
125128
sign_decl.set_name("math.sign");
126129

127130
FunctionDecl abs_decl;
128131
abs_decl.set_name("math.abs");
129132

130133
for (const Type& type : kNumerics) {
134+
CEL_RETURN_IF_ERROR(sqrt_decl.AddOverload(
135+
MakeOverloadDecl(absl::StrCat("math_sqrt_", OverloadTypeName(type)),
136+
DoubleType(), type)));
131137
CEL_RETURN_IF_ERROR(sign_decl.AddOverload(MakeOverloadDecl(
132138
absl::StrCat("math_sign_", OverloadTypeName(type)), type, type)));
133139
CEL_RETURN_IF_ERROR(abs_decl.AddOverload(MakeOverloadDecl(
134140
absl::StrCat("math_abs_", OverloadTypeName(type)), type, type)));
135141
}
136142

143+
CEL_RETURN_IF_ERROR(builder.AddFunction(sqrt_decl));
137144
CEL_RETURN_IF_ERROR(builder.AddFunction(sign_decl));
138145
CEL_RETURN_IF_ERROR(builder.AddFunction(abs_decl));
139146

‎extensions/math_ext_test.cc

Copy file name to clipboardExpand all lines: extensions/math_ext_test.cc
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ INSTANTIATE_TEST_SUITE_P(
550550
{"math.ceil(42.01) == 43.0"},
551551
{"math.floor(42.01) == 42.0"},
552552
{"math.round(42.5) == 43.0"},
553+
{"math.sqrt(49.0) == 7.0"},
554+
{"math.sqrt(0) == 0.0"},
555+
{"math.sqrt(1) == 1.0"},
556+
{"math.sqrt(25u) == 5.0"},
557+
{"math.sqrt(38.44) == 6.2"},
558+
{"math.isNaN(math.sqrt(-15)) == true"},
553559
{"math.trunc(42.0) == 42.0"},
554560
{"math.isInf(42.0 / 0.0) == true"},
555561
{"math.isNaN(double('nan')) == true"},

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.