Skip to content

Commit bdfe823

Browse files
committed
Fix math.round to use HALF_UP to match doc, cel-go/cel-cpp
1 parent 14d4c2e commit bdfe823

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

extensions/src/main/java/dev/cel/extensions/CelMathExtensions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ private static double round(double x) {
874874
if (isNaN(x) || isInfinite(x)) {
875875
return x;
876876
}
877-
return DoubleMath.roundToLong(x, RoundingMode.HALF_EVEN);
877+
return DoubleMath.roundToLong(x, RoundingMode.HALF_UP);
878878
}
879879

880880
private static Number sign(Number x) {

extensions/src/test/java/dev/cel/extensions/CelMathExtensionsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,13 @@ public void floor_invalidArgs_throwsException(String expr) {
735735
@TestParameters("{expr: 'math.round(-1.5)' , expectedResult: -2.0}")
736736
@TestParameters("{expr: 'math.round(-1.2)' , expectedResult: -1.0}")
737737
@TestParameters("{expr: 'math.round(-1.6)' , expectedResult: -2.0}")
738+
// Discriminating tie cases: confirm "ties round away from zero" (HALF_UP), not
739+
// banker's rounding (HALF_EVEN). 1.5/-1.5 above don't distingish the two because
740+
// their nearest-even neighbor (2/-2) is also the away-from-zero neighbor.
741+
@TestParameters("{expr: 'math.round(0.5)' , expectedResult: 1.0}")
742+
@TestParameters("{expr: 'math.round(2.5)' , expectedResult: 3.0}")
743+
@TestParameters("{expr: 'math.round(-0.5)' , expectedResult: -1.0}")
744+
@TestParameters("{expr: 'math.round(-2.5)' , expectedResult: -3.0}")
738745
@TestParameters("{expr: 'math.round(0.0/0.0)' , expectedResult: NaN}")
739746
@TestParameters("{expr: 'math.round(1.0/0.0)' , expectedResult: Infinity}")
740747
@TestParameters("{expr: 'math.round(-1.0/0.0)' , expectedResult: -Infinity}")

0 commit comments

Comments
 (0)