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-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00, 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 585431F4B4 for ; Mon, 12 Oct 2020 04:35:07 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 32BDA120A07; Mon, 12 Oct 2020 13:34:29 +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 360DA1209E8 for ; Mon, 12 Oct 2020 13:34:27 +0900 (JST) Received: by filterdrecv-p3mdw1-6685f47d68-9kn8s with SMTP id filterdrecv-p3mdw1-6685f47d68-9kn8s-18-5F83DCF5-54 2020-10-12 04:35:01.950509441 +0000 UTC m=+367137.976449071 Received: from herokuapp.com (unknown) by geopod-ismtpd-4-3 (SG) with ESMTP id FI24dknNSSeGG20X2cL45Q for ; Mon, 12 Oct 2020 04:35:01.903 +0000 (UTC) Date: Mon, 12 Oct 2020 04:35:01 +0000 (UTC) From: mame@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 76244 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17257 X-Redmine-Issue-Author: universato X-Redmine-Issue-Assignee: mrkn X-Redmine-Sender: mame 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?EJh2gqwnyqXtd++xo=2FinyA1V0bXouTB4FkWnzNiKb4+kcbbqemy6HPYvIW96jq?= =?us-ascii?Q?ELB56OWVr9zGgzRC9fQTKdjK=2F642HJsOKmz4L2L?= =?us-ascii?Q?pT9PtqpOCGwXvwVyc+pXmKZtbzXmM8YqJQU9jm5?= =?us-ascii?Q?NO4mFjKiaTNYdO9lupotXMZcPU86jzfF6BIzNmN?= =?us-ascii?Q?wQF+mYbTkeNL90dMJKFwqZQdzKL0dCWd2BvOsZn?= =?us-ascii?Q?LeR+6GhzNItDOuxaQ=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 100379 Subject: [ruby-core:100379] [Ruby master Bug#17257] Integer#pow(0, 1) returns 1, which is incorrect 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 #17257 has been updated by mame (Yusuke Endoh). I agree that it is a bug. The pseudocode of modular exponentiation in Wikipedia includes `if modulus = 1 then return 0` as the first check. https://en.wikipedia.org/wiki/Modular_exponentiation#Pseudocode I agree that there are few real use cases of modulo 1, so few one will be affected by this change. I'll fix it soon. ```diff diff --git a/bignum.c b/bignum.c index 65a50ea9ba..0515e2f0d6 100644 --- a/bignum.c +++ b/bignum.c @@ -7136,6 +7136,7 @@ rb_int_powm(int const argc, VALUE * const argv, VALUE const num) long const half_val = (long)HALF_LONG_MSB; long const mm = FIX2LONG(m); if (!mm) rb_num_zerodiv(); + if (mm == 1) return INT2FIX(0); if (mm <= half_val) { return int_pow_tmp1(rb_int_modulo(a, m), b, mm, nega_flg); } @@ -7145,6 +7146,7 @@ rb_int_powm(int const argc, VALUE * const argv, VALUE const num) } else { if (rb_bigzero_p(m)) rb_num_zerodiv(); + if (bignorm(m) == INT2FIX(1)) return INT2FIX(0); return int_pow_tmp3(rb_int_modulo(a, m), b, m, nega_flg); } } ``` ---------------------------------------- Bug #17257: Integer#pow(0, 1) returns 1, which is incorrect https://bugs.ruby-lang.org/issues/17257#change-87990 * Author: universato (Yoshimine Sato) * Status: Assigned * Priority: Normal * Assignee: mrkn (Kenta Murata) * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- Ruby 2.5.8, 2.6.6, 2.7.1 ```ruby p -1.pow(0, 1) #=> 1 p 0.pow(0, 1) #=> 1 p 1.pow(0, 1) #=> 1 p 1234567890.pow(0, 1) #=> 1 ``` These return values should be 0. Patch for test: Let's add some boundary value tests to `test_pow` of [test_numeric.rb](https://github.com/ruby/ruby/blob/e014e6bf6685f681998238ff005f6d161d43ce51/test/ruby/test_numeric.rb). ```ruby integers = [-2, -1, 0, 1, 2, 3, 6, 1234567890123456789] integers.each do |i| assert_equal(0, i.pow(0, 1), '[Bug #17257]') assert_equal(1, i.pow(0, 2)) assert_equal(1, i.pow(0, 3)) assert_equal(1, i.pow(0, 6)) assert_equal(1, i.pow(0, 1234567890123456789)) assert_equal(0, i.pow(0, -1)) assert_equal(-1, i.pow(0, -2)) assert_equal(-2, i.pow(0, -3)) assert_equal(-5, i.pow(0, -6)) assert_equal(-1234567890123456788, i.pow(0, -1234567890123456789)) end assert_equal(0, 0.pow(2, 1)) assert_equal(0, 0.pow(3, 1)) assert_equal(0, 2.pow(3, 1)) assert_equal(0, -2.pow(3, 1)) -- https://bugs.ruby-lang.org/