1 def assert_complex(real, exp)
2 assert "assert_complex" do
3 assert_float real.real, exp.real
4 assert_float real.imaginary, exp.imaginary
10 assert_equal Complex, c.class
11 assert_equal [c.real, c.imaginary], [0, 123]
13 assert_equal Complex, c.class
14 assert_equal [c.real, c.imaginary], [123, -1.23]
17 assert 'Complex::polar' do
18 assert_complex Complex.polar(3, 0), (3 + 0i)
19 assert_complex Complex.polar(3, Math::PI/2), (0 + 3i)
20 assert_complex Complex.polar(3, Math::PI), (-3 + 0i)
21 assert_complex Complex.polar(3, -Math::PI/2), (0 + -3i)
24 assert 'Complex::rectangular' do
25 assert_complex Complex.rectangular(1, 2), (1 + 2i)
29 assert_complex Complex(2, 3) * Complex(2, 3), (-5 + 12i)
30 assert_complex Complex(900) * Complex(1), (900 + 0i)
31 assert_complex Complex(-2, 9) * Complex(-9, 2), (0 - 85i)
32 assert_complex Complex(9, 8) * 4, (36 + 32i)
33 assert_complex Complex(20, 9) * 9.8, (196.0 + 88.2i)
37 assert_complex Complex(2, 3) + Complex(2, 3) , (4 + 6i)
38 assert_complex Complex(900) + Complex(1) , (901 + 0i)
39 assert_complex Complex(-2, 9) + Complex(-9, 2), (-11 + 11i)
40 assert_complex Complex(9, 8) + 4 , (13 + 8i)
41 assert_complex Complex(20, 9) + 9.8 , (29.8 + 9i)
45 assert_complex Complex(2, 3) - Complex(2, 3) , (0 + 0i)
46 assert_complex Complex(900) - Complex(1) , (899 + 0i)
47 assert_complex Complex(-2, 9) - Complex(-9, 2), (7 + 7i)
48 assert_complex Complex(9, 8) - 4 , (5 + 8i)
49 assert_complex Complex(20, 9) - 9.8 , (10.2 + 9i)
52 assert 'Complex#-@' do
53 assert_complex(-Complex(1, 2), (-1 - 2i))
57 assert_complex Complex(2, 3) / Complex(2, 3) , (1 + 0i)
58 assert_complex Complex(900) / Complex(1) , (900 + 0i)
59 assert_complex Complex(-2, 9) / Complex(-9, 2), ((36 / 85) - (77i / 85))
60 assert_complex Complex(9, 8) / 4 , ((9 / 4) + 2i)
61 assert_complex Complex(20, 9) / 9.8 , (2.0408163265306123 + 0.9183673469387754i)
62 if 1e39.infinite? then
63 # MRB_USE_FLOAT in effect
70 assert_complex Complex(ten, ten) / Complex(one, one), Complex(10.0, 0.0)
73 assert 'Complex#==' do
74 assert_true Complex(2, 3) == Complex(2, 3)
75 assert_true Complex(5) == 5
76 assert_true Complex(0) == 0.0
79 assert 'Complex#abs' do
80 assert_float Complex(-1).abs, 1
81 assert_float Complex(3.0, -4.0).abs, 5.0
82 if 1e39.infinite? then
83 # MRB_USE_FLOAT in effect
88 assert_true Complex(3.0*2.0**exp, 4.0*2.0**exp).abs.finite?
89 assert_float Complex(3.0*2.0**exp, 4.0*2.0**exp).abs, 5.0*2.0**exp
92 assert 'Complex#abs2' do
93 assert_float Complex(-1).abs2, 1
94 assert_float Complex(3.0, -4.0).abs2, 25.0
97 assert 'Complex#arg' do
98 assert_float Complex.polar(3, Math::PI/2).arg, 1.5707963267948966
101 assert 'Complex#conjugate' do
102 assert_complex Complex(1, 2).conjugate, (1 - 2i)
105 assert 'Complex#fdiv' do
106 assert_complex Complex(11, 22).fdiv(3), (3.6666666666666665 + 7.333333333333333i)
109 assert 'Complex#imaginary' do
110 assert_float Complex(7).imaginary , 0
111 assert_float Complex(9, -4).imaginary, -4
114 assert 'Complex#polar' do
115 assert_equal Complex(1, 2).polar, [2.23606797749979, 1.1071487177940904]
118 assert 'Complex#real' do
119 assert_float Complex(7).real, 7
120 assert_float Complex(9, -4).real, 9
123 assert 'Complex#real?' do
124 assert_false Complex(1).real?
127 assert 'Complex::rectangular' do
128 assert_equal Complex(1, 2).rectangular, [1, 2]
131 assert 'Complex::to_c' do
132 assert_equal Complex(1, 2).to_c, Complex(1, 2)
135 assert 'Complex::to_f' do
136 assert_float Complex(1, 0).to_f, 1.0
137 assert_raise(RangeError) do
142 assert 'Complex::to_i' do
143 assert_equal Complex(1, 0).to_i, 1
144 assert_raise(RangeError) do
149 assert 'Complex#frozen?' do
150 assert_predicate(1i, :frozen?)
151 assert_predicate(Complex(2,3), :frozen?)
152 assert_predicate(4+5i, :frozen?)