summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-04-24 05:08:04 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-04-24 05:08:04 +0000
commit936ad409ad96607159bef4042f1e860ffe027fcc (patch)
tree76b5e71dda411ea9d360f75969339d5f3a240c79 /numeric.c
parenta117cec653030f1e1e33d2c10b057c044fad4618 (diff)
* numeric.c (num_step): try to reduce residual on Float operations.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2403 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c41
1 files changed, 30 insertions, 11 deletions
diff --git a/numeric.c b/numeric.c
index 58961d78ef..01dadb8471 100644
--- a/numeric.c
+++ b/numeric.c
@@ -769,8 +769,6 @@ num_step(argc, argv, from)
VALUE from;
{
VALUE to, step;
- VALUE i = from;
- ID cmp;
if (rb_scan_args(argc, argv, "11", &to, &step) == 1) {
step = INT2FIX(1);
@@ -798,19 +796,40 @@ num_step(argc, argv, from)
i += diff;
}
}
- return from;
}
+ else if (TYPE(from) == T_FLOAT || TYPE(to) == T_FLOAT || TYPE(step) == T_FLOAT) {
+ double beg = NUM2DBL(from);
+ double end = NUM2DBL(to);
+ double unit = NUM2DBL(step);
+ double n = beg;
+ long i = 0;
- if (RTEST(rb_funcall(step, '>', 1, INT2FIX(0)))) {
- cmp = '>';
+ if (unit > 0) {
+ for (i=0; n<=end; i++, n=unit*i+beg) {
+ rb_yield(rb_float_new(n));
+ }
+ }
+ else {
+ for (i=0; n>=end; i++, n=unit*i+beg) {
+ rb_yield(rb_float_new(n));
+ }
+ }
}
else {
- cmp = '<';
- }
- for (;;) {
- if (RTEST(rb_funcall(i, cmp, 1, to))) break;
- rb_yield(i);
- i = rb_funcall(i, '+', 1, step);
+ VALUE i = from;
+ ID cmp;
+
+ if (RTEST(rb_funcall(step, '>', 1, INT2FIX(0)))) {
+ cmp = '>';
+ }
+ else {
+ cmp = '<';
+ }
+ for (;;) {
+ if (RTEST(rb_funcall(i, cmp, 1, to))) break;
+ rb_yield(i);
+ i = rb_funcall(i, '+', 1, step);
+ }
}
return from;
}