Complex numbers contradiction?

A

Andrew Duncan

This looks correct:=> Complex(Rational(1, 2), Rational(-1, 2))

But this does not; should be same answer:=> Complex(0, -1)

A similar try gives same (wrong) answer:=> Complex(0, -1)

What am I missing? Thanks!

Andrew
 
R

Robert Dober

This looks correct:
=> Complex(Rational(1, 2), Rational(-1, 2))

But this does not; should be same answer:
=> 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.
 
A

Andrew Duncan

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.
 
J

Jean-Julien Fleck

2010/6/7 Andrew Duncan said:
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:
=3D> Complex(0.4999500049995, -0.4999500049995)

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

Cheers,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
A

Andrew Duncan

Jean-Julien, you are correct, using integers yields error; using
floating point gives correct result:
=> Complex(0, -1)
 
B

brabuhr

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

which version do you have?

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)
 
A

Andrew Duncan

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
 
J

Joel VanderWerf

Andrew said:
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.
 
A

Andrew Duncan

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

jzakiya

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

Using Ruby 1.8.7-p249 installed with rvm:

I get this:
-----------------------------------------
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.
 
J

Joel VanderWerf

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

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

Robert Dober

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top