Integer << and >> to use Float instead of raising RangeError

parent 74696ffd
......@@ -820,11 +820,13 @@ fix_xor(mrb_state *mrb, mrb_value x)
static mrb_value
lshift(mrb_state *mrb, mrb_int val, mrb_int width)
{
mrb_assert(width >= 0);
mrb_assert(width > 0);
if (width > NUMERIC_SHIFT_WIDTH_MAX) {
mrb_raisef(mrb, E_RANGE_ERROR, "width(%S) > (%S:MRB_INT_BIT-1)",
mrb_fixnum_value(width),
mrb_fixnum_value(NUMERIC_SHIFT_WIDTH_MAX));
mrb_float f = (mrb_float)val;
while (width--) {
f *= 2;
}
return mrb_float_value(mrb, f);
}
return mrb_fixnum_value(val << width);
}
......@@ -832,7 +834,7 @@ lshift(mrb_state *mrb, mrb_int val, mrb_int width)
static mrb_value
rshift(mrb_int val, mrb_int width)
{
mrb_assert(width >= 0);
mrb_assert(width > 0);
if (width >= NUMERIC_SHIFT_WIDTH_MAX) {
if (val < 0) {
return mrb_fixnum_value(-1);
......
......@@ -147,11 +147,6 @@ assert('Integer#<<', '15.2.8.3.12') do
# Left Shift by a negative is Right Shift
assert_equal 23, 46 << -1
# Raise when shift is too large
assert_raise(RangeError) do
2 << 128
end
end
assert('Integer#>>', '15.2.8.3.13') do
......@@ -165,11 +160,6 @@ assert('Integer#>>', '15.2.8.3.13') do
# Don't raise on large Right Shift
assert_equal 0, 23 >> 128
# Raise when shift is too large
assert_raise(RangeError) do
2 >> -128
end
end
assert('Integer#ceil', '15.2.8.3.14') do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment