In: |
rational.rb
|
Parent: | Numeric |
denominator | [R] | |
numerator | [R] |
# File rational.rb, line 50 def Rational.reduce(num, den = 1) raise ZeroDivisionError, "denominator is 0" if den == 0 if den < 0 num = -num den = -den end gcd = num.gcd(den) num = num.div(gcd) den = den.div(gcd) if den == 1 && defined?(Unify) num else new!(num, den) end end
# File rational.rb, line 73 def initialize(num, den) if den < 0 num = -num den = -den end if num.kind_of?(Integer) and den.kind_of?(Integer) @numerator = num @denominator = den else @numerator = num.to_i @denominator = den.to_i end end
# File rational.rb, line 87 def + (a) if a.kind_of?(Rational) 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) elsif a.kind_of?(Float) Float(self) + a else x, y = a.coerce(self) x + y end end
# File rational.rb, line 102 def - (a) if a.kind_of?(Rational) 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) elsif a.kind_of?(Float) Float(self) - a else x, y = a.coerce(self) x - y end end
# File rational.rb, line 117 def * (a) if a.kind_of?(Rational) num = @numerator * a.numerator den = @denominator * a.denominator Rational(num, den) elsif a.kind_of?(Integer) self * Rational.new!(a, 1) elsif a.kind_of?(Float) Float(self) * a else x, y = a.coerce(self) x * y end end
# File rational.rb, line 132 def / (a) if a.kind_of?(Rational) num = @numerator * a.denominator den = @denominator * a.numerator Rational(num, den) elsif a.kind_of?(Integer) raise ZeroDivisionError, "divided by 0" if a == 0 self / Rational.new!(a, 1) elsif a.kind_of?(Float) Float(self) / a else x, y = a.coerce(self) x / y end end
# File rational.rb, line 148 def ** (other) if other.kind_of?(Rational) Float(self) ** other elsif other.kind_of?(Integer) if other > 0 num = @numerator ** other den = @denominator ** other elsif other < 0 num = @denominator ** -other den = @numerator ** -other elsif other == 0 num = 1 den = 1 end Rational.new!(num, den) elsif other.kind_of?(Float) Float(self) ** other else x, y = other.coerce(self) x ** y end end
# File rational.rb, line 171 def % (other) value = (self / other).to_i return self - other * value end
# File rational.rb, line 176 def divmod(other) value = (self / other).to_i return value, self - other * value end
# File rational.rb, line 181 def abs if @numerator > 0 Rational.new!(@numerator, @denominator) else Rational.new!(-@numerator, @denominator) end end
# File rational.rb, line 189 def == (other) 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) Float(self) == other else other == self end end
# File rational.rb, line 201 def <=> (other) if other.kind_of?(Rational) num = @numerator * other.denominator num_a = other.numerator * @denominator v = num - num_a if v > 0 return 1 elsif v < 0 return -1 else return 0 end 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) return x <=> y else return nil end end
# File rational.rb, line 225 def coerce(other) if other.kind_of?(Float) return other, self.to_f elsif other.kind_of?(Integer) return Rational.new!(other, 1), self else super end end
# File rational.rb, line 243 def to_s if @denominator == 1 @numerator.to_s else @numerator.to_s+"/"+@denominator.to_s end end