| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2018 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 |
// (-0)+0 should be 0, not -0. |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
//go:noinline |
| 12 |
func add64(x float64) float64 { |
| 13 |
return x + 0 |
| 14 |
} |
| 15 |
|
| 16 |
func testAdd64() { |
| 17 |
var zero float64 |
| 18 |
inf := 1.0 / zero |
| 19 |
negZero := -1 / inf |
| 20 |
if 1/add64(negZero) != inf { |
| 21 |
panic("negZero+0 != posZero (64 bit)") |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
//go:noinline |
| 26 |
func sub64(x float64) float64 { |
| 27 |
return x - 0 |
| 28 |
} |
| 29 |
|
| 30 |
func testSub64() { |
| 31 |
var zero float64 |
| 32 |
inf := 1.0 / zero |
| 33 |
negZero := -1 / inf |
| 34 |
if 1/sub64(negZero) != -inf { |
| 35 |
panic("negZero-0 != negZero (64 bit)") |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
//go:noinline |
| 40 |
func neg64(x float64) float64 { |
| 41 |
return -x |
| 42 |
} |
| 43 |
|
| 44 |
func testNeg64() { |
| 45 |
var zero float64 |
| 46 |
inf := 1.0 / zero |
| 47 |
negZero := -1 / inf |
| 48 |
if 1/neg64(negZero) != inf { |
| 49 |
panic("-negZero != posZero (64 bit)") |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
//go:noinline |
| 54 |
func add32(x float32) float32 { |
| 55 |
return x + 0 |
| 56 |
} |
| 57 |
|
| 58 |
func testAdd32() { |
| 59 |
var zero float32 |
| 60 |
inf := 1.0 / zero |
| 61 |
negZero := -1 / inf |
| 62 |
if 1/add32(negZero) != inf { |
| 63 |
panic("negZero+0 != posZero (32 bit)") |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
//go:noinline |
| 68 |
func sub32(x float32) float32 { |
| 69 |
return x - 0 |
| 70 |
} |
| 71 |
|
| 72 |
func testSub32() { |
| 73 |
var zero float32 |
| 74 |
inf := 1.0 / zero |
| 75 |
negZero := -1 / inf |
| 76 |
if 1/sub32(negZero) != -inf { |
| 77 |
panic("negZero-0 != negZero (32 bit)") |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
//go:noinline |
| 82 |
func neg32(x float32) float32 { |
| 83 |
return -x |
| 84 |
} |
| 85 |
|
| 86 |
func testNeg32() { |
| 87 |
var zero float32 |
| 88 |
inf := 1.0 / zero |
| 89 |
negZero := -1 / inf |
| 90 |
if 1/neg32(negZero) != inf { |
| 91 |
panic("-negZero != posZero (32 bit)") |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func main() { |
| 96 |
testAdd64() |
| 97 |
testSub64() |
| 98 |
testNeg64() |
| 99 |
testAdd32() |
| 100 |
testSub32() |
| 101 |
testNeg32() |
| 102 |
} |