Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the role of **std::setprecision()** without **std::fixed** in c++?

As shown in the tutorial http://www.cplusplus.com/reference/iomanip/setprecision/

// setprecision example
#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision

int main () {
  double f =3.14159;
  std::cout << std::setprecision(5) << f << '\n';  // prints 3.1416 and not 3.141459 why 
  std::cout << std::setprecision(9) << f << '\n';
  std::cout << std::fixed;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';
  return 0;
}

The line std::cout << std::setprecision(5) does not print 5 decimal digits but after std::fixed is set, the setprecision works as expected. Why is that ?.

What is the role of std::setprecision() without std::fixed ?

like image 924
Talespin_Kit Avatar asked Jul 13 '17 14:07

Talespin_Kit


4 Answers

Behaviour of std::setprecision() differs depending on the chosen formatting.

std::fixed make std::setprecision() refer to how many digits are printed after the decimal point. Before you change the default formatting to std::fixed, std::defaultfloat is set, and std::setprecision() sets the total number of digits to be printed, including the ones both before and after the decimal point.

Compare:

http://www.cplusplus.com/reference/ios/defaultfloat/

http://www.cplusplus.com/reference/ios/fixed/

like image 94
KjMag Avatar answered Oct 17 '22 08:10

KjMag


According to http://en.cppreference.com/w/cpp/io/ios_base/precision, the precision decides how many digits are printed, not how many digits after the floating point are printed:

std::ios_base::precision

Manages the precision (i.e. how many digits are generated) of floating point output

This explains the rounding.

Yes, using std::fixed will change the interpretation of the floatfield precision. According to http://www.cplusplus.com/reference/ios/ios_base/precision/:

In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.

like image 26
Fabien Avatar answered Oct 17 '22 08:10

Fabien


The meaning of precision depends on whether fixed, scientific or default format is used. For fixed format, it's the number of digits after the decimal point. For scientific, it is, too - but there's always exactly one digit before the point; the exponent is used to shift the decimal point into position. For default format, precision specifies the total number of digits printed (roughly; it's a bit more complicated).

For details, see http://en.cppreference.com/w/cpp/io/c/fprintf , in particular the description of %f, %e and %g format specifiers (for fixed, scientific and default format, correspondingly). std::cout << f is specified to behave as if printf("%.<precision><format>", f) is called (to the first approximation; the reality is, again, a bit more complicated, but the details are not relevant to the issue at hand), where <precision> is the number specified by setprecision, and <format> is one of f, e or g.

like image 42
Igor Tandetnik Avatar answered Oct 17 '22 08:10

Igor Tandetnik


This is because "When floatfield is set to fixed, floating-point values are written using fixed-point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part." ref. http://www.cplusplus.com/reference/ios/fixed/

like image 39
xGedaJastox Avatar answered Oct 17 '22 06:10

xGedaJastox