From: Andrew Duncan on
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/.

From: brabuhr on
On Mon, Jun 7, 2010 at 3:33 PM, Robert Dober <robert.dober(a)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?

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)

From: Andrew Duncan on
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/.

From: Joel VanderWerf on
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.


From: Andrew Duncan on
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/.