3
double fat = 0.2654654645486684646846865584656566554566556564654654899866223625564668186456564564664564;
cout<<fat<<endl;

results in:

0.265465

Should it be 7 charcters longer? I thought that a double could hold more than that?

I also get the same result of a "long double".

John Dibling
  • 99,718
  • 31
  • 186
  • 324
Greg G
  • 697
  • 2
  • 8
  • 17

5 Answers5

10

You're just seeing the default precision used by an iostream.

To improve things, use std::setprecision().

const int max_digits = std::numeric_limits<double>::digits10;

std::cout << std::setprecision(max_digits) << fat << std::endl;
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
3

Use std::setprecision(std::numeric_limits<double>::digits10) for maximum precision

std::cout << std::setprecision(std::numeric_limits<double>::digits10) << fat << std::endl;
David
  • 27,652
  • 18
  • 89
  • 138
3

There are two issues here:

  • you only get 7 significant figures because your cout stream is defaulting to a precision of 7, as the other answers state you can increase this to std::numeric_limits<double>::digits10

  • double can only store a fixed amount of precision anyway so most of the digits assigned to fat will be thrown away (on most machines you will get up to 15 significant figures into a double)

jk.
  • 13,817
  • 5
  • 37
  • 50
0

The problem is with cout, which defaults to a certain number of decimal places. You can set cout's precision and then add a fixed to give you the precision you need

cout.precision(15);
cout << fixed << fat << endl;
Hans Z
  • 4,664
  • 2
  • 27
  • 50
0

use cout.precision().
or, you can also use std::numeric_limits< double > and #include <limits> to get the maximum precision of a float or double..
see this SO post.

Community
  • 1
  • 1
Eight
  • 4,194
  • 5
  • 30
  • 51