Ashley Pieterse
Ashley Pieterse

Reputation: 83

All digits not displaying in console after using float, double and long double

I'm still a novice at C++ and have looked around on here for answers to my questions, but all answers seem a bit too technical and confuse me even more. Let me first state that I know, from the C++ tut I'm using that float = 4 bytes with approx. 7 digits. Double =8 bytes with approx. 15 digits. And Long double=8 bytes with approx 15 digits.

Here is my problem: I create a simple sum and declare variables using float, double and long double. But my answer on the console after building and running does not show the correct amount of digits. For float it is working fine, but not for the other two. Example:

float sum;
sum = 9.123456 * 5;

Now when I build and run that, the answer on the console shows 1.82469 for float(which is fine since it is 7 digits including the "dot"). But for both double and long double it still shows 1.82469(7 digits) in my console when I run it.

So I have two questions:

  1. Why is my console always displaying 7 digits only?
  2. What's the point of using long double when double uses the exact same bytes(8) as long double, and also both allows for the same amount of digits(15)? Not sure if this is in any way relevant, but I am using the latest CodeBlocks with a GNU GCC compiler.

Upvotes: 1

Views: 309

Answers (1)

Alexey Birukov
Alexey Birukov

Reputation: 1680

1) cout << setprecision(15) 

2) C++ standard not specifies types fully. For example, "long" can be 32 or 64 bit. So, "long double" can be equal to "double" and may be not.

Upvotes: 1

Related Questions