Inaccuracy is inevitable with any FP variable.
That's why I don't like it.
|
This is going to sound like the glass half-full vs half-empty expression, but it really is more than that.
You should not look at floating point numbers as being inaccurate but rather look at them as simply being 'accurate enough'.
For the purposes FP types are meant to serve, that is the requirement they are meant to meet.
This is why. You rarerly get what you expect to with floating point arithmetic because it's inaccurate. |
If you understand both how FP types work,
and how they should be used, there is absolutely no reason you should not always get what you expect. Floats and doubles are not some mystical magical data types that defy reality. They only appear that way to the people that expect them to be nothing more than decimal versions of ints.
In short: there is an infinite amount of floating point numbers between each integer number. |
And that there represents a fundamental misunderstanding of floating point numbers.
There are actually a finite amount of values any floating point type can represent. To be exact, 4,294,967,296 possible finite values for a C++ float.
What you probably
meant to say was there is an infinite amount of
real numbers between each integer number.
I'm not nitpicking here. There is more than just a semantic difference between those two statements, and that difference is in how floating point data types represent real numbers, chiefly the inherent issues with storing real numbers in what is ultimately a finite binary format.