Created
May 26, 2014 12:11
-
-
Save holsee/1c3f7014001062d6ac5c to your computer and use it in GitHub Desktop.
Rationals Worksheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
val x = new Rational(1, 3) | |
val y = new Rational(5, 7) | |
val z = new Rational(3, 2) | |
x.numer | |
x.denom | |
x - y - z | |
y + y | |
x < y | |
x max y | |
-x | |
class Rational(x:Int, y:Int) { | |
@tailrec | |
private def gcd(a:Int, b:Int): Int = | |
if(b == 0) a else gcd(b, a % b) | |
private val g = gcd(x,y) | |
def numer = x / g | |
def denom = y / g | |
def < (that: Rational) = | |
numer * that.denom < that.numer * denom | |
def max(that: Rational) = | |
if (this < that) that else this | |
// Note space after the : as it is legal operator | |
// Special Scala convention for prefix operator | |
def unary_- :Rational = new Rational(-numer, denom) | |
def +(that: Rational) = | |
new Rational(numer * that.denom + that.numer * denom, denom * that.denom) | |
def -(that: Rational) = this + -that | |
override def toString = numer + "/" + denom | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment