### aritmetic operation about

The code:

#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:

0.xxxxxxxe-xxx.

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).

 ``1234567`` ``````#include #include using namespace std; int main() { cout << numeric_limits::epsilon() << endl; }`````` ```2.22045e-016 Press any key to continue . . .```

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.
Last edited on
Topic archived. No new replies allowed.