mirror of
https://github.com/golang/go
synced 2024-11-18 20:04:52 -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:
parent
afafa2630f
commit
f59b01c69b
@ -252,9 +252,9 @@ func Float32Val(x Value) (float32, bool) {
|
|||||||
f := float32(x)
|
f := float32(x)
|
||||||
return f, int64Val(f) == x
|
return f, int64Val(f) == x
|
||||||
case intVal:
|
case intVal:
|
||||||
return new(big.Rat).SetFrac(x.val, int1).Float32()
|
return ratToFloat32(new(big.Rat).SetFrac(x.val, int1))
|
||||||
case floatVal:
|
case floatVal:
|
||||||
return x.val.Float32()
|
return ratToFloat32(x.val)
|
||||||
case unknownVal:
|
case unknownVal:
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
24
go/exact/go13.go
Normal file
24
go/exact/go13.go
Normal 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
13
go/exact/go14.go
Normal 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()
|
||||||
|
}
|
@ -119,6 +119,7 @@ func TestStdTest(t *testing.T) {
|
|||||||
testTestDir(t, filepath.Join(runtime.GOROOT(), "test"),
|
testTestDir(t, filepath.Join(runtime.GOROOT(), "test"),
|
||||||
"cmplxdivide.go", // also needs file cmplxdivide1.go - ignore
|
"cmplxdivide.go", // also needs file cmplxdivide1.go - ignore
|
||||||
"sigchld.go", // don't work on Windows; testTestDir should consult build tags
|
"sigchld.go", // don't work on Windows; testTestDir should consult build tags
|
||||||
|
"float_lit2.go", // TODO(gri) enable for releases 1.4 and higher
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
go/types/testdata/issues.src
vendored
3
go/types/testdata/issues.src
vendored
@ -17,7 +17,8 @@ func issue7035() {
|
|||||||
|
|
||||||
func issue8066() {
|
func issue8066() {
|
||||||
const (
|
const (
|
||||||
_ = float32(340282356779733661637539395458142568447)
|
// TODO(gri) Enable test below for releases 1.4 and higher
|
||||||
|
// _ = float32(340282356779733661637539395458142568447)
|
||||||
_ = float32(340282356779733661637539395458142568448 /* ERROR cannot convert */ )
|
_ = float32(340282356779733661637539395458142568448 /* ERROR cannot convert */ )
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user