• mathn.rb

Quicksearch

# Rational

Unify

### Public Instance Methods

**(other) click to toggle source
```
# File mathn.rb, line 126
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
```
Also aliased as: power!
inspect() click to toggle source
```
# File mathn.rb, line 120
def inspect
format "%s/%s", numerator.inspect, denominator.inspect
end
```
power!(other) click to toggle source
Alias for: **
power2(other) click to toggle source
```
# File mathn.rb, line 187
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
```

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.