Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions common/types/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,54 @@ import (
"github.com/google/cel-go/common/types/ref"
)

// compareDoubleInt orders a double against an int by their mathematical values.
//
// The int is split from the double rather than converted to one: ints above 2^53 have no exact
// double representation, so converting rounds them to a neighbouring value. Callers screen NaN
// before dispatching here.
func compareDoubleInt(d Double, i Int) Int {
if d < math.MinInt64 {
f := float64(d)
if f < math.MinInt64 {
return IntNegOne
}
// MaxInt64 rounds up to 2^63 as a double, which is the first value above the int64 range.
if f >= math.MaxInt64 {
return IntOne
}
// The integral part is now within the int64 range, so it converts exactly.
whole := math.Trunc(f)
if Int(whole) < i {
return IntNegOne
}
if d > math.MaxInt64 {
if Int(whole) > i {
return IntOne
}
return compareDouble(d, Double(i))
// Equal whole parts, so the remaining fraction decides the order.
return compareDouble(Double(f-whole), 0)
}

func compareIntDouble(i Int, d Double) Int {
return -compareDoubleInt(d, i)
}

// compareDoubleUint orders a double against a uint by their mathematical values, splitting the
// double instead of converting the uint for the reason given on compareDoubleInt.
func compareDoubleUint(d Double, u Uint) Int {
if d < 0 {
f := float64(d)
if f < 0 {
return IntNegOne
}
if f >= doubleTwoTo64 {
return IntOne
}
whole := math.Trunc(f)
if Uint(whole) < u {
return IntNegOne
}
if d > math.MaxUint64 {
if Uint(whole) > u {
return IntOne
}
return compareDouble(d, Double(u))
return compareDouble(Double(f-whole), 0)
}

func compareUintDouble(u Uint, d Double) Int {
Expand Down
37 changes: 37 additions & 0 deletions common/types/double_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ func TestDoubleCompare(t *testing.T) {
b: Int(204),
out: IntOne,
},
{
// 2^53+1 has no double representation, so it must not compare equal to 2^53.
a: Double(1 << 53),
b: Int(1<<53) + 1,
out: IntNegOne,
},
{
a: Double(1 << 53),
b: Uint(1<<53) + 1,
out: IntNegOne,
},
{
// MaxInt64 rounds up to 2^63 as a double, but remains below it as an int.
a: Double(1 << 63),
b: Int(math.MaxInt64),
out: IntOne,
},
{
a: Double(1),
b: String("1"),
Expand Down Expand Up @@ -407,6 +424,26 @@ func TestDoubleEqual(t *testing.T) {
b: Int(10),
out: False,
},
{
a: Double(1 << 53),
b: Int(1<<53) + 1,
out: False,
},
{
a: Double(1 << 53),
b: Uint(1<<53) + 1,
out: False,
},
{
a: Double(1 << 63),
b: Int(math.MaxInt64),
out: False,
},
{
a: Double(math.MaxUint64),
b: Uint(math.MaxUint64),
out: False,
},
}
for _, tc := range tests {
got := tc.a.Equal(tc.b)
Expand Down
21 changes: 21 additions & 0 deletions common/types/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ func TestIntCompare(t *testing.T) {
b: Double(math.MinInt64) - 1025.0,
out: IntOne,
},
{
// 2^53+1 has no double representation, so it must not compare equal to 2^53.
a: Int(1<<53) + 1,
b: Double(1 << 53),
out: IntOne,
},
{
a: Int(math.MaxInt64),
b: Double(1 << 63),
out: IntNegOne,
},
{
a: Int(1),
b: String("1"),
Expand Down Expand Up @@ -429,6 +440,16 @@ func TestIntEqual(t *testing.T) {
b: Double(math.NaN()),
out: False,
},
{
a: Int(1<<53) + 1,
b: Double(1 << 53),
out: False,
},
{
a: Int(math.MaxInt64),
b: Double(1 << 63),
out: False,
},
{
a: Int(1),
b: String("1"),
Expand Down
21 changes: 21 additions & 0 deletions common/types/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ func TestUintCompare(t *testing.T) {
b: Double(-1.0),
out: IntOne,
},
{
// 2^53+1 has no double representation, so it must not compare equal to 2^53.
a: Uint(1<<53) + 1,
b: Double(1 << 53),
out: IntOne,
},
{
a: Uint(math.MaxUint64),
b: Double(math.MaxUint64),
out: IntNegOne,
},
{
a: Uint(1),
b: String("1"),
Expand Down Expand Up @@ -379,6 +390,16 @@ func TestUintEqual(t *testing.T) {
b: Double(math.NaN()),
out: False,
},
{
a: Uint(1<<53) + 1,
b: Double(1 << 53),
out: False,
},
{
a: Uint(math.MaxUint64),
b: Double(math.MaxUint64),
out: False,
},
}
for _, tc := range tests {
got := tc.a.Equal(tc.b)
Expand Down