ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: midnight_w@gmx.tw
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100359] [Ruby master Bug#17257] Integer#pow(0, 1) returns 1, which is incorrect
Date: Sat, 10 Oct 2020 18:12:38 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-87970.20201010181237.45098@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17257.20201009141843.45098@ruby-lang.org

Issue #17257 has been updated by midnight (Sarun R).


This is undefined
`0.pow(0, 1)`
https://www.wolframalpha.com/input/?i=0%5E0

This is clearly out of the space (thus a bug)
`1.pow(0, 1)`
https://www.wolframalpha.com/input/?i=%281%5E0%29+mod+1

It is practically pointless to mod by 1; I don't think this change will break anything in the wild.
Nobody would rely on 1%1 == 0.
The problem is what to do with 0 ** 0.

----------------------------------------
Bug #17257: Integer#pow(0, 1) returns 1, which is incorrect
https://bugs.ruby-lang.org/issues/17257#change-87970

* 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/

  parent reply	other threads:[~2020-10-10 18:12 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 ` midnight_w [this message]
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 ` [ruby-core:100379] " mame

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-87970.20201010181237.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).