Project

General

Profile

Actions

Bug #5715

closed

+/-1 ** Bignum returns different results than +/-1 ** Fixnum

Added by john_firebaugh (John Firebaugh) over 12 years ago. Updated about 11 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]
Backport:
[ruby-core:41498]

Description

=begin
Consider (({1 ** expt})) and (({(-1) ** expt})).

When ((|expt|)) is a Fixnum, the result is always 1, -1, Rational(1/1), or Rational(-1,1) depending on the signs of the operands and parity of the exponent.

When ((|expt|)) is a Bignum, Float 1.0 is always returned. Either the behavior for Fixnum exponents should be followed exactly (preferred), or at the very least -1.0 should be returned when the base is -1 and the exponent is odd.

$VERBOSE = nil

wordsize = 8 * 1.size
fixnum_max = 2 ** (wordsize - 2) - 1
fixnum_min = -2 ** (wordsize - 2)

[1, -1].each do |a|
[ 1, 2, fixnum_max, fixnum_max + 1, fixnum_max + 2,
-1, -2, fixnum_min, fixnum_min - 1, fixnum_min - 2].each do |b|
puts "%5s ** %20s (%s) == %5s" % [a, b, b.class, a ** b]
end
end

Output:
1 ** 1 (Fixnum) == 1
1 ** 2 (Fixnum) == 1
1 ** 4611686018427387903 (Fixnum) == 1
1 ** 4611686018427387904 (Bignum) == 1
1 ** 4611686018427387905 (Bignum) == 1
1 ** -1 (Fixnum) == 1/1
1 ** -2 (Fixnum) == 1/1
1 ** -4611686018427387904 (Fixnum) == 1/1
1 ** -4611686018427387905 (Bignum) == 1.0 # Bug
1 ** -4611686018427387906 (Bignum) == 1.0 # Bug
-1 ** 1 (Fixnum) == -1
-1 ** 2 (Fixnum) == 1
-1 ** 4611686018427387903 (Fixnum) == -1
-1 ** 4611686018427387904 (Bignum) == 1
-1 ** 4611686018427387905 (Bignum) == -1
-1 ** -1 (Fixnum) == -1/1
-1 ** -2 (Fixnum) == 1/1
-1 ** -4611686018427387904 (Fixnum) == 1/1
-1 ** -4611686018427387905 (Bignum) == 1.0 # Bug
-1 ** -4611686018427387906 (Bignum) == 1.0 # Bug
=end


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #5713: Fixnum#** returns Infinity for 0 ** negative BignumClosedmarcandre (Marc-Andre Lafortune)12/06/2011Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0