Class: Fraccion
- Inherits:
-
Object
- Object
- Fraccion
- Includes:
- Comparable
- Defined in:
- lib/matrix_expansion/fraccion.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
readonly
a: numerador, b: denominador.
-
#b ⇒ Object
readonly
a: numerador, b: denominador.
Instance Method Summary collapse
-
#%(other) ⇒ Object
Resto de fracciones.
-
#*(other) ⇒ Object
Producto de fracciones.
-
#+(other) ⇒ Object
Suma de fracciones.
-
#-(other) ⇒ Object
Resta de fracciones.
-
#-@ ⇒ Object
Opuesto de fracción.
-
#/(other) ⇒ Object
División de fracciones.
- #<=>(other) ⇒ Object
-
#abs ⇒ Object
Fracción pasada a float.
- #coerce(other) ⇒ Object
-
#denom ⇒ Object
metedo que retorna el denominador.
-
#initialize(a, b) ⇒ Fraccion
constructor
A new instance of Fraccion.
-
#num ⇒ Object
metodo que retorna el numerador.
-
#reciprocal ⇒ Object
Fracción reciproca o inversa (a/b -> b/a).
-
#to_f ⇒ Object
Fraccion en modo flotante.
-
#to_s ⇒ Object
formateo de la salida (a/b).
Constructor Details
#initialize(a, b) ⇒ Fraccion
Returns a new instance of Fraccion.
6 7 8 9 10 11 12 13 14 |
# File 'lib/matrix_expansion/fraccion.rb', line 6 def initialize(a,b) #comprobamos que los datos de entradas sean numericos raise unless a.is_a?(Numeric) raise unless b.is_a?(Numeric) #comprobamos que el dominador no sea cero raise unless (b != 0) #inicializamos las variables @a, @b = a, b end |
Instance Attribute Details
#a ⇒ Object (readonly)
a: numerador, b: denominador
5 6 7 |
# File 'lib/matrix_expansion/fraccion.rb', line 5 def a @a end |
#b ⇒ Object (readonly)
a: numerador, b: denominador
5 6 7 |
# File 'lib/matrix_expansion/fraccion.rb', line 5 def b @b end |
Instance Method Details
#%(other) ⇒ Object
Resto de fracciones
100 101 102 103 104 105 106 107 |
# File 'lib/matrix_expansion/fraccion.rb', line 100 def % (other) r = Fraccion.new(0,1) #Inicializa objeto r = self while (r.to_f >= other.to_f) do #Hasta que no se pueda restar más. Queda en r el resto. r = r - other end return r end |
#*(other) ⇒ Object
Producto de fracciones
80 81 82 83 84 85 86 87 |
# File 'lib/matrix_expansion/fraccion.rb', line 80 def * (other) a = @a * other.num b = @b * other.denom k = gcd(a,b) a /= k b /= k Fraccion.new(a,b) end |
#+(other) ⇒ Object
Suma de fracciones
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/matrix_expansion/fraccion.rb', line 44 def + (other) comun = @b * other.denom a1 = @a * other.denom a2 = other.num * @b a3 = a1 + a2 k = gcd(a3,comun) a3 /= k comun /= k Fraccion.new(a3,comun) end |
#-(other) ⇒ Object
Resta de fracciones
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/matrix_expansion/fraccion.rb', line 56 def - (other) comun = @b * other.denom a1 = @a * other.denom a2 = other.num * @b a3 = a1 - a2 k = gcd(a3,comun) a3 /= k comun /= k Fraccion.new(a3,comun) end |
#-@ ⇒ Object
Opuesto de fracción. La suma de una fracción y su opuesto debe ser 0.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/matrix_expansion/fraccion.rb', line 68 def -@ k = gcd(@a, @b) a = @a / k b = @b / k if (b < 0) #Si el denominador es negativo return Fraccion.new(a,-b) else return Fraccion.new(-a,b) end end |
#/(other) ⇒ Object
División de fracciones
90 91 92 93 94 95 96 97 |
# File 'lib/matrix_expansion/fraccion.rb', line 90 def / (other) a = @a * other.denom b = @b * other.num k = gcd(a,b) a /= k b /= k Fraccion.new(a,b) end |
#<=>(other) ⇒ Object
110 111 112 |
# File 'lib/matrix_expansion/fraccion.rb', line 110 def <=> (other) self.to_f <=> other.to_f end |
#abs ⇒ Object
Fracción pasada a float. (ej: 1.25)
34 35 36 |
# File 'lib/matrix_expansion/fraccion.rb', line 34 def abs f= Fraccion.new(@a.abs, @b.abs) end |
#coerce(other) ⇒ Object
114 115 116 |
# File 'lib/matrix_expansion/fraccion.rb', line 114 def coerce (other) [Fraccion.new(other,1),self] end |
#denom ⇒ Object
metedo que retorna el denominador
25 26 27 |
# File 'lib/matrix_expansion/fraccion.rb', line 25 def denom return @b end |
#num ⇒ Object
metodo que retorna el numerador
21 22 23 |
# File 'lib/matrix_expansion/fraccion.rb', line 21 def num return @a end |
#reciprocal ⇒ Object
Fracción reciproca o inversa (a/b -> b/a)
39 40 41 |
# File 'lib/matrix_expansion/fraccion.rb', line 39 def reciprocal f= Fraccion.new(@b, @a) end |
#to_f ⇒ Object
Fraccion en modo flotante
29 30 31 |
# File 'lib/matrix_expansion/fraccion.rb', line 29 def to_f @a.to_f / @b.to_f #conversion de tipo a flotante mediante to_f end |
#to_s ⇒ Object
formateo de la salida (a/b)
16 17 18 |
# File 'lib/matrix_expansion/fraccion.rb', line 16 def to_s "#{@a}/#{@b}" end |