closeTo: generalized for all numbers
authorClaus Gittinger <cg@exept.de>
Tue, 18 Dec 2001 14:46:21 +0100
changeset 6335 8286bc57d05e
parent 6334 cbb0e8c8c1a5
child 6336 7accea3927f8
closeTo: generalized for all numbers
Float.st
Number.st
--- a/Float.st	Tue Dec 18 14:12:38 2001 +0100
+++ b/Float.st	Tue Dec 18 14:46:21 2001 +0100
@@ -566,29 +566,6 @@
     ^ true "/ this may be a lie 
 ! !
 
-!Float methodsFor:'Compatibility - Squeak'!
-
-closeTo: num
-	"are these two numbers close?"
-	| fuzz ans |
-	num isNumber ifFalse: [
-		[ans:=self = num] ifError: [:aString :aReceiver | ^ false].
-		^ ans].
-	self = 0.0 ifTrue: [^ num abs < 0.0001].
-	num = 0.0 ifTrue: [^ self abs < 0.0001].
-	self isNaN == num isNaN ifFalse: [^ false]. 
-	self isInfinite == num isInfinite ifFalse: [^ false].
-
-	fuzz := (self abs max: num abs) * 0.0001. 
-	^ (self - num) abs <= fuzz
-
-
-	"9.0 closeTo: 8.9999    
-	 9.9 closeTo: 9     
-	 (9/3) closeTo: 2.9999      
-	 "
-! !
-
 !Float methodsFor:'arithmetic'!
 
 * aNumber
@@ -2239,6 +2216,6 @@
 !Float class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Float.st,v 1.122 2001-11-16 13:21:03 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Float.st,v 1.123 2001-12-18 13:46:10 cg Exp $'
 ! !
 Float initialize!
--- a/Number.st	Tue Dec 18 14:12:38 2001 +0100
+++ b/Number.st	Tue Dec 18 14:46:21 2001 +0100
@@ -327,16 +327,37 @@
 "#(-500 -300 -150 -5 0 5 150 300 500 1200) collect: [:n | n asSmallAngleDegrees]"
 !
 
+closeFrom:aNumber
+    "are these two numbers close?"
+
+    | fuzz |
+
+    self isNaN == aNumber isNaN ifFalse: [^ false]. 
+    self isInfinite == aNumber isInfinite ifFalse: [^ false].
+
+    fuzz := (self abs max:aNumber abs) * 0.0001. 
+    ^ (self - aNumber) abs <= fuzz
+
+    "
+     9.0 closeTo: 8.9999     
+     9.9 closeTo: 9          
+     (9/3) closeTo: 2.9999      
+     1 closeTo: 0.9999      
+     1 closeTo: 1.0001      
+     1 closeTo: 1.001       
+     1 closeTo: 0.999       
+
+     0.9999 closeTo: 1      
+     1.0001 closeTo: 1      
+     1.001 closeTo: 1     
+     0.999 closeTo: 1     
+    "
+!
+
 closeTo:num
     "are these two numbers close?"
 
-    | ans |
-
-    self = num ifTrue:[^ true].
-
-    num isLimitedPrecisionReal "isReal" ifTrue: [^ num closeTo: self asFloat].
-    [ans := (self = num)] ifError: [:aString :aReceiver | ^ false].
-    ^ ans
+    ^ num closeFrom:self
 
     "
      1 closeTo:1.0000000001
@@ -722,5 +743,5 @@
 !Number class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.70 2001-12-05 09:23:46 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.71 2001-12-18 13:46:21 cg Exp $'
 ! !