Home | History | Annotate | Download | only in ken
      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 arithmetic on complex numbers, including multiplication and division.
      8 
      9 package main
     10 
     11 const (
     12 	R = 5
     13 	I = 6i
     14 
     15 	C1 = R + I    // ADD(5,6)
     16 	C2 = R - I    // SUB(5,-6)
     17 	C3 = -(R + I) // ADD(5,6) NEG(-5,-6)
     18 	C4 = -(R - I) // SUB(5,-6) NEG(-5,6)
     19 
     20 	C5 = C1 + R // ADD(10,6)
     21 	C6 = C1 + I // ADD(5,12)
     22 
     23 	Ca = C5 + C6 // ADD(15,18)
     24 	Cb = C5 - C6 // SUB(5,-6)
     25 
     26 	Cc = C5 * C6 // MUL(-22,-150)
     27 	Cd = C5 / C6 // DIV(0.721893,-0.532544)
     28 	Ce = Cd * C6 // MUL(10,6) sb C5
     29 )
     30 
     31 func main() {
     32 
     33 	var r complex64 = 5 + 0i
     34 	if r != R {
     35 		println("opcode 1", r, R)
     36 		panic("fail")
     37 	}
     38 
     39 	var i complex64 = 6i
     40 	if i != I {
     41 		println("opcode 2", i, I)
     42 		panic("fail")
     43 	}
     44 
     45 	c1 := r + i
     46 	if c1 != C1 {
     47 		println("opcode x", c1, C1)
     48 		panic("fail")
     49 	}
     50 
     51 	c2 := r - i
     52 	if c2 != C2 {
     53 		println("opcode x", c2, C2)
     54 		panic("fail")
     55 	}
     56 
     57 	c3 := -(r + i)
     58 	if c3 != C3 {
     59 		println("opcode x", c3, C3)
     60 		panic("fail")
     61 	}
     62 
     63 	c4 := -(r - i)
     64 	if c4 != C4 {
     65 		println("opcode x", c4, C4)
     66 		panic("fail")
     67 	}
     68 
     69 	c5 := c1 + r
     70 	if c5 != C5 {
     71 		println("opcode x", c5, C5)
     72 		panic("fail")
     73 	}
     74 
     75 	c6 := c1 + i
     76 	if c6 != C6 {
     77 		println("opcode x", c6, C6)
     78 		panic("fail")
     79 	}
     80 
     81 	ca := c5 + c6
     82 	if ca != Ca {
     83 		println("opcode x", ca, Ca)
     84 		panic("fail")
     85 	}
     86 
     87 	cb := c5 - c6
     88 	if cb != Cb {
     89 		println("opcode x", cb, Cb)
     90 		panic("fail")
     91 	}
     92 
     93 	cc := c5 * c6
     94 	if cc != Cc {
     95 		println("opcode x", cc, Cc)
     96 		panic("fail")
     97 	}
     98 
     99 	cd := c5 / c6
    100 	if cd != Cd {
    101 		println("opcode x", cd, Cd)
    102 		panic("fail")
    103 	}
    104 
    105 	ce := cd * c6
    106 	if ce != Ce {
    107 		println("opcode x", ce, Ce)
    108 		panic("fail")
    109 	}
    110 	
    111 	r32 := real(complex64(ce))
    112 	if r32 != float32(real(Ce)) {
    113 		println("real(complex64(ce))", r32, real(Ce))
    114 		panic("fail")
    115 	}
    116 	
    117 	r64 := real(complex128(ce))
    118 	if r64 != real(Ce) {
    119 		println("real(complex128(ce))", r64, real(Ce))
    120 		panic("fail")
    121 	}
    122 }
    123