C++ Rounding behavior consistency for ties with sprintf

后端 未结 1 1552
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 02:19

I have a C++ program that runs on both Windows/Linux. On Windows the program is compiled with Visual Studio 2012 and Linux it is compiled with GCC. When converting doubles

相关标签:
1条回答
  • 2020-12-18 02:28

    This is implementation defined behavior from the draft C11 standard section 7.21.6.1 The fprintf function which covers sprintf with respect for format specifiers and also C++ since the C++ standard relies on the C standard for stdio functions, it says for the f format specifier:

    The value is rounded to the appropriate number of digits.

    this is also covered by defect report 211 which added the following:

    The accuracy of the floating-point operations ( +, -, *, /) and of the library functions in and that return floating-point results is implementation defined, as is the accuracy of the conversion between floating-point internal representations and string representations performed by the libray routine in , and . The implementation may state that the accuracy is unknown.

    the article Inconsistent Rounding of Printed Floating-Point Numbers covers this inconsistency in great details and mentions that:

    glibc printf() has been updated to take the current IEEE rounding mode into account. This was done in version 2.17; I just tested it on version 2.18. In doing it this way of course, round-to-nearest/round-half-away-from-zero is still not an option, so this doesn’t help you make its output consistent with other platforms.

    but as said this does not help with cross platform consistency.

    0 讨论(0)
提交回复
热议问题