std::setprecision(3)
π NAME
std::setprecision β literally just std::setprecision, bestie π (and yeah, this whole thing lives in the C++ Standard Libary β that's the manual name, no cap, spelling and all, we don't fix it, we respect it π«Ά)
β¨ Synopsis β¨
Okay so first things first, it's defined in the header <iomanip> π
/*unspecified*/ setprecision( int n );
And here's the whole vibe: when you use it in an expression like out << setprecision(n) or in >> setprecision(n), it sets the precision parameter of the stream out or in to exactly n. Exactly. Not approximately. Not ~vibes~. Exactly n π₯
π Parameters π
nβ the new value for precision. That's it, that's the parameter πββοΈ
π¦ Return value π¦
So it returns an object of unspecified type β mysterious, main character energy, we don't know what it is and honestly we don't need to π β such that:
- if
outis an object of typestd::basic_ostream<CharT, Traits>, the expressionout << setprecision(n)- has type
std::basic_ostream<CharT, Traits>& - has value
out - behaves as if it called
f(out, n)
- has type
- if
inis an object of typestd::basic_istream<CharT, Traits>, the expressionin >> setprecision(n)- has type
std::basic_istream<CharT, Traits>& - has value
in - behaves as if it called
f(in, n)
- has type
where the function f is defined as β¬οΈ
void f(std::ios_base& str, int n)
{
// set precision
str.precision(n);
}
Like, that's the whole function. It's giving "short but powerful" π
πΈ Example πΈ
// Run this code
#include <iomanip>
#include <iostream>
#include <limits>
#include <numbers>
int main()
{
constexpr long double pi{std::numbers::pi_v<long double>};
const auto default_precision{std::cout.precision()};
constexpr auto max_precision{std::numeric_limits<long double>::digits10 + 1};
std::cout << "default precision: " << default_precision << '\n'
<< "maximum precision: " << max_precision << "\n\n"
"precision: pi:\n";
for (int p{0}; p <= max_precision; ++p)
std::cout << std::setw(2) << p << " " << std::setprecision(p) << pi << '\n';
std::cout << std::setprecision(default_precision); // restore defaults
}
β Output β
default precision: 6
maximum precision: 19
precision: pi:
0 3
1 3
2 3.1
3 3.14
4 3.142
5 3.1416
6 3.14159
7 3.141593
8 3.1415927
9 3.14159265
10 3.141592654
11 3.1415926536
12 3.14159265359
13 3.14159265359
14 3.1415926535898
15 3.14159265358979
16 3.141592653589793
17 3.1415926535897932
18 3.14159265358979324
19 3.141592653589793239
Not the 13 and 14 rows both showing 3.14159265359 π yes bestie that's really the output, we don't edit it, we love it as it is π«Ά
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (Retroactively! Time travel! Iconic β³β¨)
- DR
LWG 183β applied to C++98- behavior as published:
setprecisioncould only be used with streams of typestd::ostreamorstd::istream - correct behavior: usable with any character stream π
- behavior as published:
π See also π
fixed,scientific,hexfloat(C++11),defaultfloat(C++11) β changes formatting used for floating-point I/O (function)precisionβ manages decimal precision of floating point operations (public member function ofstd::ios_base)
ok that's the whole page, you ate that π now go set some precision and slay β¨
source: http://cppreference.com Β· version/date: 2024.06.10 Β· std::setprecision(3) β mwah, bye bestie, see you in the next manpage πππ§
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms