From b6b8afad4a603190ac3b3376b413a40588c84246 Mon Sep 17 00:00:00 2001 From: matz Date: Wed, 1 May 2002 09:41:50 +0000 Subject: * numeric.c (num_step): better iteration condition for float values; suggested by Masahiro TANAKA . * range.c (range_step): step (for Range#step method) <= 0 makes no sence, thus ArgError will be raised. * range.c (range_each): Range#each method is special case for Range#step(1) * file.c (rb_find_file): load must be done from an abolute path if $SAFE >= 4. * enum.c (enum_partition): new method. [new] * re.c (rb_reg_s_quote): quote whitespaces for /x cases. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@2420 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- numeric.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index 3d97c5218..76b59ecd7 100644 --- a/numeric.c +++ b/numeric.c @@ -788,6 +788,7 @@ num_step(argc, argv, from) while (i <= end) { rb_yield(INT2FIX(i)); i += diff; + printf("<<%g>>\n", i - end); } } else { @@ -798,21 +799,16 @@ num_step(argc, argv, from) } } else if (TYPE(from) == T_FLOAT || TYPE(to) == T_FLOAT || TYPE(step) == T_FLOAT) { + const double epsilon = 2.2204460492503131E-16; double beg = NUM2DBL(from); double end = NUM2DBL(to); double unit = NUM2DBL(step); - double n = beg; - long i = 0; + double n = (end - beg)/unit; + long i; - if (unit > 0) { - for (i=0; n<=end; i++, n=beg+unit*i) { - rb_yield(rb_float_new(n)); - } - } - else { - for (i=0; n>=end; i++, n=beg+unit*i) { - rb_yield(rb_float_new(n)); - } + n = floor(n + n*epsilon) + 1; + for (i=0; i