Forum: Ruby-core [ruby-trunk - Bug #5715][Open] +/-1 ** Bignum returns different results than +/-1 ** Fixnum

Posted by John Firebaugh (Guest)
on 2011-12-06 05:27
(Received via mailing list)
Issue #5715 has been reported by John Firebaugh.

----------------------------------------
Bug #5715: +/-1 ** Bignum returns different results than +/-1 ** Fixnum
http://redmine.ruby-lang.org/issues/5715

Author: John Firebaugh
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]


=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
Posted by John Firebaugh (Guest)
on 2011-12-09 18:01
(Received via mailing list)
Issue #5715 has been updated by John Firebaugh.


The failing cases are the ones that go through the Rational(+/-1) ** 
Bignum code path, so this is closely related to #5713.
----------------------------------------
Bug #5715: +/-1 ** Bignum returns different results than +/-1 ** Fixnum
http://redmine.ruby-lang.org/issues/5715

Author: John Firebaugh
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]


=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
Posted by Marc-Andre Lafortune (Guest)
on 2011-12-09 19:45
(Received via mailing list)
Issue #5715 has been updated by Marc-Andre Lafortune.

Category set to core
Assignee set to Marc-Andre Lafortune

Just saw this update. Yes, as I stated, same issue as 5713. Yes the case 
for (-1)  and bignum exponents can be fixed too. The case for 1 and 0 
must be addressed for rational and float exponents too (but -1 will go 
to float for those)
----------------------------------------
Bug #5715: +/-1 ** Bignum returns different results than +/-1 ** Fixnum
http://redmine.ruby-lang.org/issues/5715

Author: John Firebaugh
Status: Open
Priority: Normal
Assignee: Marc-Andre Lafortune
Category: core
Target version:
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]


=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
Posted by marcandre (Marc-Andre Lafortune) (Guest)
on 2013-01-25 06:47
(Received via mailing list)
Issue #5715 has been updated by marcandre (Marc-Andre Lafortune).

Target version set to 2.0.0


----------------------------------------
Bug #5715: +/-1 ** Bignum returns different results than +/-1 ** Fixnum
https://bugs.ruby-lang.org/issues/5715#change-35620

Author: john_firebaugh (John Firebaugh)
Status: Assigned
Priority: Normal
Assignee: marcandre (Marc-Andre Lafortune)
Category: core
Target version: 2.0.0
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]


=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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.