std::ios_base::precision(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

std::ios_base::precision β€” okay so this is literally the function that manages std::ios_base::precision, no cap. It's the one from the C++ Standard Libary manual (yes bestie that's how they spelled it, I'm not fixing it, I don't make the rules πŸŽ€).


✨ Synopsis ✨

streamsize precision() const;                     (1)
streamsize precision( streamsize new_precision ); (2)

So picture this: precision is the vibe manager for how many digits show up when you print out floating point stuff. Specifically, it controls the precision (i.e. how many digits are generated) of floating point output performed by std::num_put::do_put. That's the whole job. Slay.

  1. This overload just returns the current precision. It's a little read-only bestie, it doesn't change anything, it just tells you the tea. 🫢
  2. This one sets the precision to the given one. And the cute part? It returns the previous precision β€” so you get the old number back as a lil souvenir. πŸ’–

The default precision, as established by std::basic_ios::init, is 6. Six. That's the starting point before you touch anything. Obsessed.


🌸 Parameters 🌸

  • new_precision β€” the new precision setting. This is the number you're handing over. That's it, that's the parameter.

πŸ¦‹ Return value πŸ¦‹

The precision before the call to the function. So it's like, the past version of the precision, returned to you. Time travel but make it integer. ⭐


πŸ”₯ Example πŸ”₯

// Run this code

#include <iostream>

int main()
{
    const double d = 12.345678901234;
    std::cout << "The  default precision is " << std::cout.precision() << "\n\n";
    std::cout << "With default precision d is " << d << '\n';
    std::cout.precision(8);
    std::cout << "With high    precision d is " << d << '\n';
}

🌈 Output

The  default precision is 6

With default precision d is 12.3457
With high    precision d is 12.345679

See how the first one only gave us 12.3457 and then we bumped it to 8 and suddenly we got 12.345679? Literally glow-up behavior. The precision said "watch this" and ate. πŸ‘‘


πŸ’€ Defect reports πŸ’€

Okay so the following behavior-changing defect reports were applied retroactively to previously published C++ standards. Which means yes, they went back and fixed old stuff, iconic of them honestly:

  • DR: LWG 189 β€” Applied to: C++98 β€” Behavior as published: 'precision' was defined as 'the number of digits after the decimal point', but it is not correct in some cases β€” Correct behavior: corrected. 😭

So yeah, the old wording was lowkey wrong in some cases, and they corrected it. Fixed. Periodt.


🧚 See also 🧚

  • width β€” manages field width (public member function)
  • setprecision β€” changes floating-point precision (function)

That's the whole page bestie, go forth and format your floats πŸ’œ

Shoutout to http://cppreference.com, the 2024.06.10 crew, and the page itself: std::ios_base::precision(3). Mwah. πŸ’…βœ¨
πŸ“¦ where this page comes from