mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-03-28 08:37:29 +00:00
internal/impl: fix Has behavior for floats
According to IEEE-754, equality on floats reports true for both +0 and -0. However, this definition causes us to lose information. Instead, we want a definition that checks for exact equality with +0. Otherwise, we would lose the ability to serialize -0. Change-Id: I36450c24258fc4f856bfd4bc4c53a90199b216b9 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172970 Reviewed-by: Herbie Ong <herbie@google.com>
This commit is contained in:
parent
c90b6ada23
commit
060cdacd3b
@ -6,6 +6,7 @@ package impl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/golang/protobuf/v2/internal/flags"
|
"github.com/golang/protobuf/v2/internal/flags"
|
||||||
@ -197,7 +198,7 @@ func fieldInfoForScalar(fd pref.FieldDescriptor, fs reflect.StructField) fieldIn
|
|||||||
case reflect.Uint32, reflect.Uint64:
|
case reflect.Uint32, reflect.Uint64:
|
||||||
return rv.Uint() != 0
|
return rv.Uint() != 0
|
||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
return rv.Float() != 0
|
return rv.Float() != 0 || math.Signbit(rv.Float())
|
||||||
case reflect.String, reflect.Slice:
|
case reflect.String, reflect.Slice:
|
||||||
return rv.Len() > 0
|
return rv.Len() > 0
|
||||||
default:
|
default:
|
||||||
|
@ -387,6 +387,11 @@ func TestScalarProto3(t *testing.T) {
|
|||||||
},
|
},
|
||||||
clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
|
clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
|
||||||
equalMessage{&ScalarProto3{}},
|
equalMessage{&ScalarProto3{}},
|
||||||
|
|
||||||
|
// Verify that -0 triggers proper Has behavior.
|
||||||
|
hasFields{6: false, 7: false},
|
||||||
|
setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
|
||||||
|
hasFields{6: true, 7: true},
|
||||||
})
|
})
|
||||||
|
|
||||||
// Test read-only operations on nil message.
|
// Test read-only operations on nil message.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user