mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
numeric.c: fix bigint comparison precision loss
when comparing bigint values with <=> operator, the comparison would convert both operands to float, losing precision for values > 2^53. this caused incorrect results like (10^20+1) <=> (10^20+2) returning 0 instead of -1. add direct bigint comparison paths in cmpnum() to avoid float conversion when both operands can be handled by mrb_bint_cmp(). Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -2093,8 +2093,21 @@ cmpnum(mrb_state *mrb, mrb_value v1, mrb_value v2)
|
||||
else if (x < y) return -1;
|
||||
return 0;
|
||||
}
|
||||
#ifdef MRB_USE_BIGINT
|
||||
if (mrb_bigint_p(v2)) {
|
||||
return -mrb_bint_cmp(mrb, v2, v1);
|
||||
}
|
||||
#endif
|
||||
x = (mrb_float)mrb_integer(v1);
|
||||
}
|
||||
#ifdef MRB_USE_BIGINT
|
||||
else if (mrb_bigint_p(v1)) {
|
||||
if (mrb_integer_p(v2) || mrb_bigint_p(v2)) {
|
||||
return mrb_bint_cmp(mrb, v1, v2);
|
||||
}
|
||||
x = mrb_as_float(mrb, v1);
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
x = mrb_as_float(mrb, v1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user