# Complex numbers contradiction?

Discussion in 'Ruby' started by Andrew Duncan, Jun 7, 2010.

1. ### Andrew DuncanGuest

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

Andrew Duncan, Jun 7, 2010

2. ### Robert DoberGuest

On Mon, Jun 7, 2010 at 9:01 PM, Andrew Duncan <> 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.

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

Robert Dober, Jun 7, 2010

3. ### Andrew DuncanGuest

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

Andrew Duncan, Jun 7, 2010
4. ### Jean-Julien FleckGuest

2010/6/7 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.

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

>> require 'complex'

=3D> true
>> 1/Complex(1,1)

=3D> Complex(0, -1)
>> Complex(1,1)**(-1)

=3D> Complex(Rational(1, 2), Rational(-1, 2))
>> 1/Complex(1,0)

=3D> Complex(1, 0)
>> 1/Complex(1,0.5)

=3D> Complex(0.8, -0.4)
>> 1/Complex(1,1)

=3D> Complex(0, -1)
>> 1/Complex(1,2)

=3D> Complex(0, -1)
>> 1/Complex(1,3)

=3D> Complex(0, -1)
>> 1/Complex(1,4)

=3D> Complex(0, -1)
>> 1/Complex(1,4.4)

=3D> Complex(0.0491159135559921, -0.216110019646365)
>> 1/Complex(1,-1)

=3D> Complex(0, 0)
>> 1/Complex(1,-2)

=3D> Complex(0, 0)
>> 1/Complex(2,-2)

=3D> Complex(0, 0)
>> 1/Complex(2.2,-2)

=3D> Complex(0.248868778280543, 0.226244343891403)
>> 1/(Complex(1.0001,1.0001))

=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

Jean-Julien Fleck, Jun 7, 2010
5. ### Andrew DuncanGuest

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

Andrew Duncan, Jun 7, 2010
6. ### Andrew DuncanGuest

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, Jun 7, 2010
7. ### Guest

On Mon, Jun 7, 2010 at 3:33 PM, Robert Dober <> wrote=
:
>> This looks correct:
>>>> Complex(1,1)**-1

>> =3D> Complex(Rational(1, 2), Rational(-1, 2))
>>
>> But this does not; should be same answer:
>>>> 1/Complex(1,1)

>> =3D> Complex(0, -1)
>>

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

, Jun 7, 2010
8. ### Andrew DuncanGuest

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

Andrew Duncan, Jun 7, 2010
9. ### Joel VanderWerfGuest

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.

Joel VanderWerf, Jun 8, 2010
10. ### Andrew DuncanGuest

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

Andrew Duncan, Jun 8, 2010
11. ### jzakiyaGuest

On Jun 7, 6:59 pm, 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.

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

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.

jzakiya, Jun 8, 2010
12. ### Joel VanderWerfGuest

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

Joel VanderWerf, Jun 8, 2010
13. ### Robert DoberGuest

On Mon, Jun 7, 2010 at 9:38 PM, Andrew Duncan <> 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...

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.

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

Robert Dober, Jun 8, 2010