diff --git a/src/Math-Complex/PMComplexNumber.class.st b/src/Math-Complex/PMComplexNumber.class.st index 4033f660..9fbd1b60 100644 --- a/src/Math-Complex/PMComplexNumber.class.st +++ b/src/Math-Complex/PMComplexNumber.class.st @@ -484,7 +484,7 @@ PMComplexNumber >> divideFastAndSecureBy: anObject [ newReal := r * real + imaginary / d. r * imaginary - real / d ]. - ^ PMComplexNumber real: newReal imaginary: newImaginary ]. + ^ self class real: newReal imaginary: newImaginary ]. ^ anObject adaptToComplex: self andSend: #/ ] @@ -507,7 +507,7 @@ PMComplexNumber >> divideSecureBy: anObject [ newReal := ars*brs + (ais*bis) /s. newImaginary := ais*brs - (ars*bis)/s. - ^ PMComplexNumber real: newReal imaginary: newImaginary]. + ^ self class real: newReal imaginary: newImaginary]. ^ anObject adaptToComplex: self andSend: #/ ] diff --git a/src/Math-Tests-Complex/PMComplexNumberTest.class.st b/src/Math-Tests-Complex/PMComplexNumberTest.class.st index efddcf78..3f161527 100644 --- a/src/Math-Tests-Complex/PMComplexNumberTest.class.st +++ b/src/Math-Tests-Complex/PMComplexNumberTest.class.st @@ -630,20 +630,59 @@ PMComplexNumberTest >> testQuotientsOfComplexNumbersCannotBeWritten [ { #category : #'testing - mathematical functions' } PMComplexNumberTest >> testRaisedTo [ - | c c3 | - c := 5 - 6 i. - c3 := (c raisedTo: 0.2) raisedTo: 5. - self assert: c3 real closeTo: c real. - self assert: c3 imaginary closeTo: c imaginary + | expected squareRootOfZSquared z | + z := 5 - 6 i. + squareRootOfZSquared := (z raisedTo: 1 / 2) raisedTo: 2. + expected := 5 - 6 i. + self assert: squareRootOfZSquared closeTo: expected +] + +{ #category : #'testing - mathematical functions' } +PMComplexNumberTest >> testRaisedToFractionalIndex [ + + | z expected | + z := 1 / 2 + (3 sqrt / 2) i. + "Here, we appeal to de Moivre's theorem to compute the expected answer" + expected := 3 sqrt / 2 + (1 / 2) i. + self assert: (z raisedTo: 1 / 2) closeTo: expected ] { #category : #'testing - mathematical functions' } PMComplexNumberTest >> testRaisedToInteger [ + | c c3 | c := 5 - 6 i. c3 := c * c * c. - self assert: c3 equals: (c raisedToInteger: 3). - self assert: c3 reciprocal equals: (c raisedToInteger: -3) + self assert: (c raisedToInteger: -3) equals: c3 reciprocal +] + +{ #category : #'testing - mathematical functions' } +PMComplexNumberTest >> testRaisedToNegativeInteger [ + + | z expected | + z := 3 sqrt / 2 + (1 / 2) i. + "Here, we appeal to de Moivre's theorem to compute the expected answer" + expected := 0 - 1 i. + self assert: (z raisedTo: -3) closeTo: expected +] + +{ #category : #'testing - mathematical functions' } +PMComplexNumberTest >> testRaisedToPositiveNumber [ +| z expected | +z := (3 sqrt / 2) + (1/2) i. + "Here, we appeal to de Moivre's theorem to compute the expected answer" + expected := 0 + 1 i. + self assert: (z raisedTo: 3) closeTo: expected. +] + +{ #category : #'testing - mathematical functions' } +PMComplexNumberTest >> testRaisedToZero [ + + | z expected | + z := 3 sqrt / 2 + (1 / 2) i. + "Here, we appeal to de Moivre's theorem to compute the expected answer" + expected := 1 . + self assert: (z raisedTo: 0) equals: expected ] { #category : #tests } @@ -926,3 +965,21 @@ PMComplexNumberTest >> testZeroComplexNumberIsEqualToIntegerZero [ PMComplexNumberTest >> testZeroComplexNumbersDoNotHaveAReciprocal [ self should: [ PMComplexNumber zero reciprocal ] raise: ZeroDivide ] + +{ #category : #'testing - mathematical functions' } +PMComplexNumberTest >> testZeroRaisedToNegativePowerRaisesAZeroDivisionError [ + + | z expected | + z := PMComplexNumber zero. + expected := 0 + 1 i. + self should: [ z raisedTo: -3 ] raise: ZeroDivide +] + +{ #category : #'testing - mathematical functions' } +PMComplexNumberTest >> testZeroRaisedToPowerOfZero [ + + | z expected | + z := PMComplexNumber zero. + expected := PMComplexNumber one. + self assert: (z raisedTo: 0) equals: expected +]