Skip to content

Commit be73905

Browse files
committed
accept floats as bit operator operands; fix #3260
1 parent 8112295 commit be73905

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

Diff for: src/numeric.c

+10-20
Original file line numberDiff line numberDiff line change
@@ -809,17 +809,13 @@ fix_rev(mrb_state *mrb, mrb_value num)
809809
return mrb_fixnum_value(~val);
810810
}
811811

812-
static mrb_value
813-
bit_coerce(mrb_state *mrb, mrb_value x)
814-
{
815-
while (!mrb_fixnum_p(x)) {
816-
if (mrb_float_p(x)) {
817-
mrb_raise(mrb, E_TYPE_ERROR, "can't convert Float into Integer");
818-
}
819-
x = mrb_to_int(mrb, x);
820-
}
821-
return x;
822-
}
812+
static mrb_value flo_and(mrb_state *mrb, mrb_value x);
813+
static mrb_value flo_or(mrb_state *mrb, mrb_value x);
814+
static mrb_value flo_xor(mrb_state *mrb, mrb_value x);
815+
#define bit_op(x,y,op1,op2) do {\
816+
if (mrb_fixnum_p(y)) return mrb_fixnum_value(mrb_fixnum(x) op2 mrb_fixnum(y));\
817+
return flo_ ## op1(mrb, mrb_float_value(mrb, mrb_fixnum(x)));\
818+
} while(0)
823819

824820
/* 15.2.8.3.9 */
825821
/*
@@ -835,9 +831,7 @@ fix_and(mrb_state *mrb, mrb_value x)
835831
mrb_value y;
836832

837833
mrb_get_args(mrb, "o", &y);
838-
839-
y = bit_coerce(mrb, y);
840-
return mrb_fixnum_value(mrb_fixnum(x) & mrb_fixnum(y));
834+
bit_op(x, y, and, &);
841835
}
842836

843837
/* 15.2.8.3.10 */
@@ -854,9 +848,7 @@ fix_or(mrb_state *mrb, mrb_value x)
854848
mrb_value y;
855849

856850
mrb_get_args(mrb, "o", &y);
857-
858-
y = bit_coerce(mrb, y);
859-
return mrb_fixnum_value(mrb_fixnum(x) | mrb_fixnum(y));
851+
bit_op(x, y, or, |);
860852
}
861853

862854
/* 15.2.8.3.11 */
@@ -873,9 +865,7 @@ fix_xor(mrb_state *mrb, mrb_value x)
873865
mrb_value y;
874866

875867
mrb_get_args(mrb, "o", &y);
876-
877-
y = bit_coerce(mrb, y);
878-
return mrb_fixnum_value(mrb_fixnum(x) ^ mrb_fixnum(y));
868+
bit_op(x, y, or, ^);
879869
}
880870

881871
#define NUMERIC_SHIFT_WIDTH_MAX (MRB_INT_BIT-1)

0 commit comments

Comments
 (0)