From ec456e5b1878ec2ef27ad479dc16e661f9483737 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 24 Mar 2021 10:42:32 +0900 Subject: Use `mrb_num_div_flo` for float division. This function handles zero division properly. Also fixed bugs that multiply numbers instead of division. --- mrbgems/mruby-complex/src/complex.c | 6 ++++-- mrbgems/mruby-rational/src/rational.c | 10 +++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c index 11ca2bb95..c0af1904b 100644 --- a/mrbgems/mruby-complex/src/complex.c +++ b/mrbgems/mruby-complex/src/complex.c @@ -285,6 +285,8 @@ complex_div(mrb_state *mrb, mrb_value self) return complex_new(mrb, F(ldexp)(zr.s, zr.x), F(ldexp)(zi.s, zi.x)); } +mrb_float mrb_num_div_flo(mrb_state*, mrb_float, mrb_float); + #ifndef MRB_USE_RATIONAL mrb_int mrb_num_div_int(mrb_state *mrb, mrb_int x, mrb_int y); @@ -307,7 +309,7 @@ int_div(mrb_state *mrb, mrb_value x) x = complex_new(mrb, (mrb_float)a, 0); return mrb_funcall_id(mrb, x, MRB_OPSYM(div), 1, y); default: - return mrb_float_value(mrb, (mrb_float)a * mrb_to_flo(mrb, y)); + return mrb_float_value(mrb, mrb_num_div_flo(mrb, (mrb_float)a, mrb_to_flo(mrb, y))); } } @@ -327,7 +329,7 @@ int_quo(mrb_state *mrb, mrb_value x) x = complex_new(mrb, (mrb_float)a, 0); return mrb_funcall_id(mrb, x, MRB_OPSYM(div), 1, y); default: - return mrb_float_value(mrb, (mrb_float)a * mrb_to_flo(mrb, y)); + return mrb_float_value(mrb, mrb_num_div_flo(mrb, (mrb_float)a, mrb_to_flo(mrb, y))); } } #endif diff --git a/mrbgems/mruby-rational/src/rational.c b/mrbgems/mruby-rational/src/rational.c index 497a36d8e..be9b5eaf9 100644 --- a/mrbgems/mruby-rational/src/rational.c +++ b/mrbgems/mruby-rational/src/rational.c @@ -406,7 +406,11 @@ rational_minus(mrb_state *mrb, mrb_value x) return rational_new(mrb, -p->numerator, p->denominator); } -mrb_int mrb_num_div_int(mrb_state *, mrb_int, mrb_int); +mrb_int mrb_num_div_int(mrb_state*, mrb_int, mrb_int); +mrb_value mrb_complex_new(mrb_state*, mrb_float, mrb_float); +#ifndef MRB_NO_FLOAT +mrb_float mrb_num_div_flo(mrb_state*, mrb_float, mrb_float); +#endif /* 15.2.8.3.4 */ /* @@ -436,7 +440,7 @@ int_div(mrb_state *mrb, mrb_value x) #ifdef MRB_NO_FLOAT mrb_raise(mrb, E_TYPE_ERROR, "non integer multiplication"); #else - return mrb_float_value(mrb, (mrb_float)a * mrb_to_flo(mrb, y)); + return mrb_float_value(mrb, mrb_num_div_flo(mrb, (mrb_float)a, mrb_to_flo(mrb, y))); #endif } } @@ -468,7 +472,7 @@ int_quo(mrb_state *mrb, mrb_value x) #ifdef MRB_NO_FLOAT mrb_raise(mrb, E_TYPE_ERROR, "non integer multiplication"); #else - return mrb_float_value(mrb, (mrb_float)a * mrb_to_flo(mrb, y)); + return mrb_float_value(mrb, mrb_num_div_flo(mrb, (mrb_float)a, mrb_to_flo(mrb, y))); #endif } } -- cgit v1.2.3