Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 在Fixnum**Fixnum中键入casting_Ruby_Math_Exponent_Ruby 1.9.3 - Fatal编程技术网

Ruby 在Fixnum**Fixnum中键入casting

Ruby 在Fixnum**Fixnum中键入casting,ruby,math,exponent,ruby-1.9.3,Ruby,Math,Exponent,Ruby 1.9.3,在中,它表示**可能是小数,并给出了示例: 2 ** -1 #=> 0.5 2 ** 0.5 #=> 1.4142135623731 但在我的irb中,它有时给出一个Rational答案,就像下面的指数-1一样: 2 ** -1 #=> (1/2) 2 ** 0.5 #=> 1.4142135623731 看起来ruby文档不准确,ruby尝试在可能的情况下键入cast toRational,但我不能完全确定。当基和指数都是Fixnum时,这里的确切类型转换规则是什么

在中,它表示
**
可能是小数,并给出了示例:

2 ** -1 #=> 0.5
2 ** 0.5 #=> 1.4142135623731
但在我的irb中,它有时给出一个
Rational
答案,就像下面的指数
-1
一样:

2 ** -1 #=> (1/2)
2 ** 0.5 #=> 1.4142135623731

看起来ruby文档不准确,ruby尝试在可能的情况下键入cast to
Rational
,但我不能完全确定。当基和指数都是Fixnum时,这里的确切类型转换规则是什么?我对Ruby 1.9.3特别感兴趣,但不同版本的结果是否不同?

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(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

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(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

在您链接的文档中,您可以阅读源代码并自行查看…我不习惯Ruby的C部分。在您链接的文档中,您可以阅读源代码并自行查看…我不习惯Ruby的C部分。谢谢。我不习惯Ruby的C部分,虽然我理解C。我本可以花时间阅读它,但你的回答节省了我的时间,让我更清楚。阅读Ruby C肯定需要一点练习。有很多宏--
Ruby.h
handy是件好事!谢谢我不习惯Ruby的C部分,虽然我理解C。我本可以花时间阅读它,但你的回答节省了我的时间,让我更清楚。阅读Ruby C肯定需要一点练习。有很多宏--
Ruby.h
handy是件好事!