mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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/trunk@21298 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a7999ab13c
commit
367de8b97b
3 changed files with 41 additions and 21 deletions
|
@ -1,3 +1,10 @@
|
|||
Sun Jan 4 11:58:43 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* numeric.c (ruby_float_step): extracted from num_step().
|
||||
|
||||
* range.c (range_step): uses ruby_float_step() for float range.
|
||||
[ruby-dev:37691]
|
||||
|
||||
Sun Jan 4 11:11:31 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* ext/extmk.rb (extmake): does not use both of makefile.rb and
|
||||
|
|
50
numeric.c
50
numeric.c
|
@ -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;
|
||||
|
||||
|
|
5
range.c
5
range.c
|
@ -267,6 +267,8 @@ step_i(VALUE i, void *arg)
|
|||
return Qnil;
|
||||
}
|
||||
|
||||
extern int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl);
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* rng.step(n=1) {| obj | block } => rng
|
||||
|
@ -334,6 +336,9 @@ range_step(int argc, VALUE *argv, VALUE range)
|
|||
}
|
||||
|
||||
}
|
||||
else if (ruby_float_step(b, e, step, EXCL(range))) {
|
||||
/* done */
|
||||
}
|
||||
else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
|
||||
!NIL_P(rb_check_to_integer(b, "to_int")) ||
|
||||
!NIL_P(rb_check_to_integer(e, "to_int"))) {
|
||||
|
|
Loading…
Add table
Reference in a new issue