Intervals

Jarvis, Robert P. Jarvisb at timken.com
Tue Nov 30 14:21:58 UTC 1999


> -----Original Message-----
> From:	David N. Smith (IBM) [SMTP:dnsmith at watson.ibm.com]
> Sent:	Monday, November 29, 1999 6:42 PM
> To:	squeak at cs.uiuc.edu
> Subject:	RE: Intervals
> 
> At 13:14 -0500 11/29/99, Jarvis, Robert P. wrote:
> >The attached change set combines Andrew's changes with Dan's suggestions
> and
> >runs correctly for the test cases identified, as well as for negative
> >numbers, i.e.
> 
> Hummmm...  I've been looking at the code and I wonder if it will 
> always work with floating point values? Stepping through the interval 
> involves adding the step value at each increment, but asking if a 
> value is in the interval involves modulo arithmetic. The problem 
> comes when the step value cannot be exactly represented in binary 
> (0.7 for example).
> 
> I can't believe it will always work identically, but I've not been 
> able to come up with a test case that fails. (I implemented a version 
> if #includes: that is simply:
> 
>     includesP: aValue
>        ^ self asArray includes: aValue
> 
> and used it for comparison. This IS a precise and accurate solution, 
> though it might run out of memory at inconvenient times and otherwise 
> misbehave when the interval has lots of elements.
> 
Using the changes I just (re)posted I find that

	(1.0 to: 20.0 by: 0.2) includes: 1.2

answers false, where it should answer true.  Herewith is the changed change
set which adds a fudge factor to dodge around the problems with Float step
values.

'From Squeak2.6 of 11 October 1999 [latest update: #1559] on 30 November
1999 at 8:58:03 am'!

!Interval methodsFor: 'accessing' stamp: 'rpj 11/30/1999 08:55'!
includes: aNumber
	"Determine if aNumber is an element of this interval.  Include a
fudge factor to account for Float step values."
	^(self rangeIncludes: aNumber) and: [ aNumber - self first \\ step =
0 or: [ (aNumber - self first \\ step - step) abs < 0.000000000000001 ] ]! !

!Interval methodsFor: 'accessing' stamp: 'rpj 11/29/1999 11:39'!
rangeIncludes: aNumber
	^aNumber between: (self first min: self last) and: (self first max:
self last)! !

Bob Jarvis
Compuware @ Timken





More information about the Squeak-dev mailing list