Dale King
Posts: 3
Nickname: daleking
Registered: Dec, 2003


Re: Functional Objects Chapter : Rational Numbers

Posted: Sep 19, 2012 10:46 AM


I have one slight nitpick about the Rational example. It doesn't do a good job handling negative denominators in a "rational" way.
For example:
println(new Rational(1,3)  new Rational(1,3)) println(new Rational(1,3)  new Rational(1,3))
which are the same value mathematically, but the output is:
2/3 2/3
The code should have one way to handle the sign. Most people want to see any negative sign in the numerator, not the denominator.
The easy fix is to change to these two lines:
val numer = d.signum * n / g val denom = d.abs / g
which keeps any negative sign in the numerator
This code snippet did not override the == operator, but being consistent with the sign makes implementation of one trivial (just compare numerators and denominators).

