Skip to content

Commit b3633ef

Browse files
Merge pull request #232 from PolyMathOrg/complex-sqrt-characterisation-tests
Complex sqrt characterisation tests
2 parents 57e266a + 6edda9f commit b3633ef

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/Math-Tests-Complex/PMComplexTest.class.st

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,84 @@ PMComplexTest >> testSinh [
617617
self assert: (c sinh imaginary closeTo: c2 imaginary).
618618
]
619619

620+
{ #category : #tests }
621+
PMComplexTest >> testSquareRootOfANegativeRealNumberIsPureImaginary [
622+
623+
"Given z = -4 + 0 i, the square root is 2 i"
624+
625+
| squareRoot z |
626+
z := PMComplex real: -4 imaginary: 0.
627+
628+
squareRoot := z sqrt.
629+
630+
self assert: squareRoot equals: 2 i
631+
]
632+
633+
{ #category : #tests }
634+
PMComplexTest >> testSquareRootOfComplexNumberIsAComplexNumber [
635+
| squareRoot z |
636+
z := PMComplex real: 2 imaginary: 2.
637+
638+
squareRoot := z sqrt.
639+
640+
self assert: squareRoot real closeTo: 1.55377397.
641+
self assert: squareRoot imaginary closeTo: 0.643594253
642+
]
643+
644+
{ #category : #tests }
645+
PMComplexTest >> testSquareRootOfNegativePureImaginaryNumberIsAComplexNumberWithRealAndImaginaryParts [
646+
| squareRoot expected pureImaginaryNumber |
647+
pureImaginaryNumber := PMComplex real: 0 imaginary: -4.
648+
649+
squareRoot := pureImaginaryNumber sqrt.
650+
651+
expected := 2 sqrt negated + 2 sqrt i.
652+
self assert: squareRoot real closeTo: expected real.
653+
self assert: squareRoot imaginary closeTo: expected imaginary
654+
655+
]
656+
657+
{ #category : #tests }
658+
PMComplexTest >> testSquareRootOfPositivePureImaginaryNumberIsAComplexNumberWithRealAndImaginaryParts [
659+
660+
"e.g. square root of 4 i = root(2) + i root(2)"
661+
662+
| squareRoot expected pureImaginaryNumber |
663+
pureImaginaryNumber := PMComplex real: 0 imaginary: 4.
664+
665+
squareRoot := pureImaginaryNumber sqrt.
666+
667+
expected := 2 sqrt + 2 sqrt i.
668+
self assert: squareRoot real closeTo: expected real.
669+
self assert: squareRoot imaginary closeTo: expected imaginary
670+
]
671+
672+
{ #category : #tests }
673+
PMComplexTest >> testSquareRootOfPositiveRealNumberIsAComplexNumberWithOnlyARealPart [
674+
675+
"Given z = 6 + 0 i, then root z = root 6"
676+
677+
| squareRoot expected positiveRealNumber |
678+
positiveRealNumber := PMComplex real: 6 imaginary: 0.
679+
680+
squareRoot := positiveRealNumber sqrt.
681+
682+
expected := PMComplex real: 6 sqrt imaginary: 0.
683+
self assert: squareRoot equals: expected
684+
]
685+
686+
{ #category : #tests }
687+
PMComplexTest >> testSquareRootOfZeroIsZero [
688+
"comment stating purpose of instance-side method"
689+
"scope: class-variables & instance-variables"
690+
691+
| squareRoot expected |
692+
squareRoot := PMComplex zero sqrt .
693+
694+
expected := PMComplex zero.
695+
self assert: squareRoot equals: expected.
696+
]
697+
620698
{ #category : #tests }
621699
PMComplexTest >> testSquared [
622700
"self run: #testSquared"

0 commit comments

Comments
 (0)