Rounding differences on Windows vs Unix based system in sprintf

前端 未结 4 1919
天涯浪人
天涯浪人 2020-12-19 01:38

I have problem on UNIX based systems sprintf does not round up properly value.

For example

double tmp = 88888888888885.875
char out[512];


        
相关标签:
4条回答
  • 2020-12-19 02:15

    There is a bug report for glibc with a problem very similar to yours. The main conclusion (in comment 46) here is that double is not a 15-decimal-digit number and you should not expect it to work like that.

    As a workaround you can add something small to your numbers to make them round better. But this solution is not general because it depends on number ranges you deal with.

    Another workaround can be multiplying to prepare them for rounding, then rounding (e.g. 2597.625*100 = 259762.5 -> 259763 = 2597.63*100)

    However I think there must be smarter workarounds.

    0 讨论(0)
  • 2020-12-19 02:24

    On an IEEE 754 conformant implementation, it should print 88888888888885.88 in the default rounding mode. This has nothing to do with floating point precision since the value is exactly representable; it's simply a matter of printf's rounding to 2 places after the decimal point. No idea why you're seeing 88888888888885.87 on some systems.

    0 讨论(0)
  • 2020-12-19 02:28

    That's because you're using double which has accuracy limitations, meaning, your 88888888888885.875 is probably being rounded to something else internally.

    See more info in a similar question, in blogs or in wikipedia.

    0 讨论(0)
  • 2020-12-19 02:30

    What floating point representations are used by your processor and your compiler?

    Not all processors use the same way to represent floating-point values and even compilers may choose different floating-point representation methods (I think the Microsoft C++ compiler even has options to choose the representation).

    The page http://www.quadibloc.com/comp/cp0201.htm gives an overview of some of the floating-point representations (although they seem to be rather old architectures shown there).

    http://msdn.microsoft.com/en-us/library/0b34tf65.aspx describes how Microsoft Visual C++ stores floating-point values. I couldn't find immediately what representation is used by AIX or Linux.

    Additinally, every compiler has options that let you indicate how you want to work with floating-point operations. Do you want them to be correct as possible (but possibly somewhat slower)? Or do you want floating-point operations to be fast as possible (but possibly less correct)?

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