summaryrefslogtreecommitdiff
path: root/test/float_lit3.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2014-05-19 22:57:59 -0400
committerRuss Cox <rsc@golang.org>2014-05-19 22:57:59 -0400
commit17b0ba22ea66c4217bef7e9460d7a7d73e8e0173 (patch)
tree17c982597b21f4eb756aa5d2465e284c4fc67e64 /test/float_lit3.go
parentbfa519202cfb9e307dab42155e5b5e9d2a9a15b4 (diff)
downloadgo-17b0ba22ea66c4217bef7e9460d7a7d73e8e0173.tar.gz
cmd/gc: fix float32 const conversion and printing of big float consts
The float32 const conversion used to round to float64 and then use the hardware to round to float32. Even though there was a range check before this conversion, the double rounding introduced inaccuracy: the round to float64 might round the value further away from the float32 range, reaching a float64 value that could not actually be rounded to float32. The hardware appears to give us 0 in that case, but it is probably undefined. Double rounding also meant that the wrong value might be used for certain border cases. Do the rounding the float32 ourselves, just as we already did the rounding to float64. This makes the conversion precise and also makes the conversion match the range check. Finally, add some code to print very large (bigger than float64) floating point constants in decimal floating point notation instead of falling back to the precise but human-unreadable binary floating point notation. Fixes issue 8015. LGTM=iant R=golang-codereviews, iant CC=golang-codereviews, r https://codereview.appspot.com/100580044
Diffstat (limited to 'test/float_lit3.go')
-rw-r--r--test/float_lit3.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/test/float_lit3.go b/test/float_lit3.go
new file mode 100644
index 000000000..f045c40cf
--- /dev/null
+++ b/test/float_lit3.go
@@ -0,0 +1,28 @@
+// errorcheck
+
+// Check flagging of invalid conversion of constant to float32/float64 near min/max boundaries.
+
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+var x = []interface{}{
+ float32(-340282356779733661637539395458142568448), // ERROR "constant -3\.40282e\+38 overflows float32"
+ float32(-340282356779733661637539395458142568447),
+ float32(-340282326356119256160033759537265639424),
+ float32(340282326356119256160033759537265639424),
+ float32(340282356779733661637539395458142568447),
+ float32(340282356779733661637539395458142568448), // ERROR "constant 3\.40282e\+38 overflows float32"
+ -1e1000, // ERROR "constant -1\.00000e\+1000 overflows float64"
+ float64(-1.797693134862315907937289714053e+308), // ERROR "constant -1\.79769e\+308 overflows float64"
+ float64(-1.797693134862315807937289714053e+308),
+ float64(-1.797693134862315708145274237317e+308),
+ float64(-1.797693134862315608353258760581e+308),
+ float64(1.797693134862315608353258760581e+308),
+ float64(1.797693134862315708145274237317e+308),
+ float64(1.797693134862315807937289714053e+308),
+ float64(1.797693134862315907937289714053e+308), // ERROR "constant 1\.79769e\+308 overflows float64"
+ 1e1000, // ERROR "constant 1\.00000e\+1000 overflows float64"
+}