--- a/Fraction.st Wed Oct 28 09:09:08 2015 +0100
+++ b/Fraction.st Wed Oct 28 09:09:42 2015 +0100
@@ -439,9 +439,10 @@
negated
"optional - could use inherited method ..."
- ^ self class
- numerator:(numerator negated)
- denominator:denominator
+ "/ no need to reduce - I am already
+ ^ self class basicNew
+ setNumerator:(numerator negated)
+ denominator:denominator
"Modified: 5.11.1996 / 10:29:11 / cg"
!
@@ -450,9 +451,10 @@
"optional - could use inherited method ..."
numerator == 1 ifTrue:[^ denominator].
- ^ self class
- numerator:denominator
- denominator:numerator
+ "/ no need to reduce - I am already
+ ^ self class basicNew
+ setNumerator:denominator
+ denominator:numerator
"Modified: 5.11.1996 / 10:29:22 / cg"
! !
@@ -691,11 +693,10 @@
hash
"return a number for hashing; redefined, since fractions compare
- by numeric value (i.e. (9/3) = 3), therefore (9/3) hash must be the same
- as 3 hash."
+ by numeric value (i.e. (1/2) = 0.5), hash values must be the same"
- (denominator = 1) ifTrue:[^ numerator hash].
-
+ (denominator == 1) ifTrue:[^ numerator hash].
+ (denominator == -1) ifTrue:[^ numerator hash negated].
^ self asFloat hash
"
@@ -708,6 +709,11 @@
0.5 hash
0.25 hash
0.4 hash
+
+ 0.25 hash
+ -0.25 hash
+ (1/4) hash
+ (-1/4) hash
"
!
@@ -825,7 +831,8 @@
!
lessFromFraction:aFraction
- "sent when a fraction does not know how to compare to the receiver"
+ "sent when a fraction does not know how to compare to the receiver.
+ Return true if aFraction < self."
|n d|
@@ -834,13 +841,14 @@
"/ save a multiplication if possible
d == denominator ifTrue:[
- ^ n < numerator
+ ^ n < numerator
].
^ (denominator * n) < (numerator * d)
!
lessFromInteger:anInteger
- "sent when an integer does not know how to compare to the receiver, a fraction"
+ "sent when an integer does not know how to compare to the receiver, a fraction.
+ Return true if anInteger < self."
^ (denominator * anInteger) < numerator
!
@@ -1089,10 +1097,10 @@
!
negative
- "return true if the receiver is negative"
+ "return true if the receiver is less than zero"
(numerator < 0) ifTrue:[
- ^ (denominator < 0) not
+ ^ (denominator < 0) not
].
^ (denominator < 0)
! !