1

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.
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
Ashley Pieterse
  • 85
  • 1
  • 1
  • 10
  • 5
    You forgot to include your code in the question. – Paul R Feb 02 '15 at 13:30
  • 1
    possible duplicate of [How to change/control the output precision in gcc or g++?](http://stackoverflow.com/questions/27940723/how-to-change-control-the-output-precision-in-gcc-or-g) – Quentin Feb 02 '15 at 13:31
  • 2
    possible duplicate of [How do I print a double value with full precision using cout?](http://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout) – Öö Tiib Feb 02 '15 at 13:32
  • Thanks Cyber for splitting that brick ! – Quentin Feb 02 '15 at 13:33
  • The radix point is not a significant digit, hence not counting in the "precision" of the value. Also note that 9.123456 is a double literal, so the operation will be done in double precision before casting down to float. You need `9.123456f` to get the float value – phuclv Sep 01 '15 at 09:34

1 Answers1

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

Alexey Birukov
  • 1,565
  • 15
  • 22