4 ms·
> "The problem this has is that the number 4 might be stored as 3.999999992 and if you cast that to an int naively you could end up with the integer value being
by zvrba 14y ago
> "The problem this has is that the number 4 might be stored as 3.999999992 and if you cast that to an int naively you could end up with the integer value being 3 rather than 4. "
No it might not. The author completely misunderstands FP.
- MikeDucker 14y agoAre you saying that a floating point value just under 4 won't necessarily cast to 3 or something else? (I'm reading the above posted links, but am intrigued at what I've gotten wrong with my statement you quoted)
- monochromatic 14y ago4 is represented exactly. (But 0.4 isn't.)
- MikeDucker 14y agoAh, I see. I'll update my post. Thanks.
- scott_s 14y agoMore pernicious is you may think that the result of a computation should be an exact number, but it may not be. Consider: #include "stdio.h" int main() { double num = 3.0; unsigned long long i; for (i = 0; i < 1000000000; ++i) { num += 1e-9; } printf("%f\n", num); printf("%llx\n", *((unsigned long long*)&num)); double four = 4.0; printf("%f\n", four); printf("%llx\n", *((unsigned long long*)&four)); return 0; } And then executed, [scott_s@local ~] ./float 4.000000 40100000058d7800 4.000000 4010000000000000