Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1282

Answer by HolyBlackCat for How do you std::round doubles, but round towards zero in halfway cases?

$
0
0
#include <cmath>template <typename T>T absmax(T a, T b){    return std::abs(a) > std::abs(b) ? a : b;}template <typename T>T round_ties_towards_zero(T x){    return absmax(std::round(std::nextafter(x, T(0))), std::trunc(x));}

For sufficiently small numbers you don't need absmax(..., trunc(x)). Without it, I start getting wrong answers for doubles at around 1e16 and larger.


Viewing all articles
Browse latest Browse all 1282

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>