Class: Fraccion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/matrices/fraccion.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y = 1) ⇒ Fraccion

Returns a new instance of Fraccion.



7
8
9
10
11
12
13
14
15
16
# File 'lib/matrices/fraccion.rb', line 7

def initialize(x,y=1)
  m=mcd(x,y)
  
  # Si el denominador tiene signo negativo, se pasa el signo al numerador
  if(y < 0)
    x, y = -x, -y
  end
  
  @n,@d = x/m,y/m
end

Instance Attribute Details

#dObject

Returns the value of attribute d.



5
6
7
# File 'lib/matrices/fraccion.rb', line 5

def d
  @d
end

#nObject

Returns the value of attribute n.



5
6
7
# File 'lib/matrices/fraccion.rb', line 5

def n
  @n
end

Instance Method Details

#%(other) ⇒ Object

Modulo de fracciones



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/matrices/fraccion.rb', line 111

def %(other)
  
  # Se realiza la division, y al cociente se le quita la parte entera, multiplicandola por el divisor para obtener el resto
  div=self.abs/other.abs
  resto=other.abs * (div - Fraccion.new(div.to_f.to_i))
  # Si el dividendo es negativo, el resto sera negativo
  if (@n < 0)
    return -resto
  end
  resto  
end

#*(other) ⇒ Object

Producto de fracciones



91
92
93
94
95
96
97
98
# File 'lib/matrices/fraccion.rb', line 91

def *(other)
  if (other.is_a? Numeric)
    other = Fraccion.new(other)
  end
  num, den = @n * other.n, @d * other.d
  mx = mcd(num, den)
  Fraccion.new(num / mx, den / mx)
end

#+(other) ⇒ Object

Suma de fracciones



67
68
69
70
71
72
73
74
75
76
# File 'lib/matrices/fraccion.rb', line 67

def +(other)
  if (other.is_a? Numeric)
    other = Fraccion.new(other)
  end
  # Se aplica el algoritmo de euclides para obtener el mcm (nuevo denominador), a partir del mcd
  den = (@d * other.d) / mcd(@d, other.d)
  num = ((@n * den) / @d) + ((other.n * den) / other.d)
  mx = mcd(num, den)
  Fraccion.new(num / mx, den / mx)
end

#-(other) ⇒ Object

Resta de fracciones



79
80
81
82
83
84
85
86
87
88
# File 'lib/matrices/fraccion.rb', line 79

def -(other)
  if (other.is_a? Numeric)
    other = Fraccion.new(other)
  end
  # Se aplica el algoritmo de euclides para obtener el mcm (nuevo denominador), a partir del mcd
  den = (@d * other.d) / mcd(@d, other.d)
  num = ((@n * den) / @d) - ((other.n * den) / other.d)
  mx = mcd(num, den)
  Fraccion.new(num / mx, den / mx)
end

#-@Object

Sobrecarga del operador “-” unario



62
63
64
# File 'lib/matrices/fraccion.rb', line 62

def -@
  Fraccion.new((-1)*@n,@d)
end

#/(other) ⇒ Object

Division de fracciones



101
102
103
104
105
106
107
108
# File 'lib/matrices/fraccion.rb', line 101

def /(other)
  if (other.is_a? Numeric)
    other = Fraccion.new(other)
  end
  num, den = @n * other.d, @d * other.n
  mx = mcd(num, den)
  Fraccion.new(num / mx, den / mx)
end

#<=>(other) ⇒ Object

Operador <=>

Raises:

  • (TypeError)


124
125
126
127
# File 'lib/matrices/fraccion.rb', line 124

def <=>(other)   
  raise TypeError, 'Objeto no valido' unless other.respond_to? :to_f
    self.to_f <=> other.to_f
end

#absObject

Valor absoluto



39
40
41
42
43
44
45
46
47
48
# File 'lib/matrices/fraccion.rb', line 39

def abs()
  
  # Si la fraccion es negativa, se devuelve tras cambiarla de signo
  if(@n < 0)
    return Fraccion.new((-1)*@n, @d)
  end
  
  # Si es positiva, se devuelve
  self
end

#coerce(other) ⇒ Object

Coerce



135
136
137
# File 'lib/matrices/fraccion.rb', line 135

def coerce(other)
  [self,other]
end

#denomObject

Metodo que devuelve el denominador



24
25
26
# File 'lib/matrices/fraccion.rb', line 24

def denom()
  @d
end

#elemento_nuloObject

Elemento_nulo



130
131
132
# File 'lib/matrices/fraccion.rb', line 130

def elemento_nulo
  Fraccion.new(0)
end

#numObject

Metodo que devuelve el numerador



19
20
21
# File 'lib/matrices/fraccion.rb', line 19

def num()
  @n
end

#reciprocalObject

Calculo del reciproco



51
52
53
54
55
56
57
58
59
# File 'lib/matrices/fraccion.rb', line 51

def reciprocal()
  
  # Se intercambia el numerador y el denominador
  # Si el signo es negativo, se pasa al nuevo numerador
  if(@n != @n.abs)
    return Fraccion.new(@d*(-1), (-1)*@n)
  end
  Fraccion.new(@d, @n)
end

#to_fObject

Conversion a flotante



34
35
36
# File 'lib/matrices/fraccion.rb', line 34

def to_f()
  (@n.to_f()/@d.to_f())
end

#to_sObject

Conversion a string



29
30
31
# File 'lib/matrices/fraccion.rb', line 29

def to_s()
  "#@n/#@d"
end