From a225aaa185ee30e87ab3b830e4bfbc5852b7bd51 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 12 Jan 2026 09:17:24 +0900 Subject: [PATCH] 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 --- src/numeric.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/numeric.c b/src/numeric.c index 5bbaeee1a..db0264ff4 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -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); }