class MathExpansion::Fraccion

Public Class Methods

new(x,y) click to toggle source

Métodos principales

# File lib/math_expansion/racional.rb, line 9
def initialize(x,y)
    raise ArgumentError , 'Argumentos no enteros.' unless x.is_a? Fixnum and y.is_a? Fixnum
    raise ArgumentError , 'Denominador nulo.' unless y != 0

    @num, @den = x, y
    reducir
  
    # En caso de ser negativa, la fracción será -a/b, y no a/(-b)
    if(@num < 0 && @den < 0)
        @num = -@num
        @den = -@den
    elsif(@den < 0)
        @den = -@den
        @num = -@num
    end
end
null() click to toggle source
# File lib/math_expansion/racional.rb, line 38
def self.null
    Fraccion.new(0,1)
end

Public Instance Methods

%(other) click to toggle source
# File lib/math_expansion/racional.rb, line 124
def %(other) # Operación módulo
    raise ArgumentError, 'Argumento no racional' unless other.is_a? Fraccion
    
    Fraccion.new(0,1) # Resto de una división de fracciones = siempre nulo (0/1)
end
*(other) click to toggle source
# File lib/math_expansion/racional.rb, line 96
def *(other) # Operación producto
    if(other.respond_to? :den and other.respond_to? :num)
      if(@num*other.num == 0)
        Fraccion.null
      else
        Fraccion.new(@num*other.num, @den*other.den) # a/b * c/d = (a*c)/(b*d)
      end
    else
      Fraccion.new(@num*other, @den)  # a/b * c = (a*c)/b
    end
end
+(other) click to toggle source

Operadores aritméticos

# File lib/math_expansion/racional.rb, line 80
def +(other) # Operación suma
    if(other.respond_to? :den and other.respond_to? :num)
      Fraccion.new(@num*other.den + @den*other.num, @den*other.den) # a/b + c/d = (a*d + b*c)/(b*d)
    else
      Fraccion.new(@num + @den*other, @den) # a/b + c = (a + b*c)/b
    end
end
-(other) click to toggle source
# File lib/math_expansion/racional.rb, line 88
def -(other) # Operación resta
    if(other.respond_to? :den and other.respond_to? :num)
      Fraccion.new(@num*other.den - @den*other.num, @den*other.den) # a/b - c/d = (a*d - b*c)/(b*d)
    else
      Fraccion.new(@num - @den*other, @den) # a/b - c = (a - b*c)/b
    end
end
-@() click to toggle source
# File lib/math_expansion/racional.rb, line 75
def -@ # Operación negación
    Fraccion.new(-@num, @den)
end
/(other) click to toggle source
# File lib/math_expansion/racional.rb, line 108
def /(other) # Operación división
    if(other.respond_to? :den and other.respond_to? :num)
      if(other.num == 0)
        Fraccion.null
      else
        Fraccion.new(@num*other.den, @den*other.num) # a/b / c/d = (a*d)/(b*c)
      end
    else
      if(other == 0)
        Fraccion.null
      else
        Fraccion.new(@num, @den*other)
      end
    end
end
<=>(other) click to toggle source

Operadores comparacionales

# File lib/math_expansion/racional.rb, line 131
def <=> (other)
    #raise ArgumentError, 'Argumento no racional' unless other.is_a? Fraccion
  
    # a/b <=> c/d -> (a*d)/(b*d) <=> (c*b)/(d*b) -> a*d <=> c*b
    if(other.respond_to? :den and other.respond_to? :num)
      (@num * other.den) <=> (other.num * @den)
    else
      (@num.to_f / @den.to_f) <=> (other)
    end
end
abs() click to toggle source

Operadores unarios

# File lib/math_expansion/racional.rb, line 57
def abs
    a, b = @num, @den
    if @num < 0
        a = @num * (-1)
    end
    if @den < 0
        b = @den * (-1)
    end
    Fraccion.new(a.to_i,b.to_i)
end
coerce(other) click to toggle source
# File lib/math_expansion/racional.rb, line 34
def coerce(other)
    [Fraccion.new(other,1),self]  
end
den() click to toggle source
# File lib/math_expansion/racional.rb, line 30
def den()
    @den
end
num() click to toggle source
# File lib/math_expansion/racional.rb, line 26
def num()
    @num
end
reciprocal() click to toggle source
# File lib/math_expansion/racional.rb, line 68
def reciprocal
    aux = @num
    @num = @den
    @den = aux
    Fraccion.new(@num,@den)
end
reducir() click to toggle source
# File lib/math_expansion/racional.rb, line 46
def reducir
    mcd = MathExpansion::gcd(@num,@den)
    @num = @num / mcd
    @den = @den / mcd
end
to_f() click to toggle source
# File lib/math_expansion/racional.rb, line 52
def to_f
    @num.to_f/@den.to_f
end
to_s() click to toggle source
# File lib/math_expansion/racional.rb, line 42
def to_s
    "#{@num}/#{@den}"
end