Class: NumerosRacionales
- Inherits:
-
Object
- Object
- NumerosRacionales
- Defined in:
- lib/racionales.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
readonly
Returns the value of attribute a.
-
#b ⇒ Object
readonly
Returns the value of attribute b.
Instance Method Summary collapse
-
#%(other) ⇒ Object
Halla el modulo de una fraccion.
-
#*(other) ⇒ Object
Calcula la multiplicaciones de dos racionales.
-
#+(other) ⇒ Object
Calcula la suma de racionales o racionales con enteros.
-
#-(other) ⇒ Object
Calcula la resta de racionales o racionales con enteros.
-
#-@ ⇒ Object
Calcula el opuesto de un racional.
-
#/(other) ⇒ Object
Calcula la division de dos racionales.
-
#<(other) ⇒ Object
Compara si un numero racional es menor que otro.
-
#<=(other) ⇒ Object
Compara si un numero racional es menor o igual que otro.
-
#<=>(other) ⇒ Object
Compara si un numero racional distinto otro.
-
#==(other) ⇒ Object
Compara si dos numeros racionales son iguales.
-
#>(other) ⇒ Object
Compara si un numero racional es mayor que otro.
-
#>=(other) ⇒ Object
Compara si un numero racional es mayor o igual que otro.
-
#abs ⇒ Object
Calcula el valor absoluto de un racional.
-
#coerce(other) ⇒ Object
Hace una conversion de enteros a racionales.
-
#denom ⇒ Object
Devuelve el valor del denominador.
-
#initialize(a, b) ⇒ NumerosRacionales
constructor
Crea un numero racional con numerador a y denominador b.
-
#num ⇒ Object
Devuelve el valor del numerador.
-
#reciprocal ⇒ Object
Calcula el inverso de un racional.
-
#simplificar ⇒ Object
Simplifica el numero racional.
-
#to_f ⇒ Object
Pasa a flotante un numero racional.
-
#to_s ⇒ Object
Muestra por pantalla el racional.
Constructor Details
#initialize(a, b) ⇒ NumerosRacionales
Crea un numero racional con numerador a y denominador b
6 7 8 9 10 11 12 13 |
# File 'lib/racionales.rb', line 6 def initialize (a, b) # Crea un numero racional con numerador a y denominador b raise ZeroDivisionError, "Denominador igual a cero" if (b==0) if (a<0) and (b<0) #si los dos son negativos => nos queda positivo a,b= -a,-b end simplifica = gcd(a,b) @a,@b = a/simplifica,b/simplifica end |
Instance Attribute Details
#a ⇒ Object (readonly)
Returns the value of attribute a.
4 5 6 |
# File 'lib/racionales.rb', line 4 def a @a end |
#b ⇒ Object (readonly)
Returns the value of attribute b.
4 5 6 |
# File 'lib/racionales.rb', line 4 def b @b end |
Instance Method Details
#%(other) ⇒ Object
Halla el modulo de una fraccion
100 101 102 103 104 105 |
# File 'lib/racionales.rb', line 100 def %(other) # Halla el modulo de una fraccion tmp = (self/other).abs t = NumerosRacionales.new(1,1) tmp= t-tmp NumerosRacionales.new(tmp.num, tmp.denom) end |
#*(other) ⇒ Object
Calcula la multiplicaciones de dos racionales
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/racionales.rb', line 74 def *(other) # Calcula la multiplicaciones de dos racionales if (other.is_a?(Numeric)) other=self.coerce(other) other=other[0] end num = @a*other.a deno = @b*other.b simplifica = gcd(num,deno) #averiguamos el valor para obtener el racional irreducible if ((num < 0) and (deno < 0)) or ((num > 0) and (deno < 0)) #corregimos el signo num = num * (-1) deno = deno * (-1) end NumerosRacionales.new(num/simplifica,deno/simplifica) end |
#+(other) ⇒ Object
Calcula la suma de racionales o racionales con enteros
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/racionales.rb', line 44 def +(other) # Calcula la suma de racionales o racionales con enteros if (other.is_a?(Integer)) # other=self.coerce(other) other=NumerosRacionales.new(other,1) end deno = mcm(@b, other.b) #MCM para hallar el denominador num = ((deno/@b) * @a) + ((deno/other.b) * other.a) simplifica = gcd(num,deno) #averiguamos el valor para obtener el racional irreducible if ((num < 0) and (deno < 0)) or ((num > 0) and (deno < 0)) #corregimos el signo num = num * (-1) deno = deno * (-1) end NumerosRacionales.new(num/simplifica,deno/simplifica) end |
#-(other) ⇒ Object
Calcula la resta de racionales o racionales con enteros
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/racionales.rb', line 59 def -(other) # Calcula la resta de racionales o racionales con enteros if (other.is_a?(Numeric)) other=self.coerce(other) other=other[0] end deno = mcm(@b, other.b) #MCM para hallar el denominador num = ((deno/@b) * @a) - ((deno/other.b) * other.a) simplifica = gcd(num,deno) #averiguamos el valor para obtener el racional irreducible if ((num < 0) and (deno < 0)) or ((num > 0) and (deno < 0)) #corregimos el signo num = num * (-1) deno = deno * (-1) end NumerosRacionales.new(num/simplifica,deno/simplifica) end |
#-@ ⇒ Object
Calcula el opuesto de un racional
168 169 170 |
# File 'lib/racionales.rb', line 168 def -@ # Calcula el opuesto de un racional NumerosRacionales.new(-self.num,self.denom) end |
#/(other) ⇒ Object
Calcula la division de dos racionales
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/racionales.rb', line 89 def /(other) # Calcula la division de dos racionales num = @a*other.b deno = @b*other.a simplifica = gcd(num,deno) #averiguamos el valor para obtener el racional irreducible if ((num < 0) and (deno < 0)) or ((num > 0) and (deno < 0)) #corregimos el signo num = num * (-1) deno = deno * (-1) end NumerosRacionales.new(num/simplifica,deno/simplifica) end |
#<(other) ⇒ Object
Compara si un numero racional es menor que otro
121 122 123 124 125 126 127 |
# File 'lib/racionales.rb', line 121 def <(other) # Compara si un numero racional es menor que otro if (self.to_f < other.to_f) true else false end end |
#<=(other) ⇒ Object
Compara si un numero racional es menor o igual que otro
129 130 131 132 133 134 135 |
# File 'lib/racionales.rb', line 129 def <=(other) # Compara si un numero racional es menor o igual que otro if (self.to_f <= other.to_f) true else false end end |
#<=>(other) ⇒ Object
Compara si un numero racional distinto otro
153 154 155 |
# File 'lib/racionales.rb', line 153 def <=>(other) # Compara si un numero racional distinto otro self.to_f <=> other.to_f end |
#==(other) ⇒ Object
Compara si dos numeros racionales son iguales
113 114 115 116 117 118 119 |
# File 'lib/racionales.rb', line 113 def ==(other) # Compara si dos numeros racionales son iguales if (self.to_f == other.to_f) true else false end end |
#>(other) ⇒ Object
Compara si un numero racional es mayor que otro
137 138 139 140 141 142 143 |
# File 'lib/racionales.rb', line 137 def >(other) # Compara si un numero racional es mayor que otro if (self.to_f > other.to_f) true else false end end |
#>=(other) ⇒ Object
Compara si un numero racional es mayor o igual que otro
145 146 147 148 149 150 151 |
# File 'lib/racionales.rb', line 145 def >=(other) # Compara si un numero racional es mayor o igual que otro if (self.to_f >= other.to_f) true else false end end |
#abs ⇒ Object
Calcula el valor absoluto de un racional
157 158 159 160 |
# File 'lib/racionales.rb', line 157 def abs # Calcula el valor absoluto de un racional n, d = @a.abs, @b.abs NumerosRacionales.new(n,d) end |
#coerce(other) ⇒ Object
Hace una conversion de enteros a racionales
107 108 109 110 111 |
# File 'lib/racionales.rb', line 107 def coerce(other) # Hace una conversion de enteros a racionales if (other.is_a?(Integer)) [NumerosRacionales.new(other.to_i,1),self] end end |
#denom ⇒ Object
Devuelve el valor del denominador
19 20 21 |
# File 'lib/racionales.rb', line 19 def denom()# Devuelve el valor del denominador @b end |
#num ⇒ Object
Devuelve el valor del numerador
15 16 17 |
# File 'lib/racionales.rb', line 15 def num() # Devuelve el valor del numerador @a end |
#reciprocal ⇒ Object
Calcula el inverso de un racional
162 163 164 165 166 |
# File 'lib/racionales.rb', line 162 def reciprocal # Calcula el inverso de un racional inv = NumerosRacionales.new(1,1) tmp = inv/self NumerosRacionales.new(tmp.num,tmp.denom) end |
#simplificar ⇒ Object
Simplifica el numero racional
39 40 41 42 |
# File 'lib/racionales.rb', line 39 def simplificar # Simplifica el numero racional simplifica = gcd(@a,@b) NumerosRacionales.new(@a/simplifica,@b/simplifica) end |
#to_f ⇒ Object
Pasa a flotante un numero racional
33 34 35 36 |
# File 'lib/racionales.rb', line 33 def to_f # Pasa a flotante un numero racional tmp=@a.to_f/@b.to_f return tmp end |
#to_s ⇒ Object
Muestra por pantalla el racional
23 24 25 26 27 28 29 30 31 |
# File 'lib/racionales.rb', line 23 def to_s # Muestra por pantalla el racional if (@a == 0) #numerador = 0, racional = 0. El denominador lo controlamos en el constructor "0" elsif (@b==1) "#{@a}" else "#{@a}/#{@b}" end end |