internal/encoding/text: change Value.Float{32,64} to Value.Float
Collapse Value.Float32 and Value.Float64 into single API to keep it
consistent with Value.{Int,Uint}.
Change-Id: I07737e72715fe3cc3f6bcad579cf5d6cfe3757d5
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/167317
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/encoding/text/text_test.go b/internal/encoding/text/text_test.go
index 7361e77..2ac4bf0 100644
--- a/internal/encoding/text/text_test.go
+++ b/internal/encoding/text/text_test.go
@@ -550,10 +550,6 @@
in: `crazy:"x'"'\""\''"'z"`,
wantVal: V(Msg{{ID("crazy"), V(`x'""''z`)}}),
}, {
- in: `num: 1.02`,
- wantVal: V(Msg{{ID("num"), V(float32(1.02))}}), // Use float32 to test marshaling of Float32 type.
- wantOut: `num:1.02`,
- }, {
in: `nums: [t,T,true,True,TRUE,f,F,false,False,FALSE]`,
wantVal: V(Msg{{ID("nums"), V(Lst{
V(true),
@@ -794,16 +790,9 @@
want, _ := x.Uint(true)
got, ok := y.Uint(math.MaxUint32 < want)
return got == want && ok
- case Float32:
- want, _ := x.Float32()
- got, ok := y.Float32()
- if math.IsNaN(float64(got)) || math.IsNaN(float64(want)) {
- return math.IsNaN(float64(got)) == math.IsNaN(float64(want))
- }
- return got == want && ok
- case Float64:
- want, _ := x.Float64()
- got, ok := y.Float64()
+ case Float32, Float64:
+ want, _ := x.Float(true)
+ got, ok := y.Float(math.MaxFloat32 < math.Abs(want))
if math.IsNaN(got) || math.IsNaN(want) {
return math.IsNaN(got) == math.IsNaN(want)
}
diff --git a/internal/encoding/text/value.go b/internal/encoding/text/value.go
index 9a0863b..2498f66 100644
--- a/internal/encoding/text/value.go
+++ b/internal/encoding/text/value.go
@@ -223,40 +223,22 @@
return 0, false
}
-// Float32 returns v as a float32 of the specified precision and reports whether
+// Float returns v as a float64 of the specified precision and reports whether
// the conversion succeeded.
-func (v Value) Float32() (x float32, ok bool) {
- switch v.typ {
- case Int:
- return float32(int64(v.num)), true // possibly lossy, but allowed
- case Uint:
- return float32(uint64(v.num)), true // possibly lossy, but allowed
- case Float32, Float64:
- n := math.Float64frombits(v.num)
- if math.IsNaN(n) || math.IsInf(n, 0) {
- return float32(n), true
- }
- if math.Abs(n) <= math.MaxFloat32 {
- return float32(n), true
- }
- }
- return 0, false
-}
-
-// Float64 returns v as a float64 of the specified precision and reports whether
-// the conversion succeeded.
-func (v Value) Float64() (x float64, ok bool) {
+func (v Value) Float(b64 bool) (x float64, ok bool) {
switch v.typ {
case Int:
return float64(int64(v.num)), true // possibly lossy, but allowed
case Uint:
return float64(uint64(v.num)), true // possibly lossy, but allowed
- case Float32:
- f, ok := v.Float32()
- return float64(f), ok
- case Float64:
+ case Float32, Float64:
n := math.Float64frombits(v.num)
- return n, true
+ if math.IsNaN(n) || math.IsInf(n, 0) {
+ return float64(n), true
+ }
+ if b64 || math.Abs(n) <= math.MaxFloat32 {
+ return float64(n), true
+ }
}
return 0, false
}