Skip to content

Instantly share code, notes, and snippets.

@mkrogemann
Last active December 16, 2015 00:49
Show Gist options
  • Select an option

  • Save mkrogemann/5350319 to your computer and use it in GitHub Desktop.

Select an option

Save mkrogemann/5350319 to your computer and use it in GitHub Desktop.
Ruby's Rational and Complex making me feel uneasy
Stumbling across Rational and Complex for the first time while working with Date/DateTime,
found asking myself: Is there some area for work on these in MRI?
Here is what Ruby Doc says for the Rational class
Rational(-4) ** Rational(1,2) #=> (1.2246063538223773e-16+2.0i)
And here is what my local installation of Ruby 2.0.0 says (same for 1.8.7 and 1.9.3, all on OSX 10.7)
Rational(-4) ** Rational(1,2) #=> (1.2246467991473532e-16+2.0i)
Ok, let's fix that, Easy.
But that's not all:
1.9.3-p392 :005 > Rational(-1) ** Rational(1,2)
=> (6.123233995736766e-17+1.0i)
But should this not be the complex number (0,1)?
1.9.3-p392 :009 > Complex(6.123233995736766e-17,1) == Complex(0,1)
=> false
The following delivers the expected results though:
1.9.3-p392 :020 > (Rational(1) ** Rational(1,2)) == Complex(1,0)
=> true
Hm...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment