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:
Yukihiro "Matz" Matsumoto
2026-01-12 09:17:24 +09:00
parent ece641c56f
commit a225aaa185
+13
View File
@@ -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);
}