Floating point is, in practice, has a mantissa represented in binary (base 2). One consequence of this is that 0.1
decimal (aka the fraction 1/10
) cannot be exactly represented in floating point.
The reason is the same as 1/3
cannot be represented exactly in a fixed number of decimal places - it is an infinitely recurring value 0.3333333.....
, and truncating it to a finite number of places introduces an error. The only difference is that 1/10
is a infinitely recurring value in base 2. So it cannot be represented exactly in a finite number of binary digits.
There are obviously other values affected in such a way (such as 0.2
, 0.6
, etc). Such errors are an inherent property of floating point representations .... or, to put it another way, floating point represents an approximation (the value with a potential lack of precision). And, when doing a sequence of calculations, the errors in the values propagate.
Since there is no way to exactly represent the value 0.1
in floating point (and other values), there is no way to exactly store a floating point value into a database and get a value that is exactly 0.1
.
You can try printing the value in a way that limits the precision of output, such as
#include <sstream>
#include <iomanip>
#include <string>
std::ostringstream o;
o << std::fixed << std::setprecision(2) << result;
std::string s=o.str();
Bear in mind this controls how result
is formatted (e.g. on output to a string in this case). It does not change the value of result
.
Notionally, the string s
can be stored in your database .... if the field represents a string rather than a numeric value.