# Complex numbers contradiction?

**URL:** <https://rubytalk.org/t/complex-numbers-contradiction/58805>\
**Category:** ruby-talk\
**Created:** [7 June 2010 19:01 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805 "2010-06-07T19:01:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Duncan](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Andrew\_Duncan](https://rubytalk.org/u/Andrew_Duncan)\
**Post date:** [7 June 2010 19:01 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/1 "2010-06-07T19:01:24Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [7 June 2010 19:33 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/2 "2010-06-07T19:33:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrew\_Duncan](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Andrew\_Duncan](https://rubytalk.org/u/Andrew_Duncan)\
**Post date:** [7 June 2010 19:38 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/3 "2010-06-07T19:38:58Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brabuhr](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Brabuhr](https://rubytalk.org/u/Brabuhr)\
**Post date:** [7 June 2010 19:52 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/4 "2010-06-07T19:52:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Fleck\_Jean-Julien](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Fleck\_Jean-Julien](https://rubytalk.org/u/Fleck_Jean-Julien)\
**Post date:** [7 June 2010 19:42 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/5 "2010-06-07T19:42:34Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Andrew\_Duncan](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Andrew\_Duncan](https://rubytalk.org/u/Andrew_Duncan)\
**Post date:** [7 June 2010 19:43 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/6 "2010-06-07T19:43:35Z")

</div>

Interestingly, when I just use the Web interpreter from  
[http://TryRuby.org](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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Andrew\_Duncan](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Andrew\_Duncan](https://rubytalk.org/u/Andrew_Duncan)\
**Post date:** [7 June 2010 22:50 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/7 "2010-06-07T22:50:51Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [8 June 2010 08:11 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/8 "2010-06-08T08:11:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrew\_Duncan](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Andrew\_Duncan](https://rubytalk.org/u/Andrew_Duncan)\
**Post date:** [7 June 2010 19:47 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/9 "2010-06-07T19:47:03Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://rubytalk.org/u/Joel_VanderWerf)\
**Post date:** [7 June 2010 23:24 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/10 "2010-06-07T23:24:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrew\_Duncan](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Andrew\_Duncan](https://rubytalk.org/u/Andrew_Duncan)\
**Post date:** [7 June 2010 23:59 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/11 "2010-06-07T23:59:16Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![jzakiya](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://rubytalk.org/u/jzakiya)\
**Post date:** [8 June 2010 01:25 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/12 "2010-06-08T01:25:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://rubytalk.org/u/Joel_VanderWerf)\
**Post date:** [8 June 2010 02:48 UTC](https://rubytalk.org/t/complex-numbers-contradiction/58805/13 "2010-06-08T02:48:55Z")

</div>

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