简体   繁体   中英

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM