admin管理员组文章数量:1122826
I am trying to perform floating point operations in C++17 with round-to-nearest, round-ties-to-away mode. Searching on cppreference I found that there are four rounding modes available, including round-to-nearest (FE_TONEAREST
). However, I found no information about the tie-breaking rule for the FE_TONEAREST
mode.
I found via experiments that, at least on GCC, the FE_TONEAREST
mode is implemented using the round-ties-to-even rule. Is it possible to change the tie-breaking rule to round-ties-to-away? If yes, how do I do it? If no, is there a library that implements floating points with the round-ties-to-away rule?
Thanks in advance.
I am trying to perform floating point operations in C++17 with round-to-nearest, round-ties-to-away mode. Searching on cppreference.com I found that there are four rounding modes available, including round-to-nearest (FE_TONEAREST
). However, I found no information about the tie-breaking rule for the FE_TONEAREST
mode.
I found via experiments that, at least on GCC, the FE_TONEAREST
mode is implemented using the round-ties-to-even rule. Is it possible to change the tie-breaking rule to round-ties-to-away? If yes, how do I do it? If no, is there a library that implements floating points with the round-ties-to-away rule?
Thanks in advance.
Share Improve this question asked Nov 22, 2024 at 4:46 Daniel TurizoDaniel Turizo 1812 silver badges9 bronze badges 6 | Show 1 more comment1 Answer
Reset to default -1It's more or less a flaw in how meaning of these gesetround
arguments is stated impelementation-defined in relevant section, ISO C only states these macroses are defined if it supported, while nearest value defined for round in 7.12.9 Nearest integer functions of ISO C documentation.
The lround and llround functions round their argument to the nearest integer value, rounding halfway cases away from zero, regardless of the current rounding direction. If the rounded value is outside the range of the return type, the numeric result is unspecified and a domain error or range error may occur.
There is some circular self-referencing is observed now, because it was adopted from POSIX, but POSIX documentation currently refers to ISO C.
For FE_TONEAREST case nearbyint()
/rint()
supposed to operate in a way similar to round()
prior to C23, half-case always away from zero. After that it's to nearest, ties to even, additional rounding modes are possible
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2319.htm
本文标签: How do I enable the quotround ties to awayquot rounding mode in CStack Overflow
版权声明:本文标题:How do I enable the "round ties to away" rounding mode in C++? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305581a1932640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
std::round()
do what you need? – dimich Commented Nov 22, 2024 at 11:221.0 + epsilon/2
equal to1.0
or1.0 + epsilon
? I don't thinkstd::round()
controls that behavior. – Daniel Turizo Commented Nov 22, 2024 at 16:30