From 8ff24babd31ef9caab6f24f303b7bd7528e2954b Mon Sep 17 00:00:00 2001 From: Jim Larson <32469398+JimLarson@users.noreply.github.com> Date: Wed, 2 Jun 2021 12:02:50 -0700 Subject: [PATCH] Convert double to integer via truncation. (#192) * Convert double to integer via truncation. Matches behavior of C, Go, and Java. Introduce a round() function for round-half-away like GoogleSQL. * Address review feedback. Defer round() until another PR. Co-authored-by: Jim Larson --- doc/langdef.md | 6 ++-- tests/simple/testdata/conversions.textproto | 33 +++++++++------------ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/doc/langdef.md b/doc/langdef.md index c34d690f..de8431a9 100644 --- a/doc/langdef.md +++ b/doc/langdef.md @@ -2070,7 +2070,8 @@ See [cel-go/issues/9](https://github.com/google/cel-go/issues/9). (double) -> int - type conversion + Type conversion. Rounds toward zero, then errors if result is out of + range. @@ -2295,7 +2296,8 @@ See [cel-go/issues/9](https://github.com/google/cel-go/issues/9). (double) -> uint - type conversion + Type conversion. Rounds toward zero, then errors if result is out of + range. diff --git a/tests/simple/testdata/conversions.textproto b/tests/simple/testdata/conversions.textproto index 46e4f256..73874311 100644 --- a/tests/simple/testdata/conversions.textproto +++ b/tests/simple/testdata/conversions.textproto @@ -174,24 +174,24 @@ section { value: { int64_value: -123 } } test { - name: "double_nearest" + name: "double_truncate" expr: "int(1.9)" - value: { int64_value: 2 } + value: { int64_value: 1 } } test { - name: "double_nearest_neg" + name: "double_truncate_neg" expr: "int(-7.9)" - value: { int64_value: -8 } + value: { int64_value: -7 } } test { - name: "double_half_away_pos" + name: "double_half_pos" expr: "int(11.5)" - value: { int64_value: 12 } + value: { int64_value: 11 } } test { - name: "double_half_away_neg" + name: "double_half_neg" expr: "int(-3.5)" - value: { int64_value: -4 } + value: { int64_value: -3 } } test { name: "double_big_exact" @@ -447,19 +447,14 @@ section { value: { uint64_value: 3 } } test { - name: "double_nearest_int" - expr: "int(1.9)" - value: { int64_value: 2 } - } - test { - name: "double_nearest" + name: "double_truncate" expr: "uint(1.9)" - value: { uint64_value: 2 } + value: { uint64_value: 1 } } test { - name: "double_half_away" + name: "double_half" expr: "uint(25.5)" - value: { uint64_value: 26 } + value: { uint64_value: 25 } } test { name: "double_big_exact" @@ -474,8 +469,8 @@ section { value { uint64_value: 36028797018963968 } } test { - name: "double_range_beyond_int" - description: "Checks conversion of integer outside int range." + name: "double_range_beyond_uint" + description: "Checks conversion of integer outside uint range." expr: "uint(1e19)" value: { uint64_value: 10000000000000000000 } }