mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-complex: fix division by zero
Fix a division by zero error when dividing a complex number by `Complex(0, 0)`. Co-authored-by: Gemini <gemini@google.com>
This commit is contained in:
@@ -414,9 +414,17 @@ mrb_complex_div(mrb_state *mrb, mrb_value self, mrb_value rhs)
|
||||
mrb_int_zerodiv(mrb);
|
||||
}
|
||||
mrb_float f = mrb_as_float(mrb, rhs);
|
||||
if (f == 0.0) {
|
||||
mrb_int_zerodiv(mrb);
|
||||
}
|
||||
return complex_new(mrb, mrb_div_float(a->real, f), mrb_div_float(a->imaginary, f));
|
||||
}
|
||||
|
||||
b = complex_ptr(mrb, rhs);
|
||||
if (b->real == 0 && b->imaginary == 0) {
|
||||
mrb_int_zerodiv(mrb);
|
||||
}
|
||||
|
||||
struct float_pair ar, ai, br, bi;
|
||||
struct float_pair br2, bi2;
|
||||
struct float_pair div;
|
||||
@@ -424,8 +432,6 @@ mrb_complex_div(mrb_state *mrb, mrb_value self, mrb_value rhs)
|
||||
struct float_pair ai_br, ar_bi;
|
||||
struct float_pair zr, zi;
|
||||
|
||||
b = complex_ptr(mrb, rhs);
|
||||
|
||||
/* Split floating-point components into significand and exponent */
|
||||
ar.s = F(frexp)(a->real, &ar.x);
|
||||
ai.s = F(frexp)(a->imaginary, &ai.x);
|
||||
|
||||
Reference in New Issue
Block a user