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
  • Doesn't std::round() do what you need? – dimich Commented Nov 22, 2024 at 11:22
  • Is the results of 1.0 + epsilon/2 equal to 1.0 or 1.0 + epsilon? I don't think std::round() controls that behavior. – Daniel Turizo Commented Nov 22, 2024 at 16:30
  • Ah, you mean rounding in floating-point arithmetics, not rounding floating-point to integer. I think it is implementation-specific, and if floating-point arithmetics is implemented in hardware, it is limited by hardware capabilities. The only choise to use unsupported mode is to use software implementation. – dimich Commented Nov 22, 2024 at 17:15
  • I'm OK with that, do you know of any library that implements this? – Daniel Turizo Commented Nov 22, 2024 at 17:23
  • 1 Look for "Soft Float" for your target architecture. – dimich Commented Nov 22, 2024 at 18:24
 |  Show 1 more comment

1 Answer 1

Reset to default -1

It'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