github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/test/fixedbugs/issue27718.go (about)

     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 add32(x float32) float32 {
    41  	return x + 0
    42  }
    43  
    44  func testAdd32() {
    45  	var zero float32
    46  	inf := 1.0 / zero
    47  	negZero := -1 / inf
    48  	if 1/add32(negZero) != inf {
    49  		panic("negZero+0 != posZero (32 bit)")
    50  	}
    51  }
    52  
    53  //go:noinline
    54  func sub32(x float32) float32 {
    55  	return x - 0
    56  }
    57  
    58  func testSub32() {
    59  	var zero float32
    60  	inf := 1.0 / zero
    61  	negZero := -1 / inf
    62  	if 1/sub32(negZero) != -inf {
    63  		panic("negZero-0 != negZero (32 bit)")
    64  	}
    65  }
    66  
    67  func main() {
    68  	testAdd64()
    69  	testSub64()
    70  	testAdd32()
    71  	testSub32()
    72  }