lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Apr 11, 2014 at 11:19 PM, Peng Zhicheng
<pengzhicheng1986@gmail.com> wrote:
> I can't agree.
>
> double precision floating point number has 56 precision bits, while long
> long has 64.

Actually it has 53, not 56.

> there is not much difference.
> if you need more than 56, double is not enough.
> what if you need more than 64? long long is not enough either.

It's quite a bit of difference -- more than three orders of magnitude.
Lots of wiggle room in there.

> speaking of correctness,  that `x == x+1' is of course incorrect.
> so is 'x ^ y == 0' where x is not zero.
>
> I am personally concerned about overflow more than losing precision.

Overflow can be dealt with, though. Something like this will work:

long long int accum = 1;
long long int willOverflow = LONGLONG_MAX / abs(lhs);

for(int i = 0; i < rhs; i++) {
    if(abs(accum) >= willOverflow) return pow(lhs, rhs); // fall back to FP
    accum *= lhs;
}
return accum;

Obviously it'll have to be fancier than that to actually be used in
Lua, detecting the number types and handling the return values on the
stack; think of this more as C-looking pseudocode.

/s/ Adam