0
#include <iostream>

int main()
{
    double a = 2 , b = 3;

    double answer = a/b;
    std::cout << answer;
    std::cout << std::to_string(answer);
}

Float or double I get : 0.666667;

I was expected more precision from the double ; How do i get something more like : 0.666666666666667

Edit : and i need to not lose the precision by using to_string;

  • Your stream controls what precision is displayed. [http://www.cplusplus.com/reference/iomanip/setprecision/](http://www.cplusplus.com/reference/iomanip/setprecision/) – drescherjm Jul 14 '20 at 02:03
  • Related to the new question: [https://stackoverflow.com/questions/16605967/set-precision-of-stdto-string-when-converting-floating-point-values](https://stackoverflow.com/questions/16605967/set-precision-of-stdto-string-when-converting-floating-point-values) – drescherjm Jul 14 '20 at 02:23

2 Answers2

2

I was expected more precision from the double

The precision of a floating point type is separate concept from the precision of a decimal number in textual representation.

You didn't specify the precision that you want, so default was used. It just so happens that the default isn't what you arbitrarily expected. You can specify the precision for example like this:

std::cout.precision(15);
eerorika
  • 232,697
  • 12
  • 197
  • 326
0

The setprecision is in the header file #include<iomanip> which will let you set the decimal precision.

For example:

#include <iostream>
#include <iomanip>  // The precision manipulator is in this header file 

int main()
{
    double a = 2 , b = 3;

    double answer = a/b;
    std::cout << std::setprecision(15) << answer;
    return 0;
}
 
Geno C
  • 1,401
  • 3
  • 11
  • 26