instance method
Integer#**
(::Integer) -> ::Numeric
(::Float) -> ::Numeric
(::Rational) -> ::Numeric
(::Complex) -> ::Complex
self ** exponent → numeric
numeric.c
View on GitHub ↗
VALUE
rb_int_pow(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_pow(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_pow(x, y);
}
return Qnil;
}
Returns self raised to the power exponent:
Example
# Result for non-negative Integer exponent is Integer. 2 ** 0 # => 1 2 ** 1 # => 2 2 ** 2 # => 4 2 ** 3 # => 8 -2 ** 3 # => -8 # Result for negative Integer exponent is Rational, not Float. 2 ** -3 # => (1/8) -2 ** -3 # => (-1/8) # Result for Float exponent is Float. 2 ** 0.0 # => 1.0 2 ** 1.0 # => 2.0 2 ** 2.0 # => 4.0 2 ** 3.0 # => 8.0 -2 ** 3.0 # => -8.0 2 ** -3.0 # => 0.125 -2 ** -3.0 # => -0.125 # Result for non-negative Complex exponent is Complex with Integer parts. 2 ** Complex(0, 0) # => (1+0i) 2 ** Complex(1, 0) # => (2+0i) 2 ** Complex(2, 0) # => (4+0i) 2 ** Complex(3, 0) # => (8+0i) -2 ** Complex(3, 0) # => (-8+0i) # Result for negative Complex exponent is Complex with Rational parts. 2 ** Complex(-3, 0) # => ((1/8)+(0/1)*i) -2 ** Complex(-3, 0) # => ((-1/8)+(0/1)*i) # Result for Rational exponent is Rational. 2 ** Rational(0, 1) # => (1/1) 2 ** Rational(1, 1) # => (2/1) 2 ** Rational(2, 1) # => (4/1) 2 ** Rational(3, 1) # => (8/1) -2 ** Rational(3, 1) # => (-8/1) 2 ** Rational(-3, 1) # => (1/8) -2 ** Rational(-3, 1) # => (-1/8)
Ruby version changes
-
3.4
-
Integer#**used to returnFloat::INFINITYwhen the return value is large, but now returns anInteger. If the return value is extremely large, it raises an exception. [Feature #20811]
-