Project

General

Profile

Actions

Bug #3563

closed

problems with exponentiation, cbrt

Added by kolbrich (Kevin Olbrich) over 14 years ago. Updated over 13 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 1.9.2dev (2010-07-02 revision 28524) [i386-darwin9.8.0]
Backport:
[ruby-core:31234]

Description

=begin

nothing required

(-27)**(Rational(1,3)) #=> (1.5000000000000004+2.598076211353316i)

I would expect that this would return -3

Complex(1.5000000000000004,2.598076211353316)**3 #=> (-27.000000000000007+1.2434497875801753e-14i)

I can understand how this might happen given round off errors, but the following identity should be true

Math.cbrt(-27) == (-27)**(Rational(1,3)) #=> false

Also note that if you do this...

require 'complex'
Math.cbrt(-27) #=> (1.5000000000000004+2.598076211353316i)
=end

Actions #1

Updated by mame (Yusuke Endoh) over 14 years ago

  • Status changed from Open to Assigned
  • Assignee set to mame (Yusuke Endoh)

=begin
Hi,

2010/7/13 Kevin Olbrich :

(-27)**(Rational(1,3)) #=> (1.5000000000000004+2.598076211353316i)

I would expect that this would return -3

I think this is not a bug. 1.9.1 and 1.9.2 should be changed.
But I understand your expectation. To change this behavior in the
future, I propose to add a note to rdoc of Rational#**:

diff --git a/rational.c b/rational.c
index f5a6d26..734131b 100644
--- a/rational.c
+++ b/rational.c
@@ -907,6 +907,9 @@ nurat_fdiv(VALUE self, VALUE other)
*

  • Performs exponentiation.
    • When the solution is underspecified, it is not specified which
    • solution this method returns.
    • For example:
    • Rational(2) ** Rational(3) #=> (8/1)

Also note that if you do this...

require 'complex'
Math.cbrt(-27) #=> (1.5000000000000004+2.598076211353316i)

complex.rb is deprecated, but mathn.rb causes the same.
I think this is a bug of cmath:

diff --git a/lib/cmath.rb b/lib/cmath.rb
index b23dac2..976f269 100644
--- a/lib/cmath.rb
+++ b/lib/cmath.rb
@@ -85,7 +85,7 @@ module CMath
end

def cbrt(z)
  • if z.real? and z >= 0
  • if z.real?
    cbrt!(z)
    else
    Complex(z) ** (1.0/3)

Unless there is objection, I'll commit these patches to both trunk
and ruby_1_9_2.

--
Yusuke Endoh
=end

Actions #2

Updated by kolbrich (Kevin Olbrich) over 14 years ago

=begin
I'm fine with adding a comment, however the one you added does not really clarify the matter (at least not to me).
=end

Actions #3

Updated by mame (Yusuke Endoh) over 14 years ago

=begin
Umm. Could you make a draft?

2010/7/14 Kevin Olbrich :

I'm fine with adding a comment, however the one you added does not really clarify the matter (at least not to me).

--
Yusuke Endoh

=end

Actions #4

Updated by mame (Yusuke Endoh) over 14 years ago

=begin
I'll commit only the patch for cmath.rb and close this ticket.

If you (or anyone) show me a draft in a few days, I'll import it.
But if you don't, we will release 1.9.2 without any note, which
may make it difficult to change the behavior in 1.9.3 or later.

2010/7/14 Yusuke ENDOH :

Umm.  Could you make a draft?

2010/7/14 Kevin Olbrich :

I'm fine with adding a comment, however the one you added does not really clarify the matter (at least not to me).

--
Yusuke Endoh

--
Yusuke Endoh

=end

Actions #5

Updated by mame (Yusuke Endoh) over 14 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

=begin
This issue was solved with changeset r28698.
Kevin, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0