148 |
148 |
149 testCreation |
149 testCreation |
150 | c | |
150 | c | |
151 |
151 |
152 false ifTrue:[ |
152 false ifTrue:[ |
153 "cg: I dont think this has to be known outside complex" |
153 "cg: I dont think this has to be known outside complex" |
154 "i.e. should it really be allowed to create complex numbers with new ?" |
154 "i.e. should it really be allowed to create complex numbers with new ?" |
155 self should: [ (c := Complex new) realPart = 0 ]. |
155 self should: [ (c := Complex new) realPart = 0 ]. |
156 self should: [ c imaginaryPart = 0 ]. |
156 self should: [ c imaginaryPart = 0 ]. |
157 ]. |
157 ]. |
158 self should: [ (c := Complex real: 1 imaginary: 2) realPart = 1 ]. |
158 self should: [ (c := Complex real: 1 imaginary: 2) realPart = 1 ]. |
159 self should: [ c imaginaryPart = 2 ]. |
159 self should: [ c imaginaryPart = 2 ]. |
160 |
160 |
161 self should: [ (c := Complex real: -1 imaginary: 2) realPart = -1 ]. |
161 self should: [ (c := Complex real: -1 imaginary: 2) realPart = -1 ]. |
165 self should: [ c imaginaryPart = -2 ]. |
165 self should: [ c imaginaryPart = -2 ]. |
166 |
166 |
167 self should: [ (c := Complex real: -1 imaginary: -2) realPart = -1 ]. |
167 self should: [ (c := Complex real: -1 imaginary: -2) realPart = -1 ]. |
168 self should: [ c imaginaryPart = -2 ]. |
168 self should: [ c imaginaryPart = -2 ]. |
169 |
169 |
170 c := (1 % 2). |
170 c := (1 % 2). |
171 self assert: (c real = 1). |
171 self assert: (c real = 1). |
172 self assert: (c imaginary = 2). |
172 self assert: (c imaginary = 2). |
173 |
173 |
174 c := (-1 % 2). |
174 c := (-1 % 2). |
175 self assert: (c real = -1). |
175 self assert: (c real = -1). |
176 self assert: (c imaginary = 2). |
176 self assert: (c imaginary = 2). |
177 |
177 |
178 c := (1 % -2). |
178 c := (1 % -2). |
179 self assert: (c real = 1). |
179 self assert: (c real = 1). |
180 self assert: (c imaginary = -2). |
180 self assert: (c imaginary = -2). |
181 |
181 |
182 c := (-1 % -2). |
182 c := (-1 % -2). |
183 self assert: (c real = -1). |
183 self assert: (c real = -1). |
184 self assert: (c imaginary = -2). |
184 self assert: (c imaginary = -2). |
185 |
185 |
186 c := (1.0 % 2.0). |
186 c := (1.0 % 2.0). |
187 self assert: (c real = 1.0). |
187 self assert: (c real = 1.0). |
188 self assert: (c imaginary = 2.0). |
188 self assert: (c imaginary = 2.0). |
189 |
189 |
190 c := (-1.0 % 2.0). |
190 c := (-1.0 % 2.0). |
191 self assert: (c real = -1.0). |
191 self assert: (c real = -1.0). |
192 self assert: (c imaginary = 2.0). |
192 self assert: (c imaginary = 2.0). |
193 |
193 |
194 c := (1.0 % -2.0). |
194 c := (1.0 % -2.0). |
195 self assert: (c real = 1.0). |
195 self assert: (c real = 1.0). |
196 self assert: (c imaginary = -2.0). |
196 self assert: (c imaginary = -2.0). |
197 |
197 |
198 c := (-1.0 % -2.0). |
198 c := (-1.0 % -2.0). |
199 self assert: (c real = -1.0). |
199 self assert: (c real = -1.0). |
200 self assert: (c imaginary = -2.0). |
200 self assert: (c imaginary = -2.0). |
201 |
201 |
202 c := 5 i. |
202 c := 5 i. |
203 self assert: (c real = 0). |
203 self assert: (c real = 0). |
204 self assert: (c imaginary = 5). |
204 self assert: (c imaginary = 5). |
205 |
205 |
206 c := 6 + 7 i. |
206 c := 6 + 7 i. |
207 self assert: (c real = 6). |
207 self assert: (c real = 6). |
208 self assert: ( c imaginary = 7). |
208 self assert: ( c imaginary = 7). |
209 |
209 |
210 c := 5.6 - 8 i. |
210 c := 5.6 - 8 i. |
211 self assert: (c real = 5.6). |
211 self assert: (c real = 5.6). |
212 self assert: (c imaginary = -8). |
212 self assert: (c imaginary = -8). |
213 |
213 |
214 c := Complex real: 10 imaginary: 5. |
214 c := Complex real: 10 imaginary: 5. |
215 self assert: (c real = 10). |
215 self assert: (c real = 10). |
216 self assert: (c imaginary = 5). |
216 self assert: (c imaginary = 5). |
217 |
217 |
218 c := Complex abs: 5 arg: (Float pi/2). |
218 c := Complex abs: 5 arg: (Float pi/2). |
219 self assert: (c real rounded = 0). |
219 self assert: (c real rounded = 0). |
220 self assert: (c imaginary = 5). |
220 self assert: (c imaginary = 5). |
221 |
221 |
222 " |
222 " |
322 self new testNegated |
322 self new testNegated |
323 " |
323 " |
324 ! |
324 ! |
325 |
325 |
326 testRaisedTo |
326 testRaisedTo |
327 | c | |
327 | c | |
328 |
328 |
329 self shouldnt: [ c := Complex real: 3 imaginary: 2 ] raise: Exception. |
329 self shouldnt: [ c := Complex real: 3 imaginary: 2 ] raise: Exception. |
330 self assert: ((c raisedTo: 2) = (c * c)). |
330 self assert: ((c raisedTo: 2) = (c * c)). |
331 self assert: ((c raisedTo: 3) = (c * c * c)). |
331 self assert: ((c raisedTo: 3) = (c * c * c)). |
332 self assert: ((c raisedTo: 4) = (c * c * c * c)). |
332 self assert: ((c raisedTo: 4) = (c * c * c * c)). |
333 ! |
333 ! |
334 |
334 |
335 testReciprocal |
335 testReciprocal |
336 | c | |
336 | c | |
337 c := (2 + 5 i). |
337 c := (2 + 5 i). |
338 self assert: c reciprocal = ((2/29) - (5/29)i). |
338 self assert: c reciprocal = ((2/29) - (5/29)i). |
339 |
339 |
340 " |
340 " |
341 self run: #testReciprocal |
341 self run: #testReciprocal |
342 self new testReciprocal |
342 self new testReciprocal |
343 " |
343 " |
344 ! |
344 ! |
345 |
345 |
346 testReciprocalError |
346 testReciprocalError |
347 | c | |
347 | c | |
348 c := (0 i). |
348 c := (0 i). |
349 self should: [c reciprocal] raise: ZeroDivide |
349 self should: [c reciprocal] raise: ZeroDivide |
350 " |
350 " |
351 self run: #testReciprocalError |
351 self run: #testReciprocalError |
352 self new testReciprocalError |
352 self new testReciprocalError |
353 " |
353 " |
354 ! |
354 ! |
355 |
355 |
356 testSqrt |
356 testSqrt |
357 | c w | |
357 | c w | |
358 |
358 |
359 self shouldnt: [ c := Complex real: 0 imaginary: 0 ] raise: Exception. |
359 self shouldnt: [ c := Complex real: 0 imaginary: 0 ] raise: Exception. |
360 self should: [ c sqrt = 0 ]. |
360 self should: [ c sqrt = 0 ]. |
361 |
361 |
362 self shouldnt: [ c := Complex real: 9 imaginary: 4 ] raise: Exception. |
362 self shouldnt: [ c := Complex real: 9 imaginary: 4 ] raise: Exception. |
363 "cg: that cannot be tested easily, due to rounding errors" |
363 "cg: that cannot be tested easily, due to rounding errors" |
364 "original" false ifTrue:[ |
364 "original" false ifTrue:[ |
365 self should: [ w := 3 * (((1 + (1 + ((4/9) * (4/9))) sqrt) / 2) sqrt). |
365 self should: [ w := 3 * (((1 + (1 + ((4/9) * (4/9))) sqrt) / 2) sqrt). |
366 c sqrt = (Complex real: w imaginary: 4 / (2 * w)) ]. |
366 c sqrt = (Complex real: w imaginary: 4 / (2 * w)) ]. |
367 ] ifFalse:[ |
367 ] ifFalse:[ |
368 self should: [ |t1 t2 epsilon| |
368 self should: [ |t1 t2 epsilon| |
369 w := 3 * (((1 + (1 + ((4/9) * (4/9))) sqrt) / 2) sqrt). |
369 w := 3 * (((1 + (1 + ((4/9) * (4/9))) sqrt) / 2) sqrt). |
370 t1 := c sqrt. |
370 t1 := c sqrt. |
371 t2 := (Complex real: w imaginary: 4 / (2 * w)). |
371 t2 := (Complex real: w imaginary: 4 / (2 * w)). |
372 epsilon := 0.0000001. |
372 epsilon := 0.0000001. |
373 (t1 realPart - t2 realPart) < epsilon |
373 (t1 realPart - t2 realPart) < epsilon |
374 and:[ (t1 imaginaryPart - t2 imaginaryPart) < epsilon ] |
374 and:[ (t1 imaginaryPart - t2 imaginaryPart) < epsilon ] |
375 ]. |
375 ]. |
376 ]. |
376 ]. |
377 |
377 |
378 self shouldnt: [ c := Complex imaginary: -2 ] raise: Exception. |
378 self shouldnt: [ c := Complex imaginary: -2 ] raise: Exception. |
379 "self should: [ c sqrt = (Complex real: 1 imaginary: -1) ]. Should be true, but rounding bites us..." |
379 "self should: [ c sqrt = (Complex real: 1 imaginary: -1) ]. Should be true, but rounding bites us..." |
380 self should: [ (c sqrt realPart - 1) abs < 1.0e-10 ]. |
380 self should: [ (c sqrt realPart - 1) abs < 1.0e-10 ]. |
381 self should: [ (c sqrt imaginaryPart + 1) abs < 1.0e-10 ]. |
381 self should: [ (c sqrt imaginaryPart + 1) abs < 1.0e-10 ]. |
382 ! |
382 ! |
383 |
383 |
384 testSquared |
384 testSquared |
385 | c c2 | |
385 | c c2 | |
386 c := (6 - 6 i). |
386 c := (6 - 6 i). |