1
0
mirror of https://github.com/golang/go synced 2024-11-11 22:50:22 -07:00

test/float_lit2.go: compute test values from first principles

These constants pass go/types constant conversions as well.

LGTM=r
R=r
CC=golang-codereviews
https://golang.org/cl/91590047
This commit is contained in:
Robert Griesemer 2014-05-21 08:53:47 -07:00
parent 7d4cb4d63f
commit 765b4a3f86

View File

@ -12,20 +12,75 @@ import (
"fmt"
)
const (
m32bits = 23 // number of float32 mantissa bits
e32max = 127 // max. float32 exponent
maxExp32 = e32max - m32bits
maxMant32 = 1<<(m32bits+1) - 1
maxFloat32_0 = (maxMant32 - 0) << maxExp32
maxFloat32_1 = (maxMant32 - 1) << maxExp32
maxFloat32_2 = (maxMant32 - 2) << maxExp32
)
func init() {
if maxExp32 != 104 {
panic("incorrect maxExp32")
}
if maxMant32 != 16777215 {
panic("incorrect maxMant32")
}
if maxFloat32_0 != 340282346638528859811704183484516925440 {
panic("incorrect maxFloat32_0")
}
}
const (
m64bits = 52 // number of float64 mantissa bits
e64max = 1023 // max. float64 exponent
maxExp64 = e64max - m64bits
maxMant64 = 1<<(m64bits+1) - 1
// These expressions are not permitted due to implementation restrictions.
// maxFloat64_0 = (maxMant64-0) << maxExp64
// maxFloat64_1 = (maxMant64-1) << maxExp64
// maxFloat64_2 = (maxMant64-2) << maxExp64
// These equivalent values were computed using math/big.
maxFloat64_0 = 1.7976931348623157e308
maxFloat64_1 = 1.7976931348623155e308
maxFloat64_2 = 1.7976931348623153e308
)
func init() {
if maxExp64 != 971 {
panic("incorrect maxExp64")
}
if maxMant64 != 9007199254740991 {
panic("incorrect maxMant64")
}
}
var cvt = []struct {
val interface{}
binary string
}{
{float32(-340282356779733661637539395458142568447), "-16777215p+104"},
{float32(-340282326356119256160033759537265639424), "-16777214p+104"},
{float32(340282326356119256160033759537265639424), "16777214p+104"},
{float32(340282356779733661637539395458142568447), "16777215p+104"},
{float64(-1.797693134862315807937289714053e+308), "-9007199254740991p+971"},
{float64(-1.797693134862315708145274237317e+308), "-9007199254740991p+971"},
{float64(-1.797693134862315608353258760581e+308), "-9007199254740990p+971"},
{float64(1.797693134862315608353258760581e+308), "9007199254740990p+971"},
{float64(1.797693134862315708145274237317e+308), "9007199254740991p+971"},
{float64(1.797693134862315807937289714053e+308), "9007199254740991p+971"},
{float32(maxFloat32_0), fmt.Sprintf("%dp+%d", maxMant32-0, maxExp32)},
{float32(maxFloat32_1), fmt.Sprintf("%dp+%d", maxMant32-1, maxExp32)},
{float32(maxFloat32_2), fmt.Sprintf("%dp+%d", maxMant32-2, maxExp32)},
{float64(maxFloat64_0), fmt.Sprintf("%dp+%d", maxMant64-0, maxExp64)},
{float64(maxFloat64_1), fmt.Sprintf("%dp+%d", maxMant64-1, maxExp64)},
{float64(maxFloat64_2), fmt.Sprintf("%dp+%d", maxMant64-2, maxExp64)},
{float32(-maxFloat32_0), fmt.Sprintf("-%dp+%d", maxMant32-0, maxExp32)},
{float32(-maxFloat32_1), fmt.Sprintf("-%dp+%d", maxMant32-1, maxExp32)},
{float32(-maxFloat32_2), fmt.Sprintf("-%dp+%d", maxMant32-2, maxExp32)},
{float64(-maxFloat64_0), fmt.Sprintf("-%dp+%d", maxMant64-0, maxExp64)},
{float64(-maxFloat64_1), fmt.Sprintf("-%dp+%d", maxMant64-1, maxExp64)},
{float64(-maxFloat64_2), fmt.Sprintf("-%dp+%d", maxMant64-2, maxExp64)},
}
func main() {