summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-11-22 01:47:35 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-11-22 01:47:35 +0000
commit033244c1b2b23c3f70a2a7c5cbdef570fdb9220e (patch)
tree08992e23607b57fae69487ef42c8f761b00d6d65 /numeric.c
parent4e29a1a8fd69e8f13dbfacdbb897fc378ac0ad4b (diff)
* 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@33811 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/numeric.c b/numeric.c
index e749a396d2..c8616f5f73 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1690,10 +1690,21 @@ 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) return TRUE;
+ if (n<1)
+ n = 0;
+ else
+ n = floor(n - err);
+ }
+ else {
+ if (n<0) return TRUE;
+ n = floor(n + err);
+ }
+ for (i=0; i<=n; i++) {
+ double d = i*unit+beg;
+ if (end < d) d = end;
+ rb_yield(DBL2NUM(d));
}
}
return TRUE;