1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
naruse 2011-11-22 01:47:35 +00:00
parent 4e29a1a8fd
commit 033244c1b2
3 changed files with 57 additions and 4 deletions

View file

@ -1,3 +1,16 @@
Tue Nov 22 10:46:57 2011 NARUSE, Yui <naruse@ruby-lang.org>
* 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]
Tue Nov 22 06:59:21 2011 Tanaka Akira <akr@fsij.org>
* test/ruby/test_io.rb (test_fcntl_dupfd): there is no known platform

View file

@ -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;

View file

@ -508,4 +508,33 @@ class TestFloat < Test::Unit::TestCase
sleep(0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1)
end
end
def test_step
1000.times do
a = rand
b = a+rand*1000
s = (b - a) / 10
assert_equal(11, (a..b).step(s).to_a.length)
end
(1.0..12.7).step(1.3).each do |n|
assert_operator(n, :<=, 12.7)
end
end
def test_step_excl
1000.times do
a = rand
b = a+rand*1000
s = (b - a) / 10
assert_equal(10, (a...b).step(s).to_a.length)
end
assert_equal([1.0, 2.9, 4.8, 6.699999999999999], (1.0...6.8).step(1.9).to_a)
e = 1+1E-12
(1.0 ... e).step(1E-16) do |n|
assert_operator(n, :<=, e)
end
end
end