ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:100312] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation
@ 2020-10-06 15:46 rorymolinari
  2020-10-06 16:06 ` [ruby-core:100313] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment mame
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: rorymolinari @ 2020-10-06 15:46 UTC (permalink / raw)
  To: ruby-core

Issue #17218 has been reported by rorymolinari (Rory Molinari).

----------------------------------------
Bug #17218: Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation
https://bugs.ruby-lang.org/issues/17218

* Author: rorymolinari (Rory Molinari)
* Status: Open
* Priority: Normal
* ruby -v: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
I am using the latest stable version. The same behavior exists in 2.6.6.
```
11:28:25 $ ruby -v                                                                                                                                                                                                                    
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
```

When creating an `Enumerator::ArithmeticSequence` with Rational endpoints and increment, sometimes inconsistent behavior results due to floating-point approximation in `#last`.

``` ruby
x = Rational(10997, 10000)
y = Rational(11, 10)
s = Rational(1, 10000)

puts "#{[x, y, s].map(&:to_f)}" # -> [1.0997, 1.1, 0.0001]

# intention: this contains exactly the precise Rational representations of 1.0997, 1.0998, 1.0999, 1.1
arith_seq = (x..y).step(s)

puts arith_seq.class # -> Enumerator::ArithmeticSequence

# Things look OK
puts arith_seq.first # -> 10997/10000
puts arith_seq.end  # -> 11/10
puts arith_seq.step  # -> 1/10000

# But the array that we get from #to_a is missing the last element, (11/10)
puts arith_seq.to_a.to_s # -> [(10997/10000), (5499/5000), (10999/10000)]

# This is apparently due to the value of #last
puts arith_seq.last # -> 1.0999999999999999

# The object itself is confused
puts arith_seq.size # -> 4
puts arith_seq.to_a.size # -> 3
```

The issue is in the `Enumerator::ArithmeticSequence` instance we get when we call `Range#step` without a block. The `Range#step` method passes the right things when it gets a block.
``` ruby
block_vals = []
(x..y).step(s) { |v| block_vals << v }
puts block_vals.to_s # -> [(10997/10000), (5499/5000), (10999/10000), (11/10)]
```

I would expect `arith_seq.last` to be the exact value `Rational(11, 10)`. After all, `arith_seq` was created from a `Range` with `Rational` endpoints and given a rational step size.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:100313] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment
  2020-10-06 15:46 [ruby-core:100312] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation rorymolinari
@ 2020-10-06 16:06 ` mame
  2020-11-24 22:09 ` [ruby-core:101056] " merch-redmine
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: mame @ 2020-10-06 16:06 UTC (permalink / raw)
  To: ruby-core

Issue #17218 has been updated by mame (Yusuke Endoh).

Assignee set to mrkn (Kenta Murata)
Status changed from Open to Assigned

----------------------------------------
Bug #17218: Range#step sometimes behaves unexpectedly with Rational endpoints and increment
https://bugs.ruby-lang.org/issues/17218#change-87895

* Author: rorymolinari (Rory Molinari)
* Status: Assigned
* Priority: Normal
* Assignee: mrkn (Kenta Murata)
* ruby -v: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
I am using the latest stable version. The same behavior exists in 2.6.6.
```
11:28:25 $ ruby -v                                                                                                                                                                                                                    
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
```

When creating an `Enumerator::ArithmeticSequence` with Rational endpoints and increment, sometimes inconsistent behavior results due to floating-point approximation in `#last`.

``` ruby
x = Rational(10997, 10000)
y = Rational(11, 10)
s = Rational(1, 10000)

puts "#{[x, y, s].map(&:to_f)}" # -> [1.0997, 1.1, 0.0001]

# intention: this contains exactly the precise Rational representations of 1.0997, 1.0998, 1.0999, 1.1
arith_seq = (x..y).step(s)

puts arith_seq.class # -> Enumerator::ArithmeticSequence

# Things look OK
puts arith_seq.first # -> 10997/10000
puts arith_seq.end  # -> 11/10
puts arith_seq.step  # -> 1/10000

# But the array that we get from #to_a is missing the last element, (11/10)
puts arith_seq.to_a.to_s # -> [(10997/10000), (5499/5000), (10999/10000)]

# This is apparently due to the value of #last
puts arith_seq.last # -> 1.0999999999999999

# The object itself is confused
puts arith_seq.size # -> 4
puts arith_seq.to_a.size # -> 3
```

The issue is in the `Enumerator::ArithmeticSequence` instance we get when we call `Range#step` without a block. The `Range#step` method passes the right things when it gets a block.
``` ruby
block_vals = []
(x..y).step(s) { |v| block_vals << v }
puts block_vals.to_s # -> [(10997/10000), (5499/5000), (10999/10000), (11/10)]
```

I would expect `arith_seq.last` to be the exact value `Rational(11, 10)`. After all, `arith_seq` was created from a `Range` with `Rational` endpoints and given a rational step size.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:101056] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment
  2020-10-06 15:46 [ruby-core:100312] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation rorymolinari
  2020-10-06 16:06 ` [ruby-core:100313] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment mame
@ 2020-11-24 22:09 ` merch-redmine
  2020-12-09  6:37 ` [ruby-core:101331] " muraken
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: merch-redmine @ 2020-11-24 22:09 UTC (permalink / raw)
  To: ruby-core

Issue #17218 has been updated by jeremyevans0 (Jeremy Evans).


This is caused by calling rb_int_plus/rb_int_mul/rb_int_minus directly instead of calling Ruby methods in arith_seq_last.  I'm guessing the use of Rational begin/end/step was not anticipated.  Calling Ruby methods seems to fix the issue.  I've submitted a pull request with that approach: https://github.com/ruby/ruby/pull/3810

----------------------------------------
Bug #17218: Range#step sometimes behaves unexpectedly with Rational endpoints and increment
https://bugs.ruby-lang.org/issues/17218#change-88728

* Author: rorymolinari (Rory Molinari)
* Status: Assigned
* Priority: Normal
* Assignee: mrkn (Kenta Murata)
* ruby -v: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
I am using the latest stable version. The same behavior exists in 2.6.6.
```
11:28:25 $ ruby -v                                                                                                                                                                                                                    
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
```

When creating an `Enumerator::ArithmeticSequence` with Rational endpoints and increment, sometimes inconsistent behavior results due to floating-point approximation in `#last`.

``` ruby
x = Rational(10997, 10000)
y = Rational(11, 10)
s = Rational(1, 10000)

puts "#{[x, y, s].map(&:to_f)}" # -> [1.0997, 1.1, 0.0001]

# intention: this contains exactly the precise Rational representations of 1.0997, 1.0998, 1.0999, 1.1
arith_seq = (x..y).step(s)

puts arith_seq.class # -> Enumerator::ArithmeticSequence

# Things look OK
puts arith_seq.first # -> 10997/10000
puts arith_seq.end  # -> 11/10
puts arith_seq.step  # -> 1/10000

# But the array that we get from #to_a is missing the last element, (11/10)
puts arith_seq.to_a.to_s # -> [(10997/10000), (5499/5000), (10999/10000)]

# This is apparently due to the value of #last
puts arith_seq.last # -> 1.0999999999999999

# The object itself is confused
puts arith_seq.size # -> 4
puts arith_seq.to_a.size # -> 3
```

The issue is in the `Enumerator::ArithmeticSequence` instance we get when we call `Range#step` without a block. The `Range#step` method passes the right things when it gets a block.
``` ruby
block_vals = []
(x..y).step(s) { |v| block_vals << v }
puts block_vals.to_s # -> [(10997/10000), (5499/5000), (10999/10000), (11/10)]
```

I would expect `arith_seq.last` to be the exact value `Rational(11, 10)`. After all, `arith_seq` was created from a `Range` with `Rational` endpoints and given a rational step size.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:101331] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment
  2020-10-06 15:46 [ruby-core:100312] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation rorymolinari
  2020-10-06 16:06 ` [ruby-core:100313] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment mame
  2020-11-24 22:09 ` [ruby-core:101056] " merch-redmine
@ 2020-12-09  6:37 ` muraken
  2021-03-20  6:35 ` [ruby-core:102950] " nagachika00
  2021-04-05  1:22 ` [ruby-core:103234] " usa
  4 siblings, 0 replies; 6+ messages in thread
From: muraken @ 2020-12-09  6:37 UTC (permalink / raw)
  To: ruby-core

Issue #17218 has been updated by mrkn (Kenta Murata).


@jeremyevans0 Thank you for making a candidate patch.  I have reviewed it, I noticed that it resolves only an issue of `last` method, but does not of `to_a` method.
I made another patch to resolve both issues: https://github.com/ruby/ruby/pull/3870

----------------------------------------
Bug #17218: Range#step sometimes behaves unexpectedly with Rational endpoints and increment
https://bugs.ruby-lang.org/issues/17218#change-89029

* Author: rorymolinari (Rory Molinari)
* Status: Assigned
* Priority: Normal
* Assignee: mrkn (Kenta Murata)
* ruby -v: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
I am using the latest stable version. The same behavior exists in 2.6.6.
```
11:28:25 $ ruby -v                                                                                                                                                                                                                    
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
```

When creating an `Enumerator::ArithmeticSequence` with Rational endpoints and increment, sometimes inconsistent behavior results due to floating-point approximation in `#last`.

``` ruby
x = Rational(10997, 10000)
y = Rational(11, 10)
s = Rational(1, 10000)

puts "#{[x, y, s].map(&:to_f)}" # -> [1.0997, 1.1, 0.0001]

# intention: this contains exactly the precise Rational representations of 1.0997, 1.0998, 1.0999, 1.1
arith_seq = (x..y).step(s)

puts arith_seq.class # -> Enumerator::ArithmeticSequence

# Things look OK
puts arith_seq.first # -> 10997/10000
puts arith_seq.end  # -> 11/10
puts arith_seq.step  # -> 1/10000

# But the array that we get from #to_a is missing the last element, (11/10)
puts arith_seq.to_a.to_s # -> [(10997/10000), (5499/5000), (10999/10000)]

# This is apparently due to the value of #last
puts arith_seq.last # -> 1.0999999999999999

# The object itself is confused
puts arith_seq.size # -> 4
puts arith_seq.to_a.size # -> 3
```

The issue is in the `Enumerator::ArithmeticSequence` instance we get when we call `Range#step` without a block. The `Range#step` method passes the right things when it gets a block.
``` ruby
block_vals = []
(x..y).step(s) { |v| block_vals << v }
puts block_vals.to_s # -> [(10997/10000), (5499/5000), (10999/10000), (11/10)]
```

I would expect `arith_seq.last` to be the exact value `Rational(11, 10)`. After all, `arith_seq` was created from a `Range` with `Rational` endpoints and given a rational step size.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:102950] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment
  2020-10-06 15:46 [ruby-core:100312] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation rorymolinari
                   ` (2 preceding siblings ...)
  2020-12-09  6:37 ` [ruby-core:101331] " muraken
@ 2021-03-20  6:35 ` nagachika00
  2021-04-05  1:22 ` [ruby-core:103234] " usa
  4 siblings, 0 replies; 6+ messages in thread
From: nagachika00 @ 2021-03-20  6:35 UTC (permalink / raw)
  To: ruby-core

Issue #17218 has been updated by nagachika (Tomoyuki Chikanaga).

Backport changed from 2.5: DONTNEED, 2.6: REQUIRED, 2.7: REQUIRED to 2.5: DONTNEED, 2.6: REQUIRED, 2.7: DONE

ruby_2_7 82bce422ba9e131e62b528854dea69a6e8cc0c04 merged revision(s) 254bed302752a401b5fcc3b6c65a9c93711d91d6,fad3023e94c45e7f03478732f7641b6f39ba9d12,3156fb0f2c3ebf8229f392c8502c08fe165ab181.

----------------------------------------
Bug #17218: Range#step sometimes behaves unexpectedly with Rational endpoints and increment
https://bugs.ruby-lang.org/issues/17218#change-91015

* Author: rorymolinari (Rory Molinari)
* Status: Closed
* Priority: Normal
* Assignee: mrkn (Kenta Murata)
* ruby -v: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
* Backport: 2.5: DONTNEED, 2.6: REQUIRED, 2.7: DONE
----------------------------------------
I am using the latest stable version. The same behavior exists in 2.6.6.
```
11:28:25 $ ruby -v                                                                                                                                                                                                                    
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
```

When creating an `Enumerator::ArithmeticSequence` with Rational endpoints and increment, sometimes inconsistent behavior results due to floating-point approximation in `#last`.

``` ruby
x = Rational(10997, 10000)
y = Rational(11, 10)
s = Rational(1, 10000)

puts "#{[x, y, s].map(&:to_f)}" # -> [1.0997, 1.1, 0.0001]

# intention: this contains exactly the precise Rational representations of 1.0997, 1.0998, 1.0999, 1.1
arith_seq = (x..y).step(s)

puts arith_seq.class # -> Enumerator::ArithmeticSequence

# Things look OK
puts arith_seq.first # -> 10997/10000
puts arith_seq.end  # -> 11/10
puts arith_seq.step  # -> 1/10000

# But the array that we get from #to_a is missing the last element, (11/10)
puts arith_seq.to_a.to_s # -> [(10997/10000), (5499/5000), (10999/10000)]

# This is apparently due to the value of #last
puts arith_seq.last # -> 1.0999999999999999

# The object itself is confused
puts arith_seq.size # -> 4
puts arith_seq.to_a.size # -> 3
```

The issue is in the `Enumerator::ArithmeticSequence` instance we get when we call `Range#step` without a block. The `Range#step` method passes the right things when it gets a block.
``` ruby
block_vals = []
(x..y).step(s) { |v| block_vals << v }
puts block_vals.to_s # -> [(10997/10000), (5499/5000), (10999/10000), (11/10)]
```

I would expect `arith_seq.last` to be the exact value `Rational(11, 10)`. After all, `arith_seq` was created from a `Range` with `Rational` endpoints and given a rational step size.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:103234] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment
  2020-10-06 15:46 [ruby-core:100312] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation rorymolinari
                   ` (3 preceding siblings ...)
  2021-03-20  6:35 ` [ruby-core:102950] " nagachika00
@ 2021-04-05  1:22 ` usa
  4 siblings, 0 replies; 6+ messages in thread
From: usa @ 2021-04-05  1:22 UTC (permalink / raw)
  To: ruby-core

Issue #17218 has been updated by usa (Usaku NAKAMURA).


backported into ruby_2_6 at r67936

----------------------------------------
Bug #17218: Range#step sometimes behaves unexpectedly with Rational endpoints and increment
https://bugs.ruby-lang.org/issues/17218#change-91314

* Author: rorymolinari (Rory Molinari)
* Status: Closed
* Priority: Normal
* Assignee: mrkn (Kenta Murata)
* ruby -v: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
* Backport: 2.5: DONTNEED, 2.6: REQUIRED, 2.7: DONE
----------------------------------------
I am using the latest stable version. The same behavior exists in 2.6.6.
```
11:28:25 $ ruby -v                                                                                                                                                                                                                    
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
```

When creating an `Enumerator::ArithmeticSequence` with Rational endpoints and increment, sometimes inconsistent behavior results due to floating-point approximation in `#last`.

``` ruby
x = Rational(10997, 10000)
y = Rational(11, 10)
s = Rational(1, 10000)

puts "#{[x, y, s].map(&:to_f)}" # -> [1.0997, 1.1, 0.0001]

# intention: this contains exactly the precise Rational representations of 1.0997, 1.0998, 1.0999, 1.1
arith_seq = (x..y).step(s)

puts arith_seq.class # -> Enumerator::ArithmeticSequence

# Things look OK
puts arith_seq.first # -> 10997/10000
puts arith_seq.end  # -> 11/10
puts arith_seq.step  # -> 1/10000

# But the array that we get from #to_a is missing the last element, (11/10)
puts arith_seq.to_a.to_s # -> [(10997/10000), (5499/5000), (10999/10000)]

# This is apparently due to the value of #last
puts arith_seq.last # -> 1.0999999999999999

# The object itself is confused
puts arith_seq.size # -> 4
puts arith_seq.to_a.size # -> 3
```

The issue is in the `Enumerator::ArithmeticSequence` instance we get when we call `Range#step` without a block. The `Range#step` method passes the right things when it gets a block.
``` ruby
block_vals = []
(x..y).step(s) { |v| block_vals << v }
puts block_vals.to_s # -> [(10997/10000), (5499/5000), (10999/10000), (11/10)]
```

I would expect `arith_seq.last` to be the exact value `Rational(11, 10)`. After all, `arith_seq` was created from a `Range` with `Rational` endpoints and given a rational step size.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-04-05  1:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-06 15:46 [ruby-core:100312] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation rorymolinari
2020-10-06 16:06 ` [ruby-core:100313] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment mame
2020-11-24 22:09 ` [ruby-core:101056] " merch-redmine
2020-12-09  6:37 ` [ruby-core:101331] " muraken
2021-03-20  6:35 ` [ruby-core:102950] " nagachika00
2021-04-05  1:22 ` [ruby-core:103234] " usa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).