[#100309] How to use backport custom field — Jun Aruga <jaruga@...>
Please allow my ignorance.
9 messages
2020/10/06
[#100310] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/06
IkJhY2twb3J0IGN1c3RvbSBmaWVsZCIgaXMgb25seSBhdmFpbGFibGUgZm9yIHRpY2tldHMgd2hv
[#100311] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/06
On Tue, Oct 6, 2020 at 4:44 PM NARUSE, Yui <naruse@airemix.jp> wrote:
[#100314] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/06
VGhhbmsgeW91IGZvciBjb25maXJtYXRpb24uCkkgY2hlY2tlZCBhZ2FpbiBhbmQgdG8gZWRpdCBi
[#100322] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/07
On Tue, Oct 6, 2020 at 7:25 PM NARUSE, Yui <naruse@airemix.jp> wrote:
[#100326] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/07
SSBhZGRlZCB5b3UgdG8gIlJlcG9ydGVyIiByb2xlIGluIHRoZSBwcm9qZWN0CgoyMDIw5bm0MTDm
[#100327] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/07
On Wed, Oct 7, 2020 at 1:42 PM NARUSE, Yui <naruse@airemix.jp> wrote:
[ruby-core:100312] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment due to silent floating-point approximation
From:
rorymolinari@...
Date:
2020-10-06 15:46:51 UTC
List:
ruby-core #100312
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/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>