#include <iostream>
using namespace std;
int main() {
double a, b, c, d;
a = 1.38371e-20;
b = 4.12551e-19;
c = 1;
d = c - (a + b);
cout << scientific;
cout.precision(7);
cout << d << "\n";
if ( d == c )
cout << "(buuuu) d = c \n";
else
cout << "wow\n";
return 0;
}
returns
1.0000000e+00
(buuuu) d = c
How can I obtain the exact value for d (the result is different from 1). It should be:
You can increase the precision in your cout.precision() function. However, note that you may not be able to display all of those digits. The reason is that a double only has 64 bits of precision. Some of those are dedicated to the exponent, and the rest are dedicated to the mantissa (the coeffeicent in front of the exponent).
You can see the smallest number that can be shown by a double with the limits library. Epsilon is a number representing the value of the smallest bit in a floating point number of 1.0. Since c is coincidently 1.0, we can see that the smallest amount that can be displayed by 1 bit is 2.22e-16. That means that any operations with smaller numbers will have no effect on it!
So, due to limits in computer science, there is no EXACT value.