Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/mruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ void mrb_bug(const char *fmt, ...);
#define E_SYNTAX_ERROR (mrb_class_obj_get(mrb, "SyntaxError"))
#define E_LOCALJUMP_ERROR (mrb_class_obj_get(mrb, "LocalJumpError"))
#define E_REGEXP_ERROR (mrb_class_obj_get(mrb, "RegexpError"))
#define E_ZERODIVISION_ERROR (mrb_class_obj_get(mrb, "ZeroDivisionError"))

#define E_NOTIMP_ERROR (mrb_class_obj_get(mrb, "NotImplementedError"))
#define E_FLOATDOMAIN_ERROR (mrb_class_obj_get(mrb, "FloatDomainError"))
Expand Down
2 changes: 2 additions & 0 deletions mrblib/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ class KeyError < IndexError
class NotImplementedError < ScriptError
end

class ZeroDivisionError < StandardError
end
10 changes: 8 additions & 2 deletions src/numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ flodivmod(mrb_state *mrb, mrb_float x, mrb_float y, mrb_float *divp, mrb_float *
{
mrb_float div, mod;

if (y == 0.0)
mrb_raise(mrb, E_ZERODIVISION_ERROR, "divided by 0");
mod = fmod(x, y);
if (isinf(x) && !isinf(y) && !isnan(y))
div = x;
Expand Down Expand Up @@ -278,6 +280,8 @@ flo_mod(mrb_state *mrb, mrb_value x)
mrb_get_args(mrb, "o", &y);

fy = mrb_to_flo(mrb, y);
if (!FIXNUM_P(y) && fy == 0.0)
return mrb_float_value(nan(""));
flodivmod(mrb, mrb_float(x), fy, 0, &mod);
return mrb_float_value(mod);
}
Expand Down Expand Up @@ -730,6 +734,8 @@ fixdivmod(mrb_state *mrb, mrb_int x, mrb_int y, mrb_int *divp, mrb_int *modp)
{
mrb_int div, mod;

if (y == 0)
mrb_raise(mrb, E_ZERODIVISION_ERROR, "divided by 0");
if (y < 0) {
if (x < 0)
div = -x / -y;
Expand Down Expand Up @@ -765,11 +771,11 @@ static mrb_value
fix_mod(mrb_state *mrb, mrb_value x)
{
mrb_value y;
mrb_int a, b;
mrb_int a;

mrb_get_args(mrb, "o", &y);
a = mrb_fixnum(x);
if (FIXNUM_P(y) && (b=mrb_fixnum(y)) != 0) {
if (FIXNUM_P(y)) {
mrb_int mod;

fixdivmod(mrb, a, mrb_fixnum(y), 0, &mod);
Expand Down
22 changes: 21 additions & 1 deletion test/t/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@
b = 1%1.0
c = 2%4

a == 0 and b == 0.0 and c == 2
e2 = nil
begin
1%0
rescue => e1
e2 = e1
end

a == 0 and b == 0.0 and c == 2 and
e2.class == ZeroDivisionError
end

assert('Integer#<=>', '15.2.8.3.6') do
Expand Down Expand Up @@ -175,6 +183,18 @@

# Not ISO specified

assert('Integer#divmod') do
a, b = 3.divmod(2)

begin
1.divmod(0)
rescue
# should not dump core
end

a == 1 and b == 1
end

assert('Integer#step') do
a = []
b = []
Expand Down