modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/ken/cplx3.go (about)

     1  // run
     2  
     3  // Copyright 2010 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  // Test composition, decomposition, and reflection on complex numbers.
     8  
     9  package main
    10  
    11  import "unsafe"
    12  import "reflect"
    13  
    14  const (
    15  	R = 5
    16  	I = 6i
    17  
    18  	C1 = R + I // ADD(5,6)
    19  )
    20  
    21  func main() {
    22  	c0 := C1
    23  	c0 = (c0 + c0 + c0) / (c0 + c0 + 3i)
    24  	r, i := real(c0), imag(c0)
    25  	d := r - 1.292308
    26  	if d < 0 {
    27  		d = - d
    28  	}
    29  	if d > 1e-6 {
    30  		println(r, "!= 1.292308")
    31  		panic(0)
    32  	}
    33  	d = i + 0.1384615
    34  	if d < 0 {
    35  		d = - d
    36  	}
    37  	if d > 1e-6 {
    38  		println(i, "!= -0.1384615")
    39  		panic(0)
    40  	}
    41  
    42  	c := *(*complex128)(unsafe.Pointer(&c0))
    43  	if c != c0 {
    44  		println(c, "!=", c)
    45  		panic(0)
    46  	}
    47  
    48  	var a interface{}
    49  	switch c := reflect.ValueOf(a); c.Kind() {
    50  	case reflect.Complex64, reflect.Complex128:
    51  		v := c.Complex()
    52  		_, _ = complex128(v), true
    53  	}
    54  }