diff options
| author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-01-04 02:58:45 +0000 |
|---|---|---|
| committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-01-04 02:58:45 +0000 |
| commit | badfd187b146c9a597f0bd88bdf9d57797cf353c (patch) | |
| tree | 57d614a85c249c770f26fa8eb6d359267d12243b /numeric.c | |
| parent | 103589aa7bc7e0c714db36c897448263365993ff (diff) | |
| download | ruby-badfd187b146c9a597f0bd88bdf9d57797cf353c.tar.gz ruby-badfd187b146c9a597f0bd88bdf9d57797cf353c.tar.xz ruby-badfd187b146c9a597f0bd88bdf9d57797cf353c.zip | |
* 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: http://svn.ruby-lang.org/repos/ruby/trunk@21298 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
| -rw-r--r-- | numeric.c | 50 |
1 files changed, 29 insertions, 21 deletions
@@ -1442,6 +1442,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 @@ -1512,27 +1540,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; |
