| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
// This test checks if the compiler's internal constant |
| 8 |
// arithmetic correctly rounds up floating-point values |
| 9 |
// that become the smallest denormal value. |
| 10 |
// |
| 11 |
// See also related issue 14553 and test issue14553.go. |
| 12 |
|
| 13 |
package main |
| 14 |
|
| 15 |
import ( |
| 16 |
"fmt" |
| 17 |
"math" |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
p149 = 1.0 / (1 << 149) // 1p-149 |
| 22 |
p500 = 1.0 / (1 << 500) // 1p-500 |
| 23 |
p1074 = p500 * p500 / (1<<74) // 1p-1074 |
| 24 |
) |
| 25 |
|
| 26 |
const ( |
| 27 |
m0000p149 = 0x0 / 16.0 * p149 // = 0.0000p-149 |
| 28 |
m1000p149 = 0x8 / 16.0 * p149 // = 0.1000p-149 |
| 29 |
m1001p149 = 0x9 / 16.0 * p149 // = 0.1001p-149 |
| 30 |
m1011p149 = 0xb / 16.0 * p149 // = 0.1011p-149 |
| 31 |
m1100p149 = 0xc / 16.0 * p149 // = 0.1100p-149 |
| 32 |
|
| 33 |
m0000p1074 = 0x0 / 16.0 * p1074 // = 0.0000p-1074 |
| 34 |
m1000p1074 = 0x8 / 16.0 * p1074 // = 0.1000p-1074 |
| 35 |
m1001p1074 = 0x9 / 16.0 * p1074 // = 0.1001p-1074 |
| 36 |
m1011p1074 = 0xb / 16.0 * p1074 // = 0.1011p-1074 |
| 37 |
m1100p1074 = 0xc / 16.0 * p1074 // = 0.1100p-1074 |
| 38 |
) |
| 39 |
|
| 40 |
func main() { |
| 41 |
test32(float32(m0000p149), f32(m0000p149)) |
| 42 |
test32(float32(m1000p149), f32(m1000p149)) |
| 43 |
test32(float32(m1001p149), f32(m1001p149)) |
| 44 |
test32(float32(m1011p149), f32(m1011p149)) |
| 45 |
test32(float32(m1100p149), f32(m1100p149)) |
| 46 |
|
| 47 |
test64(float64(m0000p1074), f64(m0000p1074)) |
| 48 |
test64(float64(m1000p1074), f64(m1000p1074)) |
| 49 |
test64(float64(m1001p1074), f64(m1001p1074)) |
| 50 |
test64(float64(m1011p1074), f64(m1011p1074)) |
| 51 |
test64(float64(m1100p1074), f64(m1100p1074)) |
| 52 |
} |
| 53 |
|
| 54 |
func f32(x float64) float32 { return float32(x) } |
| 55 |
func f64(x float64) float64 { return float64(x) } |
| 56 |
|
| 57 |
func test32(a, b float32) { |
| 58 |
abits := math.Float32bits(a) |
| 59 |
bbits := math.Float32bits(b) |
| 60 |
if abits != bbits { |
| 61 |
panic(fmt.Sprintf("%08x != %08x\n", abits, bbits)) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func test64(a, b float64) { |
| 66 |
abits := math.Float64bits(a) |
| 67 |
bbits := math.Float64bits(b) |
| 68 |
if abits != bbits { |
| 69 |
panic(fmt.Sprintf("%016x != %016x\n", abits, bbits)) |
| 70 |
} |
| 71 |
} |