A rational number is a number that can be expressed as a ratio n/d, where n and d are integers, except that d cannot be zero.
n is called the numerator and d the denominator.
Each rational number will be represented by on Rational object. when you add two Rational objects, you'll create a new Rational object to hold the sum.
scala> val oneHalf = new Rational(1, 2)
oneHalf: Rational = 1/2
scala> val twoThirds = new Rational(2, 3)
twoThirds: Rational = 2/3
scala> (oneHalf / 7) + (1 twoThirds)
res0: Rational = 17/42
댓글 없음:
댓글 쓰기