DGM 是对的;答案在您链接的文档中,尽管它在 C 中。这是相关的位;我添加了一些评论:
static VALUE
fix_pow(VALUE x, VALUE y)
{
long a = FIX2LONG(x);
if (FIXNUM_P(y)) { // checks to see if Y is a Fixnum
long b = FIX2LONG(y);
if (b < 0)
// if b is less than zero, convert x into a Rational
// and call ** on it and 1 over y
// (this is how you raise to a negative power).
return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
现在我们可以继续阅读 Rational 的文档并**
检查它对操作符的描述:
rat ** numeric → numeric
执行幂运算。
例如:
Rational(2) ** Rational(3) #=> (8/1)
Rational(10) ** -2 #=> (1/100)
Rational(10) ** -2.0 #=> 0.01
Rational(-4) ** Rational(1,2) #=> (1.2246063538223773e-16+2.0i)
Rational(1, 2) ** 0 #=> (1/1)
Rational(1, 2) ** 0.0 #=> 1.0