Class: MathExpansion::Fraccion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/math_expansion/racional.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Fraccion

Métodos principales

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Class Method Details

.nullObject



38
39
40
# File 'lib/math_expansion/racional.rb', line 38

def self.null
    Fraccion.new(0,1)
end

Instance Method Details

#%(other) ⇒ Object

Operación módulo

Raises:

  • (ArgumentError)


124
125
126
127
128
# 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) ⇒ Object

Operación producto



96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object

Operadores aritméticos



80
81
82
83
84
85
86
# 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) ⇒ Object

Operación resta



88
89
90
91
92
93
94
# 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

#-@Object

Operación negación



75
76
77
# File 'lib/math_expansion/racional.rb', line 75

def -@ # Operación negación

    Fraccion.new(-@num, @den)
end

#/(other) ⇒ Object

Operación división



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object

Operadores comparacionales



131
132
133
134
135
136
137
138
139
140
# 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

#absObject

Operadores unarios



57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



34
35
36
# File 'lib/math_expansion/racional.rb', line 34

def coerce(other)
    [Fraccion.new(other,1),self]  
end

#denObject



30
31
32
# File 'lib/math_expansion/racional.rb', line 30

def den()
    @den
end

#numObject



26
27
28
# File 'lib/math_expansion/racional.rb', line 26

def num()
    @num
end

#reciprocalObject



68
69
70
71
72
73
# File 'lib/math_expansion/racional.rb', line 68

def reciprocal
    aux = @num
    @num = @den
    @den = aux
    Fraccion.new(@num,@den)
end

#reducirObject



46
47
48
49
50
# File 'lib/math_expansion/racional.rb', line 46

def reducir
    mcd = MathExpansion::gcd(@num,@den)
    @num = @num / mcd
    @den = @den / mcd
end

#to_fObject



52
53
54
# File 'lib/math_expansion/racional.rb', line 52

def to_f
    @num.to_f/@den.to_f
end

#to_sObject



42
43
44
# File 'lib/math_expansion/racional.rb', line 42

def to_s
    "#{@num}/#{@den}"
end