#include <iostream> #include <string> #include <cfenv> struct raii_feround { raii_feround() : value(std::fegetround()) { } ~raii_feround() { std::fesetround(value); } inline void round_up () const noexcept { std::fesetround(FE_UPWARD ); } inline void round_down() const noexcept { std::fesetround(FE_DOWNWARD); } template<typename T> T add(T fst, T snd) const noexcept { return fst + snd; } private: int value; }; float a = 1.1; float b = 1.2; float c = 0; float d = 0; int main() { { raii_feround raii; raii.round_up(); c = raii.add(a, b); } { raii_feround raii; raii.round_down(); d = raii.add(a, b); } std::cout << c << "\n"; // Output is: 2.3 std::cout << d << "\n"; // Output is: 2.3 or 2.29999 }
add()
cout
FE_TONEAREST
FE_DOWNWARD
std::cout << std::setprecision(10) << c << "\n"; std::cout << std::setprecision(10) << d << "\n";
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)