starmole
starmole

Reputation: 5068

What does configuring Lua to float do to integers?

So one of the very cool things about Lua is that you can make it use floats instead of doubles for numbers. LUA_NUMBER in luaconf.h. It works very well for me!

But i always wondered how lua treats integers. In js and as3 I know integers are also still just "numbers". This works very well if numbers are always doubles with 53 mantissa digits, because the 32 bit people expect in integers are well covered. Now if one was to go to float with 23 mantissa digits this does no longer work as expected. Code often treats integers as 32 bits, for bitwise ops or things like packed 32 bit colors.

So my question is, if I build with "#define LUA_NUMBER float" is my integer limit 23 bits? Is the answer more complicated? If so, why and how? Also I never used luajit but how do other lua implementations deal with this?

I know I could just read the code but I think it's an interesting issue. And I am lazy :) For my use case I am fine with assuming 23 bits.

Upvotes: 4

Views: 459

Answers (1)

lhf
lhf

Reputation: 72402

Yes, the integers are limited to the size of numbers. So, if you use floats, you'll get 24 bits for integers (there is a hidden bit in floating-point representation).

Upvotes: 2

Related Questions