mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-rational (rational_pow): fix wrong power calculation
`a/b ** -n` can be calculated by `b/a ** n` for correctness.
This commit is contained in:
@@ -690,6 +690,12 @@ rational_pow(mrb_state *mrb, mrb_value x)
|
||||
if (i == 1) {
|
||||
return x;
|
||||
}
|
||||
if (i < 0) {
|
||||
i = -i;
|
||||
if (i < 0) rat_overflow(mrb);
|
||||
return rational_new_i(mrb, (mrb_int)pow((mrb_float)p1->denominator, (mrb_float)i),
|
||||
(mrb_int)pow((mrb_float)p1->numerator, (mrb_float)i));
|
||||
}
|
||||
return rational_new_i(mrb, (mrb_int)pow((mrb_float)p1->numerator, (mrb_float)i),
|
||||
(mrb_int)pow((mrb_float)p1->denominator, (mrb_float)i));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user