While studying about float datatypes I wrote a program in C++:
float a = 0.3, b = 0.4, c = 0.7;
cout << "a+b= " << (a + b) << endl;
if ((a + b) == c)
{
cout << "Success..." << endl;
}
else
{
cout << "Failure..." << endl;
}
The output I received was:
a+b= 0.7
Failure...
I am using visual studio code as my IDE and it facilitates that on hovering the cursor over a declared variable it shows the approximate value assigned to the variable and I realized that for a it was 0.2999999999999999889
, for b:0.4000000000000000222
and for c:0.6999999999999999556
.
So, my question is that is there a method/a data type which can store the true value of a floating point. And why does the system stores these values in approximate form rather than in the true value.