[squeak-dev] #closeTo: on small numbers

St├ęphane Rollandin lecteur at zogotounga.net
Sun Mar 8 10:21:54 UTC 2020


Hello all,

I just got bitten by the fact that

  1.2246063538223773e-15 closeTo: 3.552713678800501e-15

retuns false.

Now Float>>closeTo: has special tests for zero, so that when a number is 
compared to zero the test is based on an absolute deviation.

But in the above case, none of the numbers are actually zero so the 
zero-specific test is bypassed, and the regular check based on relative 
deviation fails.

I guess this all works as expected. But I am wondering: would #closeTo: 
still be consistent if we implemented this way:

closeTo: num
  	"are these two numbers close?"
	num isNumber ifFalse: [^[self = num] ifError: [false]].
	self = 0.0 ifTrue: [^num abs < 0.0001].
	num = 0 ifTrue: [^self abs < 0.0001].
	(self closeTo: 0.0) ifTrue: [^self abs < 0.0001].
	^self = num asFloat
		or: [(self - num) abs / (self abs max: num abs) < 0.0001]


There I added the (self closeTo: 0.0) case to have two near-zero numbers 
compared as if one of them was indeed zero.

Would nasty side effects be expected?


Stef


More information about the Squeak-dev mailing list