new_operators 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/new_operators/version.rb +1 -1
- data/lib/new_operators.rb +10 -4
- metadata +1 -1
data/lib/new_operators.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
class Fixnum
|
2
2
|
def ^(x)
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
return 'no negative exponents' if x < 0
|
4
|
+
return 'no negative numbers' if self < 0
|
5
|
+
product, remainder = self, nil
|
6
|
+
return 'undefined' if self == 0 && x == 0
|
7
|
+
return 1 if x == 0
|
8
|
+
return self if x == 1
|
9
|
+
(x % 2 == 0) ? (remainder = 1) : (remainder = self; x = x - 1)
|
10
|
+
(x-1).times do
|
11
|
+
product = product * self
|
6
12
|
end
|
7
|
-
|
13
|
+
return product * remainder
|
8
14
|
end
|
9
15
|
end
|