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:100374] [Ruby master Bug#17257] Integer#pow(0, 1) returns 1, which is incorrect
Date: Sun, 11 Oct 2020 13:18:07 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-87984.20201011131806.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).


The point of undefinition is:
There is nothing definitely right or wrong.
Neither 0 nor 1 is wrong; it is just a different point of view.

You are thinking that 0 ** 0 == 1 is useful because you are looking from the perspective of x ** 0 == 1 for most of x
while 0 ** x == 0 is also true for most of x too, and it is as useful as x ** 0 == 1.

Weather 0 ** 0 should return 0 or 1 is subjective, and whatever the actual implementation is, it is not entirely wrong, same for little-endian vs. big-endian.
This is the reason why the value is undefined.
Regardless of mathematical definitions, I second for `x.pow(y, 1)` should return 0, as I said initially, `% m` is the space and every value inside `% 1` space is 0.

Mathematical wise this is considered undefined behavior. Implementation wise there is an advantage that we follow other popular languages in this matter.

I just want to point out that people who oppose the behavior change has some merit.
Even if the current behavior sounds wildly wrong, most people are not going to care, because the value is mathematically undefined in the first place.
And no practical result will be obtained from `% 1` space.

Anyway, `x % 1 == 0` sound more "correct" to me too.

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

* 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-11 13:18 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 ` midnight_w [this message]
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-87984.20201011131806.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).