这是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: #/
Run Code Online (Sandbox Code Playgroud)
我不懂代码.你能给我一些关于如何调试代码的提示,所以我可以跟踪代码行为吗?就像打开工作区一样,键入4/3,我可以检查分数.有自己的对象,分子,分母等.我怎样才能进入4/3,看看Smalltalk如何实现除法?
首先,你的消息来源有问题.方法Integer >> /实际上是这样的:
/ 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: #/
Run Code Online (Sandbox Code Playgroud)
其次,此代码仅用于大整数.如果您评估4 / 3此方法未使用,而是使用SmallInteger >> /直接创建Fraction.
要调用所需的方法,需要使用大数字,例如:
12345678901234567890 / 2
Run Code Online (Sandbox Code Playgroud)
选择此表达式,然后从上下文菜单中选择"debug it".或者,您可以使用"暂停"消息来调用调试器:
12345678901234567890 halt / 2
Run Code Online (Sandbox Code Playgroud)
弹出调试器后,单击其"进入"按钮以进入该方法.
| 归档时间: |
|
| 查看次数: |
443 次 |
| 最近记录: |