3 ms·
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. It also means that new programm
by mort96 8y 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. 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 8y 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 8y 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 8y 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 8y 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 8y 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.