Skip to content

Commit f4de205

Browse files
committed
range.c (range_num_to_a): reduce integer to float conversion
For big numbers (especially integers close to MRB_INT_MAX), float conversion may be lose precisions. So we try not to convert numbers, but just compare.
1 parent 76d3739 commit f4de205

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

src/range.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,27 +382,25 @@ range_num_to_a(mrb_state *mrb, mrb_value range)
382382
}
383383
#ifndef MRB_NO_FLOAT
384384
if (mrb_float_p(end)) {
385-
mrb_float a = (mrb_float)mrb_integer(beg);
385+
mrb_int a = mrb_integer(beg);
386386
mrb_float b = mrb_float(end);
387387

388388
if (a > b) {
389389
return mrb_ary_new_capa(mrb, 0);
390390
}
391-
ary = mrb_ary_new_capa(mrb, (mrb_int)(b - a) + 1);
391+
mrb_int alen = (mrb_int)(b - a) + 1;
392+
ary = mrb_ary_new_capa(mrb, alen);
392393
mrb_value *ptr = RARRAY_PTR(ary);
393-
mrb_int i = 0;
394394
if (RANGE_EXCL(r)) {
395-
while (a < b) {
396-
ptr[i++] = mrb_int_value(mrb, (mrb_int)a);
395+
for (mrb_int i=0; a<b; a++) {
396+
ptr[i++] = mrb_int_value(mrb, a);
397397
ARY_SET_LEN(RARRAY(ary), i);
398-
a += 1.0;
399398
}
400399
}
401400
else {
402-
while (a <= b) {
403-
ptr[i++] = mrb_int_value(mrb, (mrb_int)a);
401+
for (mrb_int i=0; a<=b; a++) {
402+
ptr[i++] = mrb_int_value(mrb, a);
404403
ARY_SET_LEN(RARRAY(ary), i);
405-
a += 1.0;
406404
}
407405
}
408406
return ary;

0 commit comments

Comments
 (0)