summaryrefslogtreecommitdiffhomepage
path: root/mrblib/numeric.rb
diff options
context:
space:
mode:
Diffstat (limited to 'mrblib/numeric.rb')
-rw-r--r--mrblib/numeric.rb40
1 files changed, 27 insertions, 13 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb
index 1f44a2c81..6e4c5027f 100644
--- a/mrblib/numeric.rb
+++ b/mrblib/numeric.rb
@@ -48,7 +48,7 @@ module Integral
return to_enum(:downto, num) unless block_given?
i = self.to_i
- while(i >= num)
+ while i >= num
block.call(i)
i -= 1
end
@@ -89,7 +89,7 @@ module Integral
return to_enum(:upto, num) unless block_given?
i = self.to_i
- while(i <= num)
+ while i <= num
block.call(i)
i += 1
end
@@ -100,18 +100,18 @@ module Integral
# Calls the given block from +self+ to +num+
# incremented by +step+ (default 1).
#
- def step(num, step=1, &block)
+ def step(num, step = 1, &block)
raise ArgumentError, "step can't be 0" if step == 0
return to_enum(:step, num, step) unless block_given?
i = if num.kind_of? Float then self.to_f else self end
if step > 0
- while(i <= num)
+ while i <= num
block.call(i)
i += step
end
else
- while(i >= num)
+ while i >= num
block.call(i)
i += step
end
@@ -165,16 +165,30 @@ class Float
# floats should be compatible to integers.
def >> other
n = self.to_i
- other.to_i.times {
- n /= 2
- }
- n
+ other = other.to_i
+ if other < 0
+ n << -other
+ else
+ other.times { n /= 2 }
+ if n.abs < 1
+ if n >= 0
+ 0
+ else
+ -1
+ end
+ else
+ n.to_i
+ end
+ end
end
def << other
n = self.to_i
- other.to_i.times {
- n *= 2
- }
- n.to_i
+ other = other.to_i
+ if other < 0
+ n >> -other
+ else
+ other.times { n *= 2 }
+ n
+ end
end
end