1

这是 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: #/

我不明白代码。你能给我一些关于如何调试代码的提示,以便我可以跟踪代码行为吗?就像打开一个工作区一样,输入 4/3,我可以检查 Fraction。对象有self、分子、分母等。如何步入4/3,看看Smalltalk是如何实现除法的?

4

1 回答 1

6

首先,您的消息来源有问题。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: #/

其次,此代码仅用于大整数。如果评估4 / 3不使用此方法,而是使用SmallInteger>>/直接创建 Fraction。

要调用您想要的方法,您需要使用一个大数字,例如:

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.

于 2011-02-14T14:14:51.847 回答