1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:54:42 -07:00

go.tools/go/types, exact: fix build for 1.2

Fixes golang/go#8192.

LGTM=bradfitz
R=bradfitz, adonovan
CC=golang-codereviews
https://golang.org/cl/105160043
This commit is contained in:
Robert Griesemer 2014-06-12 12:46:21 -07:00
parent afafa2630f
commit f59b01c69b
5 changed files with 42 additions and 3 deletions

View File

@ -252,9 +252,9 @@ func Float32Val(x Value) (float32, bool) {
f := float32(x)
return f, int64Val(f) == x
case intVal:
return new(big.Rat).SetFrac(x.val, int1).Float32()
return ratToFloat32(new(big.Rat).SetFrac(x.val, int1))
case floatVal:
return x.val.Float32()
return ratToFloat32(x.val)
case unknownVal:
return 0, false
}

24
go/exact/go13.go Normal file
View File

@ -0,0 +1,24 @@
// 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.
// +build !go1.4
package exact
import (
"math"
"math/big"
)
func ratToFloat32(x *big.Rat) (float32, bool) {
// Before 1.4, there's no Rat.Float32.
// Emulate it, albeit at the cost of
// imprecision in corner cases.
x64, exact := x.Float64()
x32 := float32(x64)
if math.IsInf(float64(x32), 0) {
exact = false
}
return x32, exact
}

13
go/exact/go14.go Normal file
View File

@ -0,0 +1,13 @@
// 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.
// +build go1.4
package exact
import "math/big"
func ratToFloat32(x *big.Rat) (float32, bool) {
return x.Float32()
}

View File

@ -119,6 +119,7 @@ func TestStdTest(t *testing.T) {
testTestDir(t, filepath.Join(runtime.GOROOT(), "test"),
"cmplxdivide.go", // also needs file cmplxdivide1.go - ignore
"sigchld.go", // don't work on Windows; testTestDir should consult build tags
"float_lit2.go", // TODO(gri) enable for releases 1.4 and higher
)
}

View File

@ -17,7 +17,8 @@ func issue7035() {
func issue8066() {
const (
_ = float32(340282356779733661637539395458142568447)
// TODO(gri) Enable test below for releases 1.4 and higher
// _ = float32(340282356779733661637539395458142568447)
_ = float32(340282356779733661637539395458142568448 /* ERROR cannot convert */ )
)
}