jeudi 13 juillet 2017

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

As shown in the tutorial http://ift.tt/1AFjaLe

// 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 ?

Aucun commentaire:

Enregistrer un commentaire