summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-04-29 12:51:05 -0700
committerJeremy Evans <code@jeremyevans.net>2021-05-29 08:56:15 -0700
commitf516379853f36d143d820c55d5eeaa9fc410ef52 (patch)
treee5a9e14896dee5767d0d33834613bd56c590ce66 /numeric.c
parente56ba6231f77dd0aa88a1ce737a342baafc884c7 (diff)
downloadruby-f516379853f36d143d820c55d5eeaa9fc410ef52.tar.gz
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]
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c20
1 files changed, 18 insertions, 2 deletions
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;
}