std::fegetr...::fesetround(3)

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

πŸ’– NAME πŸ’–

okay so this is the std::fegetr...::fesetround(3) page from the C++ Standard Libary manual β€” yes, β€œLibary”, that’s the name up top, we’re keeping it. It’s all about std::fegetround,std::fesetround, and the NAME line is literally:

  • std::fegetround,std::fesetround β€” std::fegetround,std::fesetround

no cap, that’s the whole NAME vibe. 🫢


πŸŒ€ Synopsis πŸŒ€

Defined in header <cfenv> β€” that’s the include, bestie.

int fesetround( int round ) (1) (since C++11)
int fegetround()            (2) (since C++11)

This pair manages the floating-point rounding direction. It’s giving tiny functions, huge control over rounding. ✨

  1. fesetround attempts to establish the floating-point rounding direction equal to the argument round, which is expected to be one of the floating point rounding macros.
  2. fegetround returns the value of the floating point rounding macro that corresponds to the current rounding direction.

🌸 Parameters 🌸

  • round β€” rounding direction, one of floating point rounding macros

✨ Return value ✨

  1. 0 on success, non-zero otherwise.
  2. The floating point rounding macro describing the current rounding direction, or a negative value if the direction cannot be determined.

πŸ¦‹ Notes πŸ¦‹

The current rounding mode β€” reflecting the effects of the most recent fesetround β€” can also be queried with FLT_ROUNDS.

See floating-point rounding macros for the effects of rounding. iykyk. πŸ’…


πŸŽ€ Example πŸŽ€

The page says // Run this code, and then hits you with this:

#include <cfenv>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <utility>
// #pragma STDC FENV_ACCESS ON

int main()
{
    static constexpr std::pair<const char*, const double> samples[]
    {
        {" 12.0", 12.0},  {" 12.1", 12.1}, {"-12.1", -12.1}, {" 12.5", 12.5},
        {"-12.5", -12.5}, {" 12.9", 12.9}, {"-12.9", -12.9}, {" 13.0", 13.0}
    };

    std::cout <<
        "β”‚ sample β”‚  FE_DOWNWARD  β”‚   FE_UPWARD   β”‚ FE_TONEAREST  β”‚ FE_TOWARDZERO β”‚\n";

    for (const auto& [str, fp] : samples)
    {
        std::cout << "β”‚ " << std::setw(6) << str << " β”‚  ";
        for (const int dir : {FE_DOWNWARD, FE_UPWARD, FE_TONEAREST, FE_TOWARDZERO})
        {
            std::fesetround(dir);
            std::cout << std::setw(10) << std::fixed << std::nearbyint(fp) << "   β”‚  ";
        }
        std::cout << '\n';
    }
}

πŸ”₯ Output πŸ”₯

Output:

β”‚ sample β”‚  FE_DOWNWARD  β”‚   FE_UPWARD   β”‚ FE_TONEAREST  β”‚ FE_TOWARDZERO β”‚
β”‚   12.0 β”‚   12.000000   β”‚   12.000000   β”‚   12.000000   β”‚   12.000000   β”‚
β”‚   12.1 β”‚   12.000000   β”‚   13.000000   β”‚   12.000000   β”‚   12.000000   β”‚
β”‚  -12.1 β”‚  -13.000000   β”‚  -12.000000   β”‚  -12.000000   β”‚  -12.000000   β”‚
β”‚   12.5 β”‚   12.000000   β”‚   13.000000   β”‚   12.000000   β”‚   12.000000   β”‚
β”‚  -12.5 β”‚  -13.000000   β”‚  -12.000000   β”‚  -12.000000   β”‚  -12.000000   β”‚
β”‚   12.9 β”‚   12.000000   β”‚   13.000000   β”‚   13.000000   β”‚   12.000000   β”‚
β”‚  -12.9 β”‚  -13.000000   β”‚  -12.000000   β”‚  -13.000000   β”‚  -12.000000   β”‚
β”‚   13.0 β”‚   13.000000   β”‚   13.000000   β”‚   13.000000   β”‚   13.000000   β”‚

πŸ‘‘ See also πŸ‘‘

  • nearbyint, nearbyintf, nearbyintl β€” nearest integer using current rounding mode; (C++11), (function), (C++11), (C++11)
  • rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl β€” nearest integer using current rounding mode with exception if the result differs; (C++11), (function), (C++11), (C++11), (C++11), (C++11), (C++11), (C++11), (C++11), (C++11), (C++11)
  • C documentation for fegetround, fesetround

that’s the whole page, bestie 🫢 huge kiss from the source http://cppreference.com, version/date 2024.06.10, page std::fegetr...::fesetround(3). go round some floats responsibly πŸ’…

πŸ“¦ where this page comes from