class Kwyjibo::Fraccion

Attributes

denom[RW]
num[RW]

Public Class Methods

new(a, b) click to toggle source
# File lib/kwyjibo.rb, line 196
def initialize(a, b)
    x = mcd(a,b)
    @num = a/x
    @denom = b/x

    if (@num < 0 && @denom < 0)
        @num = @num * -1
        @denom = @denom * -1
    end

    if (@denom < 0)
        @denom = @denom * -1
        @num = @num * -1
    end
end

Public Instance Methods

%(other) click to toggle source
# File lib/kwyjibo.rb, line 264
def %(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        division = Fraccion.new(@num * c.denom, @denom * c.num)
    else
        division = Fraccion.new(@num * other.denom, @denom * other.num)
    end  
    division.num % division.denom
end
*(other) click to toggle source
# File lib/kwyjibo.rb, line 246
def *(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        Fraccion.new(@num * c.num, @denom * c.denom)
    else
        Fraccion.new(@num * other.num, @denom * other.denom)
    end
end
+(other) click to toggle source
# File lib/kwyjibo.rb, line 228
def +(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        Fraccion.new(@num * c.denom + @denom * c.num, @denom * c.denom)
    else
        Fraccion.new(@num * other.denom + @denom * other.num, @denom * other.denom)
    end
end
-(other) click to toggle source
# File lib/kwyjibo.rb, line 237
def -(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        Fraccion.new(@num * c.denom - @denom * c.num, @denom * c.denom)
    else
        Fraccion.new(@num * other.denom - @denom * other.num, @denom * other.denom)
    end
end
-@() click to toggle source
# File lib/kwyjibo.rb, line 285
def -@
    if (@num > 0)
        @num = @num * -1
    end
end
/(other) click to toggle source
# File lib/kwyjibo.rb, line 255
def /(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        Fraccion.new(@num * c.denom, @denom * c.num)
    else
        Fraccion.new(@num * other.denom, @denom * other.num)
    end        
end
<=>(other) click to toggle source
# File lib/kwyjibo.rb, line 291
def <=>(other)
    return nil unless (other.instance_of? Fraccion) || (other.instance_of? Fixnum)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        (c.num.to_f/c.denom.to_f) <=> (self.num.to_f/self.denom.to_f)
    else
        (self.num.to_f/self.denom.to_f) <=> (other.num.to_f/other.denom.to_f)
    end
end
abs() click to toggle source
# File lib/kwyjibo.rb, line 274
def abs
    @num = @num.abs
    @denom = @denom.abs
end
coerce(other) click to toggle source
# File lib/kwyjibo.rb, line 301
def coerce(other)
    [self,other]
end
mcd(u, v) click to toggle source
# File lib/kwyjibo.rb, line 212
def mcd(u, v)
   u, v = u.abs, v.abs
   while v != 0
      u, v = v, u % v
   end
   u
end
reciprocal() click to toggle source
# File lib/kwyjibo.rb, line 279
def reciprocal
    x = @num
    @num = @denom
    @denom = x
end
to_f() click to toggle source
# File lib/kwyjibo.rb, line 224
def to_f
    @num.to_f/@denom.to_f
end
to_s() click to toggle source
# File lib/kwyjibo.rb, line 220
def to_s
    "#{@num}/#{@denom}"
end