# Rational (Class)

In: | rational.rb
Parent: | Numeric

## Methods

% * ** + - / <=> == abs coerce divmod hash inspect new new! reduce to_f to_i to_r to_s

## Attributes

denominator | [R] |
numerator | [R] |

## Public Class methods

reduce(num, den = 1)

[Source]

    # 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

new!(num, den = 1)

[Source]

    # File rational.rb, line 67
      def Rational.new!(num, den = 1)
        new(num, den)
      end

new(num, den)

[Source]

    # 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

## Public Instance methods

+(a)

[Source]

    # 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

-(a)

[Source]

    # 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

*(a)

[Source]

    # 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

/(a)

[Source]

    # 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

**(other)

[Source]

    # 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

%(other)

[Source]

    # File rational.rb, line 171
      def % (other)
        value = (self / other).to_i
        return self - other * value
      end

divmod(other)

[Source]

    # File rational.rb, line 176
      def divmod(other)
        value = (self / other).to_i
        return value, self - other * value
      end

abs()

[Source]

    # File rational.rb, line 181
      def abs
        if @numerator > 0
          Rational.new!(@numerator, @denominator)
        else
          Rational.new!(-@numerator, @denominator)
        end
      end

==(other)

[Source]

    # 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

<=>(other)

[Source]

    # 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

coerce(other)

[Source]

    # 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

to_i()

[Source]

    # File rational.rb, line 235
      def to_i
        Integer(@numerator.div(@denominator))
      end

to_f()

[Source]

    # File rational.rb, line 239
      def to_f
        @numerator.to_f/@denominator.to_f
      end

to_s()

[Source]

    # File rational.rb, line 243
      def to_s
        if @denominator == 1
          @numerator.to_s
        else
          @numerator.to_s+"/"+@denominator.to_s
        end
      end

to_r()

[Source]

    # File rational.rb, line 251
      def to_r
        self
      end

inspect()

[Source]

    # File rational.rb, line 255
      def inspect
        sprintf("Rational(%s, %s)", @numerator.inspect, @denominator.inspect)
      end

hash()

[Source]

    # File rational.rb, line 259
      def hash
        @numerator.hash ^ @denominator.hash
      end

[Validate]