ruby-changes:9757
From: nobu <ko1@a...>
Date: Sun, 4 Jan 2009 11:59:10 +0900 (JST)
Subject: [ruby-changes:9757] Ruby:r21298 (trunk, ruby_1_8): * numeric.c (ruby_float_step): extracted from num_step().
nobu 2009-01-04 11:58:45 +0900 (Sun, 04 Jan 2009) New Revision: 21298 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=21298 Log: * numeric.c (ruby_float_step): extracted from num_step(). * range.c (range_step): uses ruby_float_step() for float range. [ruby-dev:37691] Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/numeric.c branches/ruby_1_8/range.c trunk/ChangeLog trunk/numeric.c trunk/range.c Index: ChangeLog =================================================================== --- ChangeLog (revision 21297) +++ ChangeLog (revision 21298) @@ -1,3 +1,10 @@ +Sun Jan 4 11:58:43 2009 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * ext/extmk.rb (extmake): does not use both of makefile.rb and Index: range.c =================================================================== --- range.c (revision 21297) +++ range.c (revision 21298) @@ -267,6 +267,8 @@ 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 @@ } } + 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"))) { Index: numeric.c =================================================================== --- numeric.c (revision 21297) +++ numeric.c (revision 21298) @@ -1442,6 +1442,34 @@ } +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 @@ } } } - 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; Index: ruby_1_8/numeric.c =================================================================== --- ruby_1_8/numeric.c (revision 21297) +++ ruby_1_8/numeric.c (revision 21298) @@ -1430,6 +1430,33 @@ } +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 @@ -1504,22 +1531,7 @@ } } } - 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; Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 21297) +++ ruby_1_8/ChangeLog (revision 21298) @@ -1,3 +1,10 @@ +Sun Jan 4 11:58:43 2009 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * ext/extmk.rb (extmake): does not use both of makefile.rb and Index: ruby_1_8/range.c =================================================================== --- ruby_1_8/range.c (revision 21297) +++ ruby_1_8/range.c (revision 21298) @@ -273,6 +273,8 @@ return Qnil; } +extern int ruby_float_step _((VALUE from, VALUE to, VALUE step, int excl)); + /* * call-seq: * rng.step(n=1) {| obj | block } => rng @@ -343,6 +345,9 @@ } } + 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"))) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/