Complex numbers contradiction?

This looks correct:

Complex(1,1)**-1

=> Complex(Rational(1, 2), Rational(-1, 2))

But this does not; should be same answer:

1/Complex(1,1)

=> Complex(0, -1)

A similar try gives same (wrong) answer:

Complex(1,0) / Complex(1,1)

=> Complex(0, -1)

What am I missing? Thanks!

Andrew

···

--
Posted via http://www.ruby-forum.com/\.

Correct on my box
ruby-1.9.1-p378 > 1/Complex(1,1)
=> 12-12i

which version do you have?

HTH
R.

···

On Mon, Jun 7, 2010 at 9:01 PM, Andrew Duncan <andrew.duncan@sonos.com> wrote:

This looks correct:

Complex(1,1)**-1

=> Complex(Rational(1, 2), Rational(-1, 2))

But this does not; should be same answer:

1/Complex(1,1)

=> Complex(0, -1)

--
The best way to predict the future is to invent it.
-- Alan Kay

I have ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]

BTW, I hope that was 1/2 - 1/2i in your post...

If this is a bug, it's pretty egregious.

Robert Dober wrote:

···

On Mon, Jun 7, 2010 at 9:01 PM, Andrew Duncan <andrew.duncan@sonos.com> > wrote:

This looks correct:

Complex(1,1)**-1

=> Complex(Rational(1, 2), Rational(-1, 2))

But this does not; should be same answer:

1/Complex(1,1)

=> Complex(0, -1)

Correct on my box
ruby-1.9.1-p378 > 1/Complex(1,1)
=> 12-12i

which version do you have?

HTH
R.

--
Posted via http://www.ruby-forum.com/\.

ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
Complex(0, -1)

jruby 1.5.0 (ruby 1.8.7 patchlevel 249) (2010-05-12 6769999) (OpenJDK
Client VM 1.6.0_18) [i386-java]
Complex(0, -1)

jruby 1.5.0 (ruby 1.9.2dev trunk 24787) (2010-05-12 6769999) (OpenJDK
Client VM 1.6.0_18) [i386-java]
lib/complex.rb is deprecated
((1/2)-(1/2)*i)

rubinius 1.0.0 (1.8.7 release 2010-05-14 JI) [i686-pc-linux-gnu]
Complex(0, -1)

ruby 1.9.2dev (2009-07-18 trunk 24186) [i686-linux]
((1/2)-(1/2)*i)

···

On Mon, Jun 7, 2010 at 3:33 PM, Robert Dober <robert.dober@gmail.com> wrote:

This looks correct:

Complex(1,1)**-1

=> Complex(Rational(1, 2), Rational(-1, 2))

But this does not; should be same answer:

1/Complex(1,1)

=> Complex(0, -1)

Correct on my box
ruby-1.9.1-p378 > 1/Complex(1,1)
=> 12-12i

which version do you have?

I have ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]

BTW, I hope that was 1/2 - 1/2i in your post...

If this is a bug, it's pretty egregious.

Seems to be a bug when both real and imaginary parts are integers:

require 'complex'

=> true

1/Complex(1,1)

=> Complex(0, -1)

Complex(1,1)**(-1)

=> Complex(Rational(1, 2), Rational(-1, 2))

1/Complex(1,0)

=> Complex(1, 0)

1/Complex(1,0.5)

=> Complex(0.8, -0.4)

1/Complex(1,1)

=> Complex(0, -1)

1/Complex(1,2)

=> Complex(0, -1)

1/Complex(1,3)

=> Complex(0, -1)

1/Complex(1,4)

=> Complex(0, -1)

1/Complex(1,4.4)

=> Complex(0.0491159135559921, -0.216110019646365)

1/Complex(1,-1)

=> Complex(0, 0)

1/Complex(1,-2)

=> Complex(0, 0)

1/Complex(2,-2)

=> Complex(0, 0)

1/Complex(2.2,-2)

=> Complex(0.248868778280543, 0.226244343891403)

1/(Complex(1.0001,1.0001))

=> Complex(0.4999500049995, -0.4999500049995)

Brisingr ~>ruby -v
ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]

Cheers,

···

2010/6/7 Andrew Duncan <andrew.duncan@sonos.com>:

--
JJ Fleck
PCSI1 Lycée Kléber

Interestingly, when I just use the Web interpreter from
http://TryRuby.org, I do get the correct result also:

1/Complex(1,1)

=> ((1/2)-(1/2)*i)

Looks like a serious bug to me...

···

--
Posted via http://www.ruby-forum.com/\.

Although the varied results of this make it clear that the phenomenon is
probably not a *planned* feature of Ruby, it could just be a consequence
of integer vs. float context.

Probably the designers of Ruby were not intending to emulate the
Gaussian integers! (I.e. complex numbers with only integral real and
imaginary parts.)

But (as I now learn as I delve further into canonical Ruby syntax) it is
a "feature" that 5/2 evaluates to 2 and not 2.5. So something of the
sort is happening here.

Lesson: always specify complex literals with the trailing ".0" to force
them to be evaluated as floats!

Andrew

···

--
Posted via http://www.ruby-forum.com/.

That is how Irb shows them, why not provide a patch? We are spoiled,
aren't we? No offense intended I mean we are indeed *spoiled* by what
we get for free, and sometimes things are not perfect.
Cheers
R.

···

On Mon, Jun 7, 2010 at 9:38 PM, Andrew Duncan <andrew.duncan@sonos.com> wrote:

I have ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]

BTW, I hope that was 1/2 - 1/2i in your post...

--
The best way to predict the future is to invent it.
-- Alan Kay

Jean-Julien, you are correct, using integers yields error; using
floating point gives correct result:

1.0/Complex(1,1)

=> Complex(0.5, -0.5)

1/Complex(1,1)

=> Complex(0, -1)

···

--
Posted via http://www.ruby-forum.com/\.

Andrew Duncan wrote:

But (as I now learn as I delve further into canonical Ruby syntax) it is a "feature" that 5/2 evaluates to 2 and not 2.5. So something of the sort is happening here.

Many languages, including C, do integer division that way.

As a mathematician, and compiler writer for the last twenty years or so,
I am aware of that.

Joel VanderWerf wrote:

···

Andrew Duncan wrote:

But (as I now learn as I delve further into canonical Ruby syntax) it is
a "feature" that 5/2 evaluates to 2 and not 2.5. So something of the
sort is happening here.

Many languages, including C, do integer division that way.

--
Posted via http://www.ruby-forum.com/\.

Using Ruby 1.8.7-p249 installed with rvm:

I get this:

···

On Jun 7, 6:59 pm, Andrew Duncan <andrew.dun...@sonos.com> wrote:

As a mathematician, and compiler writer for the last twenty years or so,
I am aware of that.

Joel VanderWerf wrote:
> Andrew Duncan wrote:
>> But (as I now learn as I delve further into canonical Ruby syntax) it is
>> a "feature" that 5/2 evaluates to 2 and not 2.5. So something of the
>> sort is happening here.

> Many languages, including C, do integer division that way.

--
Posted viahttp://www.ruby-forum.com/.

-----------------------------------------
require 'complex'

Complex(1,1)**-1
=> Complex(0.5, -0.5)

1/Complex(1,1)
=> Complex(0, -1)
-----------------------------------------
But, when I use the 'mathn' lib I get:

require 'mathn'

Complex(1,1)**-1
=> Complex(1/2, -1/2)

1/Complex(1,1)
=> Complex(1/2, -1/2)
-----------------------------------------

So require the mathn library first for 1.8.7 to get correct results.

For 1.9.1 and 1.9.2 I also get correct results for both cases straight
out of the box.

Andrew Duncan wrote:

As a mathematician, and compiler writer for the last twenty years or so, I am aware of that.

Joel VanderWerf wrote:

Andrew Duncan wrote:

But (as I now learn as I delve further into canonical Ruby syntax) it is a "feature" that 5/2 evaluates to 2 and not 2.5. So something of the sort is happening here.

Many languages, including C, do integer division that way.

Sorry (*blush*). It's just a knee-jerk reaction to newcomers to this list who start talking about a "feature" of ruby that cannot possibly be correct and must be changed.

Again, I apologize for that (and for assuming you are a newb).