## [squeak-dev] The Inbox: Kernel-nice.643.mcz

commits at source.squeak.org commits at source.squeak.org
Wed Oct 19 20:21:34 UTC 2011

```A new version of Kernel was added to project The Inbox:
http://source.squeak.org/inbox/Kernel-nice.643.mcz

==================== Summary ====================

Name: Kernel-nice.643
Author: nice
Time: 19 October 2011, 10:20:57.421 pm
UUID: 2a71e43b-e95f-468b-8eb1-f9f0395e5800
Ancestors: Kernel-nice.642

Speed-up mixed Fraction-Integer arithmetic.
This is at the expense of a few isFraction tests and code duplication.

=============== Diff against Kernel-nice.642 ===============

Item was changed:
----- Method: Fraction>>- (in category 'arithmetic') -----
- aNumber
+ 	| n d d1 d2 |
aNumber isFraction ifTrue:
+ 		[d := denominator gcd: aNumber denominator.
+ 		n := numerator * (d1 := aNumber denominator // d) - (aNumber numerator * (d2 := denominator // d)).
+ 		d1 := d1 * d2.
+ 		n := n // (d2 := n gcd: d).
+ 		(d := d1 * (d // d2)) = 1 ifTrue: [^ n].
+ 		^ Fraction numerator: n denominator: d].
- 		[^ self + aNumber negated].
^ aNumber adaptToFraction: self andSend: #-!

Item was changed:
----- Method: Fraction>>/ (in category 'arithmetic') -----
/ aNumber
+ 	| d1 d2 |
+ 	aNumber isFraction ifTrue:
+ 		[d1 := numerator gcd: aNumber numerator.
+ 		d2 := denominator gcd: aNumber denominator.
+ 		(d2 = denominator and: [d1 = aNumber numerator])
+ 			ifTrue: [^ numerator // d1 * (aNumber denominator // d2)].
+ 		^ Fraction numerator: numerator // d1 * (aNumber denominator // d2)
+ 				denominator: denominator // d2 * (aNumber numerator // d1)].
- 	aNumber isFraction
- 		ifTrue: [^self * aNumber reciprocal].
^ aNumber adaptToFraction: self andSend: #/!

Item was changed:
----- Method: Integer>>* (in category 'arithmetic') -----
* aNumber
"Refer to the comment in Number * "
aNumber isInteger ifTrue:
[^ self digitMultiply: aNumber
+ 					neg: self negative == aNumber negative == false].
+ 	aNumber isFraction
+ 		ifTrue:
+ 			[| n d |
+ 			n := self // (d := self gcd: aNumber denominator).
+ 			d := aNumber denominator // d.
+ 			^d = 1
+ 				ifTrue: [n * aNumber numerator]
+ 				ifFalse: [Fraction numerator: n * aNumber numerator denominator: d]].
- 					neg: self negative ~~ aNumber negative].
^ aNumber adaptToInteger: self andSend: #*!

Item was changed:
----- Method: Integer>>+ (in category 'arithmetic') -----
+ aNumber
"Refer to the comment in Number + "
aNumber isInteger ifTrue:
[self negative == aNumber negative
ifTrue: [^ (self digitAdd: aNumber) normalize]
ifFalse: [^ self digitSubtract: aNumber]].
+ 	aNumber isFraction ifTrue: [^Fraction numerator: self * aNumber denominator + aNumber numerator denominator: aNumber denominator].
^ aNumber adaptToInteger: self andSend: #+!

Item was changed:
----- Method: Integer>>- (in category 'arithmetic') -----
- aNumber
"Refer to the comment in Number - "
aNumber isInteger ifTrue:
[self negative == aNumber negative
ifTrue: [^ self digitSubtract: aNumber]
ifFalse: [^ (self digitAdd: aNumber) normalize]].
+ 	aNumber isFraction ifTrue: [^Fraction numerator: self * aNumber denominator - aNumber numerator denominator: aNumber denominator].
^ aNumber adaptToInteger: self andSend: #-!

Item was changed:
----- Method: Integer>>/ (in category 'arithmetic') -----
/ aNumber
"Refer to the comment in Number / "
| quoRem |
aNumber isInteger ifTrue:
[quoRem := self digitDiv: aNumber abs	"*****I've added abs here*****"
+ 						neg: self negative == aNumber negative == false.
- 						neg: self negative ~~ aNumber negative.
(quoRem at: 2) = 0
ifTrue: [^ (quoRem at: 1) normalize]
ifFalse: [^ (Fraction numerator: self denominator: aNumber) reduced]].
+ 	aNumber isFraction
+ 		ifTrue:
+ 			[| n d |
+ 			n := self // (d := self gcd: aNumber numerator).
+ 			d := aNumber numerator // d.
+ 			^d abs = 1
+ 				ifTrue: [n * aNumber denominator * d]
+ 				ifFalse: [Fraction numerator: n * aNumber denominator denominator: d]].
^ aNumber adaptToInteger: self andSend: #/!

```