Search code examples
smalltalkfractionssqueak

Squeak Smalltalk, why sometimes the reduced method doesn't work?


(2332 / 2332) reduced 
(2332 / 2) reduced 
(2332 / 322) reduced  (1166/161)
(2332 / 3) reduced  (2332/3)
(2332 / 2432423) reduced  (2332/2432423)

Look at the above codes. The first and second, when printed, do not work. The MessageNotUnderstood window pops up. And the 3rd, 4th, 5th code are okay. Results come out right.

Why does the reduced method not work?

Is it because the reduced method fails to handle final results which are integers like Uko guesses ?


Solution

  • Fractions are reduced automatically in the / method. There is no need to send the reduced message.

    E.g. if you print the result of

    2 / 4
    

    you get the reduced (1/2) automatically.

    If you print the result of

    2332 / 2332
    

    it is reduced to 1 which is not a Fraction, but an Integer, and Integers do not understand the reduced message. That's why you get an error.

    The only case when a Fraction is not automatically reduced is when you create it manually, as in

    Fraction numerator: 2 denominator: 4
    

    which will answer the non-reduced (2/4). But in normal arithmetic expressions you never need to send reduced.