-3

I would like to improve the precision of a double. It seems the precision of double and float is the same. (I also need a better precision for my future c++ plans.)

I have no clue how to do this.

#include <iostream>

using namespace std;

int main()
{
    double fraction1 = 0.123467890123456789;
    float fraction2 = 0.123467890123456789;
    cout << fraction1 << endl;
    cout << fraction2 << endl;
    return 0;
}

This yields:

0.123468
0.123468

I would have expected something like a precision of 10 or more digits.

Cat lover
  • 19
  • 4
  • 1
    Those variables store more digits than `cout` prints by default. Try [`std::setprecision`](https://en.cppreference.com/w/cpp/io/manip/setprecision). – HolyBlackCat Dec 27 '18 at 21:46
  • 6
    There is a big difference between the precision of a real number and how that number is displayed. –  Dec 27 '18 at 21:47
  • Other possible duplicates: https://stackoverflow.com/q/1231685/1896169 , https://stackoverflow.com/q/554063/1896169 , https://stackoverflow.com/q/4217510/1896169 – Justin Dec 27 '18 at 21:50
  • 1
    You should also look into [`std::numeric_limits::max_digits10`](https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10) –  Dec 27 '18 at 21:50

1 Answers1

1

No. You are observing the default output precision of an ostream, not the precision of a double.

Use std::setprecision to get the output you want.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055