Class: Rational

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysl/mathn/mathn.rb

Constant Summary collapse

Unify =
true

Instance Method Summary collapse

Instance Method Details

#**(other) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rubysl/mathn/mathn.rb', line 131

def ** (other)
  if other.kind_of?(Rational)
    other2 = other
    if self < 0
      return Complex.new!(self, 0) ** other
    elsif other == 0
      return Rational(1,1)
    elsif self == 0
      return Rational(0,1)
    elsif self == 1
      return Rational(1,1)
    end

    npd = numerator.prime_division
    dpd = denominator.prime_division
    if other < 0
      other = -other
      npd, dpd = dpd, npd
    end

    for elm in npd
      elm[1] = elm[1] * other
      if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
        return Float(self) ** other2
      end
      elm[1] = elm[1].to_i
    end

    for elm in dpd
      elm[1] = elm[1] * other
      if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
        return Float(self) ** other2
      end
      elm[1] = elm[1].to_i
    end

    num = Integer.from_prime_division(npd)
    den = Integer.from_prime_division(dpd)

    Rational(num,den)

  elsif other.kind_of?(Integer)
    if other > 0
      num = numerator ** other
      den = denominator ** other
    elsif other < 0
      num = denominator ** -other
      den = numerator ** -other
    elsif other == 0
      num = 1
      den = 1
    end
    Rational.new!(num, den)
  elsif other.kind_of?(Float)
    Float(self) ** other
  else
    x , y = other.coerce(self)
    x ** y
  end
end

#inspectObject



125
126
127
# File 'lib/rubysl/mathn/mathn.rb', line 125

def inspect
  format "%s/%s", numerator.inspect, denominator.inspect
end

#power!Object



129
# File 'lib/rubysl/mathn/mathn.rb', line 129

alias power! **

#power2(other) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rubysl/mathn/mathn.rb', line 192

def power2(other)
  if other.kind_of?(Rational)
    if self < 0
      return Complex(self, 0) ** other
    elsif other == 0
      return Rational(1,1)
    elsif self == 0
      return Rational(0,1)
    elsif self == 1
      return Rational(1,1)
    end

    dem = nil
    x = self.denominator.to_f.to_i
    neard = self.denominator.to_f ** (1.0/other.denominator.to_f)
    loop do
      if (neard**other.denominator == self.denominator)
        dem = neaed
        break
      end
    end
    nearn = self.numerator.to_f ** (1.0/other.denominator.to_f)
    Rational(num,den)

  elsif other.kind_of?(Integer)
    if other > 0
      num = numerator ** other
      den = denominator ** other
    elsif other < 0
      num = denominator ** -other
      den = numerator ** -other
    elsif other == 0
      num = 1
      den = 1
    end
    Rational.new!(num, den)
  elsif other.kind_of?(Float)
    Float(self) ** other
  else
    x , y = other.coerce(self)
    x ** y
  end
end