[BUG] Complex equality problem

nicolas cellier ncellier at ifrance.com
Wed Feb 8 21:18:55 UTC 2006


One more evidence of Complex equality problem.
(Am i sole user of Complex in the world ?).

try:
    1 i = nil.

I am joining a SUnit test (already existed, it is a enhanced version).

I am joining what i think a good patch.
I use #isNumberExtension testing implemented in Object Number and Complex.
(this would extend naturally to quaternions, octonions, ...)

This will replace the isNumber test in Number subclass >> =
(needed against the 1 = (1 + 0 i) bug)

Since the bug is already published on mantis how do i let them know about this 
fix ?
-------------- next part --------------
'From Squeak3.8 of ''5 May 2005'' [latest update: #6665] on 8 February 2006 at 10:02:41 pm'!

!Object methodsFor: 'testing' stamp: 'nice 2/8/2006 22:00'!
isNumberExtension
	"Answer whether or numbers can be considered as being special representation of this class.
	Like for example, numbers can be considered as complex numbers with null imaginary part.
	Default is to answer false."

	^false! !


!Complex methodsFor: 'comparing' stamp: 'nice 2/8/2006 21:56'!
= anObject 
	"answer true if anObject is equal to the receiver"

	^ anObject isComplex
		ifTrue: [real = anObject real & (imaginary = anObject imaginary)]
		ifFalse: [anObject isNumberExtension
				and: [anObject adaptToComplex: self andSend: #=]]! !

!Complex methodsFor: 'testing' stamp: 'nice 2/8/2006 21:57'!
isNumberExtension
	"Answer true since numbers can be considered as special complex"

	^true! !


!Number methodsFor: 'testing' stamp: 'nice 2/8/2006 21:58'!
isNumberExtension
	"Answer true since numbers can be considered extension of themselves"

	^true! !


!Float methodsFor: 'comparing' stamp: 'nice 2/8/2006 22:01'!
= aNumber 
	"Primitive. Compare the receiver with the argument and return true
	if the receiver is equal to the argument. Otherwise return false.
	Fail if the argument is not a Float. Essential. See Object documentation
	whatIsAPrimitive."

	<primitive: 47>
	aNumber isNumberExtension ifFalse: [^ false].
	^ aNumber adaptToFloat: self andSend: #=! !


!Fraction methodsFor: 'comparing' stamp: 'nice 2/8/2006 22:01'!
= aNumber
	aNumber isNumberExtension ifFalse: [^ false].
	aNumber isFraction
		ifTrue: [numerator = 0 ifTrue: [^ aNumber numerator = 0].
				^ (numerator * aNumber denominator) =
					(aNumber numerator * denominator)
				"Note: used to just compare num and denom,
					but this fails for improper fractions"].
	^ aNumber adaptToFraction: self andSend: #=! !


!Integer methodsFor: 'comparing' stamp: 'nice 2/8/2006 22:01'!
= aNumber
	aNumber isNumberExtension ifFalse: [^ false].
	aNumber isInteger ifTrue:
		[aNumber negative == self negative
			ifTrue: [^ (self digitCompare: aNumber) = 0]
			ifFalse: [^ false]].
	^ aNumber adaptToInteger: self andSend: #=! !


!ScaledDecimal methodsFor: 'comparing' stamp: 'nice 2/8/2006 22:02'!
= comparand 
	"Implementation of Number 'comparing' method."
	comparand isNumberExtension ifFalse: [^ false].
	(comparand isKindOf: ScaledDecimal) ifTrue: [^ fraction = comparand asFraction].
	^ comparand adaptToScaledDecimal: self andSend: #=! !

Object removeSelector: #adaptToComplex:andSend:!
-------------- next part --------------
'From Squeak3.8 of ''5 May 2005'' [latest update: #6665] on 8 February 2006 at 10:09:54 pm'!

!ComplexTest methodsFor: 'testing' stamp: 'nice 2/8/2006 22:09'!
testEquality
	"self run: #testEquality"
	"self debug: #testEquality"
	
	self assert: 0i = 0.
	self assert: (2 - 5i) = ((1 -4 i) + (1 - 1i)).
	self assert: 0i isZero.
	self deny: (1 + 3 i) = 1.
	self deny: (1 + 3 i) = (1 + 2i).

"Some more stuff"
	self deny: (1 i) = nil.
	self deny: nil = (1 i).

	self deny: (1 i) = #(1 2 3).
	self deny: #(1 2 3) = (1 i).

	self deny: (1 i) = 0.
	self deny: 0 = (1 i).

	self assert:  (1 + 0 i) = 1.
	self assert:  1 = (1+ 0 i).

	self assert:  (1 + 0 i) = 1.0.
	self assert:  1.0 = (1+ 0 i).

	self assert:  (1/2 + 0 i) = (1/2).
	self assert:  (1/2) = (1/2+ 0 i).! !



More information about the Squeak-dev mailing list