Double variable not giving correct number


I'm learning C++, and have a problem I hope someone can explain to me.

I declare a double, enter a long number, and print it out. Look at the result below.

1
2
3
4
5
6
7
double a;

cout << endl << "Please enter a decimal number: " << endl;
cin >> a;

cout << fixed << a;


1
2
3
Please enter a decimal number:
639825673894678293674859267384962783964578923
639825673894678283193361275766842976939016192.000000


Why does it not give me back the same number?

---
Tr1978
I'm pretty sure most compilers don't have a type large enough to contain that number.

If you really need to use such large quantities, you might want to try one of the arbitrary precision libraries. GMP is very popular.
Last edited on
OP, I think it is best if you go to and read up on double values and how they are represented in a computer. A double is normally only stored in 32-bits so when you are working with large numbers an approximation of the number is used.

for more reading:

http://en.wikipedia.org/wiki/Floating_point title (Implementation in actual computers: IEEE floating-point)



doubles are usually 64 bits, not 32. floats are 32. nonetheless the explanation is accurate.
Topic archived. No new replies allowed.