lua-users home
lua-l archive

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


> string.unpack("I8", "\255\255\255\255\255\255\255\127")
9223372036854775807     9
> string.unpack("I8", "\255\255\255\255\255\255\255\128")
-9151314442816847873    9

with the increment on the last byte of 8 (for a 64-bit integer in
two's-complement encoding), you're incrementing the most significant
byte (where the sign is the most significant bit). So:
- this correctly changes from positive to negative
- the actual increment on the integer is not 1 but a huge value (so
the difference of absiute magnitude is not 1)

Retry with:
> string.unpack("I8", "\255\255\255\255\255\255\255\127")
9223372036854775807     9
> string.unpack("I8", "\0\0\0\0\0\0\0\128")
-9223372036854775808     9

Le jeu. 12 mars 2020 à 18:53, dyngeccetor8 <dyngeccetor8@disroot.org> a écrit :
>
> On 12/03/2020 20.06, cherue wrote:
> > Hello,
> >
> > I found a bug in string.unpack:
> >
> > ```
> > Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> >> string.unpack("I4", "\255\255\255\255")
> > 4294967295    5
> >> string.unpack("I8", "\255\255\255\255\255\255\255\255")
> > -1    9
> >> string.unpack("I8", "\255\255\255\255\255\255\255\127")
> > 9223372036854775807   9
> >> string.unpack("I8", "\255\255\255\255\255\255\255\128")
> > -9151314442816847873  9
> > ```
> >
> > I tried looking for the cause but I am not fluent in C. Maybe something about lua_Integer?
> >
>
> So where is the bug?
>
> In my point of view you've got quite consistent results
> for two's-complement little-endian integer encoding.
>
> -- Martin
>