Bug #5713

Fixnum#** returns Infinity for 0 ** negative Bignum

Added by John Firebaugh about 3 years ago. Updated about 2 years ago.

[ruby-core:41494]
Status:Closed
Priority:Normal
Assignee:Marc-Andre Lafortune
ruby -v:ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0] Backport:

Description

=begin
Instead it should raise ZeroDivisionError, the same as negative Fixnums.

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

def zero_power(exp)
0 ** exp
rescue ZeroDivisionError
"ZeroDivisionError"
end

[-1, fixnum_min, (fixnum_min-1)].each {|i| puts zero_power(i)}
=end


Related issues

Related to Ruby trunk - Bug #5715: +/-1 ** Bignum returns different results than +/-1 ** Fixnum Closed 12/06/2011

Associated revisions

Revision 39063
Added by Marc-Andre Lafortune about 2 years ago

  • rational.c (nurat_expt): Deal with special cases for rationals 0, ±1 [bug #5713] [bug #5715]

Revision 39063
Added by Marc-Andre Lafortune about 2 years ago

  • rational.c (nurat_expt): Deal with special cases for rationals 0, ±1 [bug #5713] [bug #5715]

Revision 39064
Added by Marc-Andre Lafortune about 2 years ago

  • numeric.c (fix_pow): Handle special cases when base is 0, -1 or +1 [Bug #5713] [Bug #5715]

Revision 39064
Added by Marc-Andre Lafortune about 2 years ago

  • numeric.c (fix_pow): Handle special cases when base is 0, -1 or +1 [Bug #5713] [Bug #5715]

History

#1 Updated by Marc-Andre Lafortune about 3 years ago

  • Assignee set to Marc-Andre Lafortune

#2 Updated by John Firebaugh about 3 years ago

The execution path of 0 ** -Bignum goes to Rational(0) ** -Bignum, so I think the issue is there. I.e. Rational(0) ** -Bignum should raise ZeroDivisionError, the same as Rational(0) ** -Fixnum.

#3 Updated by Marc-Andre Lafortune about 3 years ago

  • Target version set to 2.0.0
  • Category set to core

John Firebaugh wrote:

The execution path of 0 ** -Bignum goes to Rational(0) ** -Bignum, so I think the issue is there. I.e. Rational(0) ** -Bignum should raise ZeroDivisionError, the same as Rational(0) ** -Fixnum.

Yes, unless there is objection, Rational#** should treat 0 and 1 as special cases before resorting to conversion to float, i.e.

Rational(0) ** (-2**100) # => Infinity, should raise an Error
Rational(0) ** (2**100) # => 0.0, should be Rational(0)
Rational(1) ** (2**100) # => 1.0, should be Rational(1)

#5 Updated by Shyouhei Urabe almost 3 years ago

  • Status changed from Open to Assigned

#6 Updated by Marc-Andre Lafortune about 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r39063.
John, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • rational.c (nurat_expt): Deal with special cases for rationals 0, ±1 [bug #5713] [bug #5715]

Also available in: Atom PDF