std::fegetr...::fesetround(3)
π 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. β¨
fesetroundattempts to establish the floating-point rounding direction equal to the argumentround, which is expected to be one of the floating point rounding macros.fegetroundreturns 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 β¨
0on success, non-zero otherwise.- 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
- 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