In ruby-doc, it says that <Fixnum> ** <Numeric> may be fractional, and gives the examples:
2 ** -1 #=> 0.5
2 ** 0.5 #=> 1.4142135623731
but on my irb, it sometimes gives a Rational answer as with the exponent -1 below:
2 ** -1 #=> (1/2)
2 ** 0.5 #=> 1.4142135623731
It looks like ruby-doc is not accurate, and ruby tries to type cast to Rational when possible, but I am not completely sure. What is the exact type casting rule here when the base and the exponent are both Fixnum? I am particularly interested in Ruby 1.9.3, but is the result different among different versions?
DGM is right; the answer is right in the docs you linked, although it’s in C. Here is pertinent bit; I’ve added a few comments:
Now we can move on to the docs for Rational and check what it says about the
**operator: