[bug] my first squeak bugfix - Interval>>includes:

Alan Lovejoy sourcery at pacbell.net
Sat Jul 31 05:59:23 UTC 1999


> ** Original Sender: Alan Lovejoy <sourcery at pacbell.net>
> > ** Original Sender: merlyn at stonehenge.com (Randal L. Schwartz)
> >
> > Interval>>includes: fails if the increment is not 1.  Here's code that works:
> > 
> > includes: aNumber
> >   ^self increment = 1
> >     ifTrue: [aNumber between: self first and: self last]
> >     ifFalse: [super includes: aNumber] "fall back to enumerate items"
> >
> 
> Oh dear.  The ParcPlace code for Interval>includes: has worked since I first
> started using it, way back in 1986.  So I was shocked to see the following code
> in my Squeak image:
> 
> Interval>includes: aNumber
> 	^ aNumber between: self first and: self last
> 
> I guess this reinforces for me yet again that I should not assume that 
> things that have always been working correctly in the ParcPlace 
> image line are also good in Squeak (in spite of the fact that they both
> ultimately come from Xerox PARC stock).
> 
> As for the fix above, it will work, but I would like to offer an alternative:
> 
> Interval>includes: aNumber
> 	^ (aNumber between: self first and: self last)
> 		and: [self increment = 1
> 			ifTrue: [true]
> 			ifFalse: 	[aNumber - self first \\ self increment = 0]]
> 
> Kudos to Randy for finding this!
> 
Ahem. Well, after considering the flurry of replies to this, it has dawned on 
me that the fix above is not correct. It fails in the case where the increment 
is 1. Why? Because it assumes that <aNumber> is an integer and that <start>
is an integer (note the small i). So it will incorrectly report that 
<(1 to: 10 by: 1) includes: 5.5s> is true! 

Therefore, the new, improved fix is as follows: 

Interval>includes: aNumber 
	"Answer whether the receiver, considered as the set of values S, 
	where S = {x | x >= start, x <= stop, (x - start) mod step = 0}, 
	includes <aNumber>. If <aNumber> is a floating point value, 
	results are not defined." 

	(aNumber between: self first and: self last) ifFalse: [^false]. 
	(aNumber isInteger and: [self increment = 1 and: [self first isInteger]]) 
		ifTrue: [^true]. 
	^aNumber - self first \\ self increment = 0 

--Alan 





More information about the Squeak-dev mailing list