summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-04 14:05:36 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-04 14:05:36 +0000
commit0c27d72a0df796b3ffaea2ae1acfe5e86fbe1f27 (patch)
tree8fecc6eaeaf994c9c68b8d96cafb7c687d6079de /numeric.c
parent919989981081db5a4dce98ed61b5f5cce07f29d5 (diff)
merges r21298 from trunk into ruby_1_9_1.
* numeric.c (ruby_float_step): extracted from num_step(). * range.c (range_step): uses ruby_float_step() for float range. [ruby-dev:37691] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@21322 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c50
1 files changed, 29 insertions, 21 deletions
diff --git a/numeric.c b/numeric.c
index df942b302f..4fe06d2d4b 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1424,6 +1424,34 @@ num_truncate(VALUE num)
}
+int
+ruby_float_step(VALUE from, VALUE to, VALUE 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 (isinf(unit)) {
+ if (unit > 0) rb_yield(DBL2NUM(beg));
+ }
+ else {
+ if (err>0.5) err=0.5;
+ n = floor(n + err);
+ if (!excl) n++;
+ for (i=0; i<n; i++) {
+ rb_yield(DBL2NUM(i*unit+beg));
+ }
+ }
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
/*
* call-seq:
* num.step(limit, step ) {|i| block } => num
@@ -1494,27 +1522,7 @@ num_step(int argc, VALUE *argv, VALUE 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 (isinf(unit)) {
- if (unit > 0) rb_yield(DBL2NUM(beg));
- }
- else {
- if (err>0.5) err=0.5;
- n = floor(n + err) + 1;
- for (i=0; i<n; i++) {
- rb_yield(DBL2NUM(i*unit+beg));
- }
- }
- }
- else {
+ else if (!ruby_float_step(from, to, step, Qfalse)) {
VALUE i = from;
ID cmp;