diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-05 07:35:27 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-05 07:35:27 +0000 |
commit | a635de7dcbc14a1f59fbcf07122c6bce1fb21673 (patch) | |
tree | efb823dbd5bf08b14e02793e434f19c6cd64bc80 /numeric.c | |
parent | 6f8f555d2f8d46a4358d3aecdc6914022c20c5cb (diff) | |
download | ruby-a635de7dcbc14a1f59fbcf07122c6bce1fb21673.tar.gz |
* numeric.c (ruby_float_step): improve floating point calculations.
[ruby-core:35753] [Bug #4576]
* numeric.c (ruby_float_step): correct the error of floating point
numbers on the excluding case.
patched by Masahiro Tanaka [ruby-core:39608]
* numeric.c (ruby_float_step): use the end value when the current
value is greater than or equal to the end value.
patched by Akira Tanaka [ruby-core:39612]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33407 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -1690,10 +1690,25 @@ ruby_float_step(VALUE from, VALUE to, VALUE step, int excl) } else { if (err>0.5) err=0.5; - n = floor(n + err); - if (!excl || ((long)n)*unit+beg < end) n++; - for (i=0; i<n; i++) { - rb_yield(DBL2NUM(i*unit+beg)); + if (excl) { + if (n>0) { + if (n<err) + n = 1; + else + n = floor(n - err) + 1; + } + } else { + n = floor(n + err) + 1; + } + if (end < (n-1)*unit+beg) { + for (i=0; i<n; i++) { + rb_yield(DBL2NUM((n-1-i)/(n-1)*beg+i/(n-1)*end)); + } + } + else { + for (i=0; i<n; i++) { + rb_yield(DBL2NUM(i*unit+beg)); + } } } return TRUE; |