Source file src/cmd/compile/internal/test/truncconst_test.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import "testing"
     8  
     9  var f52want float64 = 1.0 / (1 << 52)
    10  var f53want float64 = 1.0 / (1 << 53)
    11  
    12  func TestTruncFlt(t *testing.T) {
    13  	const f52 = 1 + 1.0/(1<<52)
    14  	const f53 = 1 + 1.0/(1<<53)
    15  
    16  	if got := f52 - 1; got != f52want {
    17  		t.Errorf("f52-1 = %g, want %g", got, f52want)
    18  	}
    19  	if got := float64(f52) - 1; got != f52want {
    20  		t.Errorf("float64(f52)-1 = %g, want %g", got, f52want)
    21  	}
    22  	if got := f53 - 1; got != f53want {
    23  		t.Errorf("f53-1 = %g, want %g", got, f53want)
    24  	}
    25  	if got := float64(f53) - 1; got != 0 {
    26  		t.Errorf("float64(f53)-1 = %g, want 0", got)
    27  	}
    28  }
    29  
    30  func TestTruncCmplx(t *testing.T) {
    31  	const r52 = complex(1+1.0/(1<<52), 0)
    32  	const r53 = complex(1+1.0/(1<<53), 0)
    33  
    34  	if got := real(r52 - 1); got != f52want {
    35  		t.Errorf("real(r52-1) = %g, want %g", got, f52want)
    36  	}
    37  	if got := real(complex128(r52) - 1); got != f52want {
    38  		t.Errorf("real(complex128(r52)-1) = %g, want %g", got, f52want)
    39  	}
    40  	if got := real(r53 - 1); got != f53want {
    41  		t.Errorf("real(r53-1) = %g, want %g", got, f53want)
    42  	}
    43  	if got := real(complex128(r53) - 1); got != 0 {
    44  		t.Errorf("real(complex128(r53)-1) = %g, want 0", got)
    45  	}
    46  
    47  	const i52 = complex(0, 1+1.0/(1<<52))
    48  	const i53 = complex(0, 1+1.0/(1<<53))
    49  
    50  	if got := imag(i52 - 1i); got != f52want {
    51  		t.Errorf("imag(i52-1i) = %g, want %g", got, f52want)
    52  	}
    53  	if got := imag(complex128(i52) - 1i); got != f52want {
    54  		t.Errorf("imag(complex128(i52)-1i) = %g, want %g", got, f52want)
    55  	}
    56  	if got := imag(i53 - 1i); got != f53want {
    57  		t.Errorf("imag(i53-1i) = %g, want %g", got, f53want)
    58  	}
    59  	if got := imag(complex128(i53) - 1i); got != 0 {
    60  		t.Errorf("imag(complex128(i53)-1i) = %g, want 0", got)
    61  	}
    62  
    63  }
    64  

View as plain text