From f516379853f36d143d820c55d5eeaa9fc410ef52 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 29 Apr 2021 12:51:05 -0700 Subject: Fix Enumerator::ArithmeticSequence handling of float ranges Depending on the float range, there could be an off-by-one error, where the last result that should be in the range was missed. Fix this by checking if the computed value for the expected value outside the range is still inside the range, and if so, increment the step size. Fixes [Bug #16612] --- numeric.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index 17a401f7d7..d0538ca126 100644 --- a/numeric.c +++ b/numeric.c @@ -2409,11 +2409,11 @@ ruby_float_step_size(double beg, double end, double unit, int excl) if (unit == 0) { return HUGE_VAL; } - n= (end - beg)/unit; - err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon; if (isinf(unit)) { return unit > 0 ? beg <= end : beg >= end; } + n= (end - beg)/unit; + err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon; if (err>0.5) err=0.5; if (excl) { if (n<=0) return 0; @@ -2421,10 +2421,26 @@ ruby_float_step_size(double beg, double end, double unit, int excl) n = 0; else n = floor(n - err); + if (beg < end) { + if ((n+1)*unit+beg < end) + n++; + } + else if (beg > end) { + if ((n+1)*unit+beg > end) + n++; + } } else { if (n<0) return 0; n = floor(n + err); + if (beg < end) { + if ((n+1)*unit+beg <= end) + n++; + } + else if (beg > end) { + if ((n+1)*unit+beg >= end) + n++; + } } return n+1; } -- cgit v1.2.3