# $Release Version: 0.5 $
# $Date: 1999/08/24 12:49:28 $
# by Keiju ISHITSUKA(SHL Japan Inc.)
# Documentation by Kevin Jackson and Gavin Sinclair.
# When you <tt>require 'rational'</tt>, all interactions between numbers
# potentially return a rational result. For example:
# 1.quo(2) # -> Rational(1,2)
# See Rational for full documentation.
# Creates a Rational number (i.e. a fraction). +a+ and +b+ should be Integers:
# Note: trying to construct a Rational with floating point or real values
# Rational(1.1, 2.3) # -> NoMethodError
if a.kind_of?(Rational) && b == 1
# Rational implements a rational class for numbers.
# <em>A rational number is a number that can be expressed as a fraction p/q
# where p and q are integers and q != 0. A rational number p/q is said to have
# numerator p and denominator q. Numbers that are not rational are called
# irrational numbers.</em> (http://mathworld.wolfram.com/RationalNumber.html)
# To create a Rational Number:
# Rational.new!(a,b) # -> a/b
# Rational numbers are reduced to their lowest terms:
# Rational(6,10) # -> 3/5
# But not if you use the unusual method "new!":
# Rational.new!(6,10) # -> 6/10
# Division by zero is obviously not allowed:
# Rational(3,0) # -> ZeroDivisionError
@RCS_ID='-$Id: rational.rb,v 1.7 1999/08/24 12:49:28 keiju Exp keiju $-'
# Reduces the given numerator and denominator to their lowest terms. Use
def Rational.reduce(num, den = 1)
raise ZeroDivisionError, "denominator is zero" if den == 0
if den == 1 && defined?(Unify)
# Implements the constructor. This method does not reduce to lowest terms or
# check for division by zero. Therefore #Rational() should be preferred in
def Rational.new!(num, den = 1)
private_class_method :new
# This method is actually private.
if num.kind_of?(Integer) and den.kind_of?(Integer)
# Returns the addition of this value and +a+.
# r = Rational(3,4) # -> Rational(3,4)
# r + 1 # -> Rational(7,4)
num = @numerator * a.denominator
num_a = a.numerator * @denominator
Rational(num + num_a, @denominator * a.denominator)
elsif a.kind_of?(Integer)
self + Rational.new!(a, 1)
# Returns the difference of this value and +a+.
# r = Rational(3,4) # -> Rational(3,4)
# r - 1 # -> Rational(-1,4)
num = @numerator * a.denominator
num_a = a.numerator * @denominator
Rational(num - num_a, @denominator*a.denominator)
elsif a.kind_of?(Integer)
self - Rational.new!(a, 1)
# Returns the product of this value and +a+.
# r = Rational(3,4) # -> Rational(3,4)
# r * 2 # -> Rational(3,2)
# r * 4 # -> Rational(3,1)
# r * Rational(1,2) # -> Rational(3,8)
num = @numerator * a.numerator
den = @denominator * a.denominator
elsif a.kind_of?(Integer)
self * Rational.new!(a, 1)
# Returns the quotient of this value and +a+.
# r = Rational(3,4) # -> Rational(3,4)
# r / 2 # -> Rational(3,8)
# r / Rational(1,2) # -> Rational(3,2)
num = @numerator * a.denominator
den = @denominator * a.numerator
elsif a.kind_of?(Integer)
raise ZeroDivisionError, "division by zero" if a == 0
self / Rational.new!(a, 1)
# Returns this value raised to the given power.
# r = Rational(3,4) # -> Rational(3,4)
# r ** 2 # -> Rational(9,16)
# r ** Rational(1,2) # -> 0.866025403784439
if other.kind_of?(Rational)
elsif other.kind_of?(Integer)
num = @numerator ** other
den = @denominator ** other
num = @denominator ** -other
den = @numerator ** -other
elsif other.kind_of?(Float)
x, y = other.coerce(self)
# Returns the remainder when this value is divided by +other+.
# r = Rational(7,4) # -> Rational(7,4)
# r % Rational(1,2) # -> Rational(1,4)
# r % 1 # -> Rational(3,4)
# r % Rational(1,7) # -> Rational(1,28)
value = (self / other).floor
return self - other * value
# Returns the quotient _and_ remainder.
# r = Rational(7,4) # -> Rational(7,4)
# r.divmod Rational(1,2) # -> [3, Rational(1,4)]
value = (self / other).floor
return value, self - other * value
# Returns the absolute value.
Rational.new!(-@numerator, @denominator)
# Returns +true+ iff this value is numerically equal to +other+.
# Rational(1,2) == Rational(4,8) # -> true
# Rational(1,2) == Rational.new!(4,8) # -> false
# Don't use Rational.new!
if other.kind_of?(Rational)
@numerator == other.numerator and @denominator == other.denominator
elsif other.kind_of?(Integer)
self == Rational.new!(other, 1)
elsif other.kind_of?(Float)
# Standard comparison operator.
if other.kind_of?(Rational)
num = @numerator * other.denominator
num_a = other.numerator * @denominator
elsif other.kind_of?(Integer)
return self <=> Rational.new!(other, 1)
elsif other.kind_of?(Float)
return Float(self) <=> other
elsif defined? other.coerce
x, y = other.coerce(self)
elsif other.kind_of?(Integer)
return Rational.new!(other, 1), self
# Converts the rational to an Integer. Not the _nearest_ integer, the
# truncated integer. Study the following example carefully:
# Rational(+7,4).to_i # -> 1
# Rational(-7,4).to_i # -> -1
# Rational(-7,4) == -1.75 # -> true
# Rational(-7,4).to_i == (-1.75).to_i # -> true
@numerator.div(@denominator)
-((-@numerator).div(@denominator))
return -((-@numerator).div(@denominator))
@numerator.div(@denominator)
alias_method :to_i, :truncate
num = num * 2 + @denominator
num = @numerator * 2 + @denominator
# Converts the rational to a Float.
@numerator.fdiv(@denominator)
# Returns a string representation of the rational number.
# Rational(3,4).to_s # "3/4"
@numerator.to_s+"/"+@denominator.to_s
# Returns a reconstructable string representation:
# Rational(5,8).inspect # -> "Rational(5, 8)"
sprintf("Rational(%s, %s)", @numerator.inspect, @denominator.inspect)
# Returns a hash code for the object.
@numerator.hash ^ @denominator.hash
# In an integer, the value _is_ the numerator of its rational equivalent.
# Therefore, this method returns +self+.
# In an integer, the denominator is 1. Therefore, this method returns 1.
# Returns a Rational representation of this integer.
# Returns the <em>greatest common denominator</em> of the two numbers (+self+
# The result is positive, no matter the sign of the arguments.
# Returns the <em>lowest common multiple</em> (LCM) of the two arguments
if self.zero? or other.zero?
(self.div(self.gcd(other)) * other).abs
# Returns the GCD _and_ the LCM (see #gcd and #lcm) of the two arguments
# (+self+ and +other+). This is more efficient than calculating them