representing rational numbers in Smalltalk

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, should you be 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. ;)

Smalltalk

" 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
  ]
]

This entry was posted in Programming and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>