Reputation: 917
Here is the division method in Squeak 4.1:
/t1
| t2 |
t1 isInteger
ifTrue: [t2 := self digitDiv: t1 abs neg: self negative ~~ t1 negative.
(t2 at: 2)
= 0
ifTrue: [^ (t2 at: 1) normalize].
^ (Fraction numerator: self denominator: t1) reduced].
^ t1 adaptToInteger: self andSend: #/
I do not understand the code. Can you give me some hints on how to debug the code, so I can trace the code behavior? Like open a workspace, type 4/3, I can inspect into Fraction. There are objects self, numerator, denominator and etc. How can I step into 4/3, and see how Smalltalk implemented division?
Upvotes: 1
Views: 521
Reputation: 4623
First of all, something is wrong with your sources. The method Integer>>/ actually looks like this:
/ 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.
(quoRem at: 2) = 0
ifTrue: [^ (quoRem at: 1) normalize]
ifFalse: [^ (Fraction numerator: self denominator: aNumber) reduced]].
^ aNumber adaptToInteger: self andSend: #/
Secondly, this code is only used for large integers. If you evaluate 4 / 3
this method is not used, but rather SmallInteger>>/ which creates a Fraction directly.
To invoke the method you want, you need to use a large number, e.g.:
12345678901234567890 / 2
Select this expression, and choose "debug it" from the context menu. Alternatively, you can use the "halt" message to invoke the debugger:
12345678901234567890 halt / 2
When the debugger pops up, click its "Into" button to step into the method.
Upvotes: 6