From 67c866ec7414a915abaadddb8c1d21c698113e48 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 10 Sep 2013 14:43:23 -0700 Subject: [PATCH] go.tools/go/types: extra tests for float conversions R=adonovan CC=golang-dev https://golang.org/cl/13393046 --- go/types/expr.go | 5 +++-- go/types/testdata/const1.src | 12 +++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/go/types/expr.go b/go/types/expr.go index c4478585fe1..e71b2d968ae 100644 --- a/go/types/expr.go +++ b/go/types/expr.go @@ -163,8 +163,9 @@ func fitsFloat32(x exact.Value) bool { // the conversion succeeds but the result value is implementation- // dependent." // - // We assume that float32(f) returns an Inf if f cannot be represented - // as a float32, or if f is an Inf. + // We assume that float32(f) returns an Inf if f is too large for + // a float32, or if f is an Inf; and that it returns 0 for values + // with too small a magnitude. return !math.IsInf(float64(float32(f)), 0) } diff --git a/go/types/testdata/const1.src b/go/types/testdata/const1.src index a600fdc6d49..df4d52293bc 100644 --- a/go/types/testdata/const1.src +++ b/go/types/testdata/const1.src @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// implicit constant conversions +// constant conversions package const1 @@ -248,6 +248,11 @@ const ( _ = float32(-maxFloat32) _ = float32(maxFloat32) _ = float32(maxFloat32 /* ERROR "cannot convert" */ + delta32) + + _ = assert(float32(smallestFloat32) == smallestFloat32) + _ = assert(float32(smallestFloat32/2) == 0) + _ = assert(float32(smallestFloat64) == 0) + _ = assert(float32(smallestFloat64/2) == 0) ) const delta64 = maxFloat64/(1 << 52) @@ -262,6 +267,11 @@ const ( _ = float64(-maxFloat64) _ = float64(maxFloat64) _ = float64(maxFloat64 /* ERROR "cannot convert" */ + delta64) + + _ = assert(float64(smallestFloat32) == smallestFloat32) + _ = assert(float64(smallestFloat32/2) == smallestFloat32/2) + _ = assert(float64(smallestFloat64) == smallestFloat64) + _ = assert(float64(smallestFloat64/2) == 0) ) const (