For my graduate-level programming languages class, I wrote this class that represents a rational number in Smalltalk. I figured I would share my source code with the interwebs for anyone else trying to learn the language. I release the code under the GNU General Public License v3.

Note for students: my professor requested I state that, if you’re taking his programming languages class CS 655 at the University of Kentucky and you try to use this code, (1) you’ll get in trouble for using someone else’s work and (2) you have to include the GPL and my copyright notice, which would be a big hint that it’s not entirely your work. ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
" Copyright 2009 Sarah Vessels
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. "
Object subclass: #Rational.
Rational instanceVariableNames: 'numerator denominator'.
Rational comment: 'I represent a fraction'.

" Thanks to http://en.wikipedia.org/wiki/Euclidean_algorithm "
Rational class extend [
  getGCD: a other: b [
    |A B|
    A := a.
    B := b.
    [B ~= 0] whileTrue: [
      |tmp|
      tmp := B.
      B := A \\ B.
      A := tmp.
    ].
    ^A
  ]
]

" Rational instance methods "
Rational extend [
  " Given a numerator and a non-zero denominator, this will initialize the
    Rational. "
  init: num over: denom [
    |gcd|
    denom = 0 ifTrue: [
      self error: 'Cannot have a 0 denominator'
    ].

    (num isKindOf: Integer) ifFalse: [
      self error: 'Must have an Integer numerator'
    ].

    (denom isKindOf: Integer) ifFalse: [
      self error: 'Must have a nonzero Integer denominator'
    ].

    gcd := (Rational class new) getGCD: num other: denom.
    numerator := (num / gcd).
    denominator := (denom / gcd).
  ]

  " Given another Rational instance, this will return the Rational sum of this
    instance and the other instance. "
  add: other [
    |otherRat|
    (other isKindOf: Integer) ifTrue: [
      otherRat := (Rational new) init: (other * denominator) over: denominator
    ] ifFalse: [
      (other isKindOf: Rational) ifTrue: [
        otherRat := other
      ] ifFalse: [
        (self error: 'Do not know how to add ', ((other class) printString),
         ' to Rational')
      ]
    ].

    denominator = (otherRat getDenominator) ifTrue: [
      ^(Rational new) init: (numerator + (otherRat getNumerator)) over: denominator
    ] ifFalse: [
      |lcd scaledNum1 scaledNum2|
      lcd := self getLCD: otherRat.
      scaledNum1 := self scaleNumerator: lcd.
      scaledNum2 := otherRat scaleNumerator: lcd.
      ^(Rational new) init: (scaledNum1 + scaledNum2) over: lcd
    ]
  ]

  " Will approximate the decimal value of the Rational. "
  approximate [ ^(numerator * 1.0) / denominator ]

  " Given a Rational, compareTo compares this instance with the other Rational
    instance, returning -1, 0, or 1 depending on whether this instance is less
    than, equal to, or greater than the given instance, respectively. "
  compareTo: other [
    |otherDenom otherNum lcd|
    (self equals: other) ifTrue: [ ^0 ].

    (self isPositive) ifTrue: [
      (other isNegative) ifTrue: [ ^1 ]
    ].

    (self isNegative) ifTrue: [
      (other isPositive) ifTrue: [ ^-1 ]
    ].

    otherDenom := other getDenominator.
    otherNum := other getNumerator.

    denominator = otherDenom ifTrue: [
      numerator &gt; otherNum ifTrue: [ ^1 ]
      ifFalse: [
        numerator = otherNum ifTrue: [ ^0 ]
                             ifFalse: [ ^-1 ]
      ]
    ].

    numerator = otherNum ifTrue: [
      denominator &gt; otherDenom ifTrue: [ ^-1 ]
                               ifFalse: [ ^1 ]
    ].

    lcd := self getLCD: other.

    ((self scaleNumerator: lcd) &gt; (other scaleNumerator: lcd)) ifTrue: [
      ^1
    ] ifFalse: [
      ^-1
    ]
  ]

  " Given an Integer or Rational, this will divide this Rational by that
    value. "
  divide: other [
    |otherFrac reciprocal|
    (other isKindOf: Integer) ifTrue: [
      otherFrac := (Rational new) init: other over: 1.
    ] ifFalse: [
      (other isKindOf: Rational) ifTrue: [
        otherFrac := other
      ] ifFalse: [
        (self error: 'Do not know how to divide a Ratioanl by ',
          ((other class) printString))
      ]
    ].

    reciprocal := ((Rational new) init: (otherFrac getDenominator)
      over: (otherFrac getNumerator)).
    ^self multiply: reciprocal
  ]

  " Returns true if the given Rational equals this Rational, either as the same
    object or it represents the same fraction. "
  equals: other [
    self = other ifTrue: [
      ^true
    ] ifFalse: [
      numerator = (other getNumerator) ifTrue: [
        denominator = (other getDenominator) ifTrue: [
          ^true
        ] ifFalse: [
          ^false
        ]
      ] ifFalse: [
        ^false
      ]
    ]
  ]

  " Returns the denominator of the Rational. "
  getDenominator [ ^denominator ]

  " Given another instance of Rational, this will get the least common
    denominator (a.k.a. least common multiple) of the two denominators. "
  getLCD: other [
    |diff denominator2 product gcd|
    denominator2 := other getDenominator.
    diff := (denominator - denominator2) abs.

    1 = diff ifTrue: [ ^denominator * denominator2 ].
    0 = diff ifTrue: [ ^denominator ].

    product := (denominator * denominator2) abs.
    gcd := (Rational class new) getGCD: denominator other: denominator2.
    ^product / gcd
  ]

  " Returns the numerator of the Rational. "
  getNumerator [ ^numerator ]

  " Returns the reciprocal of this Rational. "
  getReciprocal [ ^(Rational new) init: denominator over: numerator ]

  " Returns true if this Rational is larger than the one given. "
  greaterThan: other [ ^(self compareTo: other) = 1 ]

  " Returns true if this Rational is larger than the one given, or has the
    same value. "
  greaterThanOrEqualTo: other [
    ^(self compareTo: other) &gt; -1
  ]

  " Will return true if this Rational is &lt; 0. "
  isNegative [
    ^self isPositive not
  ]

  " Will return true if this Rational number is &gt;= 0. "
  isPositive [
    numerator &gt; 0 ifTrue: [ denominator &gt; 0 ifTrue: [ ^true ] ].
    numerator &lt; 0 ifTrue: [ denominator &lt; 0 ifTrue: [ ^true ] ].
    numerator = 0 ifTrue: [ ^true ].
    ^false
  ]

  " Returns true if this Rational is smaller than the one given. "
  lessThan: other [
    ^(self compareTo: other) = -1
  ]

  " Returns true if this Rational is smaller than the one given, or has the
    same value. "
  lessThanOrEqualTo: other [
    ^(self compareTo: other) &lt; 1
  ]

  " Given an Integer, this will scale the Rational by that amount.  Given
    another Rational, this will return the product of this Rational and the one
    given. "
  multiply: other [
    |newNum newDenom|
    (other isKindOf: Integer) ifTrue: [
      newNum := numerator * other.
      newDenom := denominator
    ] ifFalse: [
      (other isKindOf: Rational) ifTrue: [
        newNum := numerator * (other getNumerator).
        newDenom := denominator * (other getDenominator)
      ] ifFalse: [
        self error: 'Do not know how to multiply by ', ((other class) printString)
      ]
    ].

    ^((Rational new) init: newNum over: newDenom)
  ]

  " Returns a string representation of the Rational. "
  printString [
    ^(numerator printString), '/', (denominator printString)
  ]

  " Given a least common denominator, this will return the numerator scaled up
    such that it pairs with the least common denominator. "
  scaleNumerator: lcd [
    denominator = lcd ifTrue: [
      ^numerator
    ] ifFalse: [
      |factor|
      factor := lcd / denominator.
      ^numerator * factor
    ]
  ]

  " Given another Rational, this will subtract the given Rational from this
    instance, returning the result as a Rational. "
  subtract: other [
    |negated negNum otherRat|
    (other isKindOf: Integer) ifTrue: [
      otherRat := (Rational new) init: (denominator * other) over: denominator
    ] ifFalse: [
      (other isKindOf: Rational) ifTrue: [
        otherRat := other
      ] ifFalse: [
        (self error: 'Do not know how to subtract ',
         ((other class) printString), ' from Rational')
      ]
    ].

    negNum := 0 - (otherRat getNumerator).
    negated := (Rational new) init: negNum over: (otherRat getDenominator).
    ^self add: negated
  ]
]