4 ms·
> For those curious, Lua handles all numbers as floating-point; there are no ints in Lua. Same as in JavaScript. I'm surprised by that. On the surface, having
by TeMPOraL 9y ago
> For those curious, Lua handles all numbers as floating-point; there are no ints in Lua.
Same as in JavaScript. I'm surprised by that. On the surface, having just floats and no ints sounds like a pretty dumb idea. What am I missing? Is there a reasonable rationale for such choice?
- justincormack 9y agoThere are ints in Lua 5.3, and ffi ints in luajit. But generally, its simpler, why should people care about representations. Lua also let you compike as int only I think.
- TeMPOraL 9y ago> why should people care about representations Because ints are exact, while floats introduce errors with nearly every operation. Representations are important in programming - different ones have different consequences.
- mort96 9y agoWell, doubles can represent all the numbers a 52 bit int can, so there's not a huge reason to not use doubles for whole numbers. It also means that new programmers don't get confused when they do 1/3 and get 1, and the language can be significantly simpler when there's only one number type. Also, when you try to store numbers bigger than 2^52, doubles degrade relatively gracefully, while ints wrap around. There are definitely downsides too, but it's a trade-off, not an exclusively bad decision.
- imron 9y ago> It also means that new programmers don't get confused when they do 1/3 and get 1, I'm not a new programmer (nearly 2 decades of professional programming experience), and I would be very confused if 1/3 using integer math returned 1.
- TeMPOraL 9y ago> Well, doubles can represent all the numbers a 52 bit int can, so there's not a huge reason to not use doubles for whole numbers. Not exactly. > It also means that new programmers don't get confused when they do 1/3 and get 1 Is this (actually, getting 0 from 1/3) really more surprising than doing 2+2 and getting 3.9999999999999? Floats are mostly inexact and introduce errors with almost every operation. It's something you have to constantly keep in mind when doing any kind of math with meaningful comsequences. I for one think that exact types are both more useful for many practical cases, as well as significantly simpler in use.
- fasquoika 9y ago> Is this (actually, getting 0 from 1/3) really more surprising than doing 2+2 and getting 3.9999999999999? No, so it's a good thing doubles never do anything remotely like that
- samatman 9y ago> > Well, doubles can represent all the numbers a 52 bit int can, so there's not a huge reason to not use doubles for whole numbers. > Not exactly. Yes, exactly [0]. The 52 bit significand gives LuaJIT and other NaN-tagged languages exact integer representations within that range. [0]: https://en.wikipedia.org/wiki/Floating-point_arithmetic#Internal_representation https://en.wikipedia.org/wiki/Floating-point_arithmetic#Inte...
- ori_b 9y agoAll floating point integer operations with values smaller than 252 are exact. Most floating point operations with small values with few significant figures are precise. Fraction, and there are fractions like 1/10 that cannot be represented precisely in base two, much like 1/3 cannot be represented precisely in base 10. Beyond that, there's the usual issue of pushing out the least significant bits, but that's insurmountable while working in finite precision.
- nneonneo 9y agoI think you meant 2^52, not 252.
- dvlsg 9y agoJavascript is likely to get 64 bit integers soon. It's busy making its way through TC39, anyways.