73 |
73 |
|
74 |
74 |
class Rational
|
75 |
75 |
remove_method :**
|
|
76 |
|
|
77 |
##
|
|
78 |
# exponentiate by +other+
|
76 |
79 |
def ** (other)
|
77 |
80 |
if other.kind_of?(Rational)
|
78 |
81 |
other2 = other
|
79 |
82 |
if self < 0
|
80 |
|
return Complex(self, 0.0) ** other
|
|
83 |
return Complex(self, 0.0) ** other
|
81 |
84 |
elsif other == 0
|
82 |
|
return Rational(1,1)
|
|
85 |
return Rational(1,1)
|
83 |
86 |
elsif self == 0
|
84 |
|
return Rational(0,1)
|
|
87 |
return Rational(0,1)
|
85 |
88 |
elsif self == 1
|
86 |
|
return Rational(1,1)
|
|
89 |
return Rational(1,1)
|
87 |
90 |
end
|
88 |
91 |
|
89 |
92 |
npd = numerator.prime_division
|
90 |
93 |
dpd = denominator.prime_division
|
91 |
94 |
if other < 0
|
92 |
|
other = -other
|
93 |
|
npd, dpd = dpd, npd
|
|
95 |
other = -other
|
|
96 |
npd, dpd = dpd, npd
|
94 |
97 |
end
|
95 |
98 |
|
96 |
99 |
for elm in npd
|
97 |
|
elm[1] = elm[1] * other
|
98 |
|
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
|
99 |
|
return Float(self) ** other2
|
100 |
|
end
|
101 |
|
elm[1] = elm[1].to_i
|
|
100 |
elm[1] = elm[1] * other
|
|
101 |
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
|
|
102 |
return Float(self) ** other2
|
|
103 |
end
|
|
104 |
elm[1] = elm[1].to_i
|
102 |
105 |
end
|
103 |
106 |
|
104 |
107 |
for elm in dpd
|
105 |
|
elm[1] = elm[1] * other
|
106 |
|
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
|
107 |
|
return Float(self) ** other2
|
108 |
|
end
|
109 |
|
elm[1] = elm[1].to_i
|
|
108 |
elm[1] = elm[1] * other
|
|
109 |
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
|
|
110 |
return Float(self) ** other2
|
|
111 |
end
|
|
112 |
elm[1] = elm[1].to_i
|
110 |
113 |
end
|
111 |
114 |
|
112 |
115 |
num = Integer.from_prime_division(npd)
|
... | ... | |
116 |
119 |
|
117 |
120 |
elsif other.kind_of?(Integer)
|
118 |
121 |
if other > 0
|
119 |
|
num = numerator ** other
|
120 |
|
den = denominator ** other
|
|
122 |
num = numerator ** other
|
|
123 |
den = denominator ** other
|
121 |
124 |
elsif other < 0
|
122 |
|
num = denominator ** -other
|
123 |
|
den = numerator ** -other
|
|
125 |
num = denominator ** -other
|
|
126 |
den = numerator ** -other
|
124 |
127 |
elsif other == 0
|
125 |
|
num = 1
|
126 |
|
den = 1
|
|
128 |
num = 1
|
|
129 |
den = 1
|
127 |
130 |
end
|
128 |
131 |
Rational(num, den)
|
129 |
132 |
elsif other.kind_of?(Float)
|
... | ... | |
144 |
147 |
if a.kind_of?(Complex)
|
145 |
148 |
abs = sqrt(a.real*a.real + a.imag*a.imag)
|
146 |
149 |
# if not abs.kind_of?(Rational)
|
147 |
|
# return a**Rational(1,2)
|
|
150 |
# return a**Rational(1,2)
|
148 |
151 |
# end
|
149 |
152 |
x = sqrt((a.real + abs)/Rational(2))
|
150 |
153 |
y = sqrt((-a.real + abs)/Rational(2))
|
151 |
154 |
# if !(x.kind_of?(Rational) and y.kind_of?(Rational))
|
152 |
|
# return a**Rational(1,2)
|
|
155 |
# return a**Rational(1,2)
|
153 |
156 |
# end
|
154 |
157 |
if a.imag >= 0
|
155 |
|
Complex(x, y)
|
|
158 |
Complex(x, y)
|
156 |
159 |
else
|
157 |
|
Complex(x, -y)
|
|
160 |
Complex(x, -y)
|
158 |
161 |
end
|
159 |
162 |
elsif a.respond_to?(:nan?) and a.nan?
|
160 |
163 |
a
|
... | ... | |
176 |
179 |
byte_a = [src & 0xffffffff]
|
177 |
180 |
# ruby's bug
|
178 |
181 |
while (src >= max) and (src >>= 32)
|
179 |
|
byte_a.unshift src & 0xffffffff
|
|
182 |
byte_a.unshift src & 0xffffffff
|
180 |
183 |
end
|
181 |
184 |
|
182 |
185 |
answer = 0
|
183 |
186 |
main = 0
|
184 |
187 |
side = 0
|
185 |
188 |
for elm in byte_a
|
186 |
|
main = (main << 32) + elm
|
187 |
|
side <<= 16
|
188 |
|
if answer != 0
|
189 |
|
if main * 4 < side * side
|
190 |
|
applo = main.div(side)
|
191 |
|
else
|
192 |
|
applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
|
193 |
|
end
|
194 |
|
else
|
195 |
|
applo = sqrt!(main).to_i + 1
|
196 |
|
end
|
197 |
|
|
198 |
|
while (x = (side + applo) * applo) > main
|
199 |
|
applo -= 1
|
200 |
|
end
|
201 |
|
main -= x
|
202 |
|
answer = (answer << 16) + applo
|
203 |
|
side += applo * 2
|
|
189 |
main = (main << 32) + elm
|
|
190 |
side <<= 16
|
|
191 |
if answer != 0
|
|
192 |
if main * 4 < side * side
|
|
193 |
applo = main.div(side)
|
|
194 |
else
|
|
195 |
applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
|
|
196 |
end
|
|
197 |
else
|
|
198 |
applo = sqrt!(main).to_i + 1
|
|
199 |
end
|
|
200 |
|
|
201 |
while (x = (side + applo) * applo) > main
|
|
202 |
applo -= 1
|
|
203 |
end
|
|
204 |
main -= x
|
|
205 |
answer = (answer << 16) + applo
|
|
206 |
side += applo * 2
|
204 |
207 |
end
|
205 |
208 |
if main == 0
|
206 |
|
answer
|
|
209 |
answer
|
207 |
210 |
else
|
208 |
|
sqrt!(a)
|
|
211 |
sqrt!(a)
|
209 |
212 |
end
|
210 |
213 |
end
|
211 |
214 |
end
|
212 |
|
-
|