From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 6DA5D1F4B4 for ; Mon, 5 Apr 2021 01:22:37 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 413D9120D64; Mon, 5 Apr 2021 10:21:37 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 8E22A120D5E for ; Mon, 5 Apr 2021 10:21:34 +0900 (JST) Received: by filterdrecv-p3iad2-7d7c446bd4-wzwn5 with SMTP id filterdrecv-p3iad2-7d7c446bd4-wzwn5-19-606A6657-30 2021-04-05 01:22:31.92654957 +0000 UTC m=+1061374.539601163 Received: from herokuapp.com (unknown) by geopod-ismtpd-3-1 (SG) with ESMTP id BbXhThkTR8GumAm1XpcjQw for ; Mon, 05 Apr 2021 01:22:31.866 +0000 (UTC) Date: Mon, 05 Apr 2021 01:22:31 +0000 (UTC) From: usa@garbagecollect.jp Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 79279 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17218 X-Redmine-Issue-Author: rorymolinari X-Redmine-Issue-Assignee: mrkn X-Redmine-Sender: usa X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?9Ij0W+xF+66shKwcOf8RvdqxJGkEJjaCZuueI4cieuCaeaadYBVp2BWnvjZYn7?= =?us-ascii?Q?pJz0cVoPhv5poL2SEfLoCi=2Fh2bSUOnKyci7K+yT?= =?us-ascii?Q?z2cW=2FB951fOlGRi6tDtMTvE8r3HP82L8zO8fviY?= =?us-ascii?Q?ng2iQa=2FK5ac9so3q=2FfqrE30PUuyYEikfISuwY9H?= =?us-ascii?Q?YGE82v+dCvbuVZOjERuKOop5UAgA8LhqWT9Z4Tj?= =?us-ascii?Q?zXstUSuHPkr1HkRwE=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 103234 Subject: [ruby-core:103234] [Ruby master Bug#17218] Range#step sometimes behaves unexpectedly with Rational endpoints and increment X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "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/