Bug #5713
Fixnum#** returns Infinity for 0 ** negative Bignum
| Status: | Assigned | Start date: | 12/06/2011 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | core | |||
| Target version: | 2.0.0 | |||
| ruby -v: | ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0] |
Description
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)}
Related issues
History
Updated by marcandre (Marc-Andre Lafortune) 6 months ago
- Assignee set to marcandre (Marc-Andre Lafortune)
Updated by john_firebaugh (John Firebaugh) 6 months 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.
Updated by marcandre (Marc-Andre Lafortune) 6 months ago
- Category set to core
- Target version set to 2.0.0
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)
Updated by john_firebaugh (John Firebaugh) 6 months ago
FYI, I've been doing RubySpec work on this in Rubinius: https://github.com/rubinius/rubinius/commits/master/spec/ruby/shared/rational/exponent.rb
Updated by shyouhei (Shyouhei Urabe) 2 months ago
- Status changed from Open to Assigned