5 ms·
What's the rationale for adding integers? I thought the rationale for not having them was to keep the language simpler, and integers 2^53 and less can be store
by benhoyt 12y ago
What's the rationale for adding integers? I thought the rationale for not having them was to keep the language simpler, and integers 2^53 and less can be store exactly as a (64-bit) float anyway. Is the rationale for adding them so Lua can have full support for 64-bit-wide integers and bitwise operations at that width?
- justincormack 12y agoYes. It is hard to interface with lots of C without 64 bit support. You can fudge it and hope it never happens but it is better to have full support.
- deleted 12y ago[deleted]
- ufo 12y agoIts one of the reasons. If you want more details you can check out this talk from Lua's main designer: video: https://www.youtube.com/watch?v=bjqNK1jA77M https://www.youtube.com/watch?v=bjqNK1jA77M slides: http://www.inf.puc-rio.br/~roberto/talks/ws2014.pdf http://www.inf.puc-rio.br/~roberto/talks/ws2014.pdf The main advantages of adding integers to Lua are: * For some things you need 2^64 bits, not just 2^53 (file handles, crypto algorithms with bitwise operations, etc) * Some embedded systems don't have hardware support for floating point (you could compile Lua with integers instead of double but it was very hacky) * It simplifies the C API. There are some things in the API and various internals in the implementation that benefit from separating integers and floating point numbers.
- benhoyt 12y agoThanks for the links -- slides were very helpful. This makes sense.
- speeder 12y agoAnother information from someone using Lua for years: Sometimes the hardware/OS/drivers/something else is buggy with floats... I once designed a game that needed integers for some things, and implemented it with Lua, on Windows machines it started to sometimes give VERY WRONG results (even in very short operations, like reporting that 5+5 == 11 or that 8+2 == 9, those are actual errors, not just short numbers to make typing easier). Later I found out it had to do with a infamous directX bug, where it changed the settings of the floating point units without permission, and sometimes this resulted into really, really, really funky floating point math, meaning that all math made with Lua in those cases maybe could be completely wrong. Back then when I found this (The issue, not the reason for it), many Lua devs kept telling me I as crazy or a troll, thankfully one guy in freenode.net #lua had the brilliance to tell me to check if it was the DX issue (and it was... the issue don't happened on Linux or OSX)
- jws 12y agoand integers 2^53 and less can be store exactly as a (64-bit) float anyway. Until you divide them by three. There is a time and a place for integer arithmetic. You could get there with liberal use of truncation, but it reads better as native integer arithmetic. The slope is slippery, time to resist or embrace native complex, rationals, and quaternions… I only stop the list here because it is as much as I've ever used in real world code.
- JadeNB 12y agoWhen did you use quaternions? (Genuinely curious!)
- dottrap 12y agoQuaternions are pretty common in 3D graphics.
- jws 12y agoMinecraft-like game I wrote with my daughter. I discovered their use the hard way after having suffered through without them. Any time you are freely rotating a viewpoint or an object in 3D space, you will probably want quaternions. Do write some good test cases though that compare to someone else's implementation. I accidentally wrote my multiplication backwards and didn't catch it for a long time. I just had a vague feeling that something was wrong every time I wrote rotation code.
- kephra 12y agoQuarternions are much easier then the typical vector arithmetic, that is used by OpenGL ES. Linden Scripting Language even provides quarternions conversion with llEuler2Rot, llRot2Euler and llAxisAngle2Rot. See: http://wiki.secondlife.com/wiki/Quaternion http://wiki.secondlife.com/wiki/Quaternion for an introduction. So quaternions are useful for 3d games, and games are a hot topic for Lua. But its not required, that complex numbers or quarternions are part of base language. See: http://lua-users.org/wiki/ComplexNumbers http://lua-users.org/wiki/ComplexNumbers - thats easy to implement with Lua prototyping.
- swift 12y ago
- dottrap 12y ago53 bits (double) is not enough to represent 64-bit integers. It was enough for 32-bit integers, but not 64-bit. There are two main reasons: - Modern systems are now 64-bit and they sometimes have APIs that demand 64-bit integers. (For example, file APIs that deal in bytes like fread). - Small, embedded systems that are 32-bit or less, which don't have good floating point hardware and are RAM constrained. Lua 5.3 now means double no longer has to be used to represent 32-bit integers. Instead integers can be directly (avoiding floating point for performance) and floating point sizes can be reduced down to float instead of double (reducing the size of internal objects). Roberto Ierusalimschy (Lua creator) has given several talks about why bring integers to Lua 5.3. https://www.youtube.com/watch?v=bjqNK1jA77M https://www.youtube.com/watch?v=bjqNK1jA77M
- tavish1 12y agoOne reason would be, as already mentioned, about embedded systems not having floating point support, making number calculations slow(even with fixed point, maybe?). A somewhat related thing(not addressed by this yet) is about Electric Imp, who had to switch to a different language from lua(which they started with). The reason was that they needed to store values for an LED array, and the only data type is tables, and all integers are 32 bit. Not sure but they might have used user defined types, with the arrays stuff being done in C, might have been easier.