Skip to content

Commit f3a1b2b

Browse files
l46kokcopybara-github
authored andcommitted
Fix signed zero equality behavior for doubles to match specification
PiperOrigin-RevId: 809130214
1 parent f174988 commit f3a1b2b

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

common/src/main/java/dev/cel/common/internal/ComparisonFunctions.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public static int compareUintInt(UnsignedLong ul, long l) {
8181
public static boolean numericEquals(Number x, Number y) {
8282
if (x instanceof Double) {
8383
if (y instanceof Double) {
84-
return !(Double.isNaN((Double) x) || Double.isNaN((Double) y)) && x.equals(y);
84+
return !(Double.isNaN((Double) x) || Double.isNaN((Double) y))
85+
&& x.doubleValue() == y.doubleValue();
8586
}
8687
if (y instanceof Long) {
8788
return compareDoubleInt((Double) x, (Long) y) == 0;
@@ -115,10 +116,7 @@ public static boolean numericEquals(Number x, Number y) {
115116
return false;
116117
}
117118

118-
119-
/**
120-
* Compare two numeric values of any type (double, int, uint).
121-
*/
119+
/** Compare two numeric values of any type (double, int, uint). */
122120
public static int numericCompare(Number x, Number y) {
123121
if (x instanceof Double) {
124122
if (y instanceof Double) {

runtime/src/test/resources/arithmDouble.baseline

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Source: 0.0 == -0.0
2+
=====>
3+
bindings: {}
4+
result: true
5+
16
Source: 1.9 < 2.0 && 1.1 <= 1.1 && 2.0 > 1.9 && 1.1 >= 1.1 && 1.1 == 1.1 && 2.0 != 1.9
27
=====>
38
bindings: {}

testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ public void arithmUInt64_error() {
306306

307307
@Test
308308
public void arithmDouble() {
309+
source = "0.0 == -0.0";
310+
runTest();
311+
309312
source = "1.9 < 2.0 && 1.1 <= 1.1 && 2.0 > 1.9 && 1.1 >= 1.1 && 1.1 == 1.1 && 2.0 != 1.9";
310313
runTest();
311314

0 commit comments

Comments
 (0)