[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: bug in ceillog2 macro (Lua-5.1.4)
- From: Leo Razoumov <slonik.az@...>
- Date: Fri, 7 Jan 2011 11:03:31 -0500
On Fri, Jan 7, 2011 at 10:44, Roberto Ierusalimschy
>> Hi List,
>> In Lua-5.1.4 macro
>> #define ceillog2(x) (luaO_log2((x)-1) + 1)
>> gives wrong answer when x is 0.
>> ceillog2(0) => 32 (should be 1).
>> I attached below a standalone code snippet to demonstrate the problem.
>> I use Lua-5.1.4 on Ubuntu-9.04 x86 (32 bits).
>> There are several ways to fix it and I like _none_ of them.
>> (1) Ignore the bug. Just make sure that x>0 always.
> This is an internal function; it is not for general use. It cannot be
> called with a zero argument (see the calling points). It should not be
> called with a zero argument, because math.ceil(math.log(0, 2)) is -inf
> Where is the bug?
> -- Roberto
Actually, luaO_log2(unsigned int x) is well defined when x is zero
luaO_log2(0) => -1
Yes, all the existing calling points carefully ensure that x is never
zero. Therefore, word "bug" is not appropriate here, indeed.
But calling ceillog2(0) will lead to surprises.
Lua implementation is very clean and provides a good example of
general algorithms which are usable outside Lua. It was my primary
interest in luaO_log2 implementation which lead to the ceillog2 issues
(unsigned x in luaO_log2 and signed x in ceillog2 calls).