diff --git a/encoding/textpb/decode.go b/encoding/textpb/decode.go index aef9d403..a621429d 100644 --- a/encoding/textpb/decode.go +++ b/encoding/textpb/decode.go @@ -281,11 +281,11 @@ func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, err return pref.ValueOf(uint64(n)), nil } case pref.FloatKind: - if n, ok := input.Float32(); ok { + if n, ok := input.Float(b32); ok { return pref.ValueOf(float32(n)), nil } case pref.DoubleKind: - if n, ok := input.Float64(); ok { + if n, ok := input.Float(b64); ok { return pref.ValueOf(float64(n)), nil } case pref.StringKind: diff --git a/internal/encoding/text/text_test.go b/internal/encoding/text/text_test.go index 7361e778..2ac4bf02 100644 --- a/internal/encoding/text/text_test.go +++ b/internal/encoding/text/text_test.go @@ -549,10 +549,6 @@ func Test(t *testing.T) { }, { 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{ @@ -794,16 +790,9 @@ spouse: null 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 9a0863bf..2498f66f 100644 --- a/internal/encoding/text/value.go +++ b/internal/encoding/text/value.go @@ -223,40 +223,22 @@ func (v Value) Uint(b64 bool) (x uint64, ok bool) { 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 }