summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c44
1 files changed, 28 insertions, 16 deletions
diff --git a/numeric.c b/numeric.c
index 4f181035ae..de00de20d9 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1427,6 +1427,33 @@ num_truncate(num)
}
+int ruby_float_step _((VALUE from, VALUE to, VALUE step, int excl));
+
+int
+ruby_float_step(from, to, step, excl)
+ VALUE from, to, step;
+ int excl;
+{
+ if (TYPE(from) == T_FLOAT || TYPE(to) == T_FLOAT || TYPE(step) == T_FLOAT) {
+ const double epsilon = DBL_EPSILON;
+ double beg = NUM2DBL(from);
+ double end = NUM2DBL(to);
+ double unit = NUM2DBL(step);
+ double n = (end - beg)/unit;
+ double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
+ long i;
+
+ if (err>0.5) err=0.5;
+ n = floor(n + err);
+ if (!excl) n++;
+ for (i=0; i<n; i++) {
+ rb_yield(rb_float_new(i*unit+beg));
+ }
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
/*
* call-seq:
* num.step(limit, step ) {|i| block } => num
@@ -1501,22 +1528,7 @@ num_step(argc, argv, from)
}
}
}
- else if (TYPE(from) == T_FLOAT || TYPE(to) == T_FLOAT || TYPE(step) == T_FLOAT) {
- const double epsilon = DBL_EPSILON;
- double beg = NUM2DBL(from);
- double end = NUM2DBL(to);
- double unit = NUM2DBL(step);
- double n = (end - beg)/unit;
- double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
- long i;
-
- if (err>0.5) err=0.5;
- n = floor(n + err) + 1;
- for (i=0; i<n; i++) {
- rb_yield(rb_float_new(i*unit+beg));
- }
- }
- else {
+ else if (!ruby_float_step(from, to, step, Qfalse)) {
VALUE i = from;
ID cmp;