From: mame@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100379] [Ruby master Bug#17257] Integer#pow(0, 1) returns 1, which is incorrect
Date: Mon, 12 Oct 2020 04:35:01 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-87990.20201012043501.45098@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17257.20201009141843.45098@ruby-lang.org
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/
prev parent reply other threads:[~2020-10-12 4:35 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-09 14:18 [ruby-core:100348] [Ruby master Bug#17257] Integer#pow(0, 1) returns 1, which is incorrect universato
2020-10-10 6:54 ` [ruby-core:100355] " nobu
2020-10-10 9:18 ` [ruby-core:100356] " universato
2020-10-10 18:12 ` [ruby-core:100359] " midnight_w
2020-10-10 19:28 ` [ruby-core:100361] " marcandre-ruby-core
2020-10-11 2:48 ` [ruby-core:100363] " nobu
2020-10-11 3:32 ` [ruby-core:100365] " midnight_w
2020-10-11 3:41 ` [ruby-core:100366] " midnight_w
2020-10-11 5:54 ` [ruby-core:100367] " universato
2020-10-11 12:09 ` [ruby-core:100373] " sawadatsuyoshi
2020-10-11 13:18 ` [ruby-core:100374] " midnight_w
2020-10-11 18:46 ` [ruby-core:100375] " universato
2020-10-12 4:35 ` mame [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-87990.20201012043501.45098@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).