mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Float << and >> should be more compatible to Fixnum
This commit is contained in:
+22
-4
@@ -165,12 +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
|
||||
|
||||
@@ -178,3 +178,25 @@ assert('Float#nan?') do
|
||||
assert_false (1.0/0.0).nan?
|
||||
assert_false (-1.0/0.0).nan?
|
||||
end
|
||||
|
||||
assert('Float#<<') do
|
||||
# Left Shift by one
|
||||
assert_equal 46, 23.0 << 1
|
||||
|
||||
# Left Shift by a negative is Right Shift
|
||||
assert_equal 23, 46.0 << -1
|
||||
end
|
||||
|
||||
assert('Float#>>') do
|
||||
# Right Shift by one
|
||||
assert_equal 23, 46.0 >> 1
|
||||
|
||||
# Right Shift by a negative is Left Shift
|
||||
assert_equal 46, 23.0 >> -1
|
||||
|
||||
# Don't raise on large Right Shift
|
||||
assert_equal 0, 23.0 >> 128
|
||||
|
||||
# Don't raise on large Right Shift
|
||||
assert_equal -1, -23.0 >> 128
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user