Setprecision is Confusing

后端 未结 3 1749
既然无缘
既然无缘 2020-12-15 09:34

I just want to ask about setprecision because I\'m a bit confused.

here\'s the code:

#include 
#include 
using namesp         


        
相关标签:
3条回答
  • 2020-12-15 09:50

    There is no reason to expect that any of the constants in your post can be represented exactly using the floating-point system. As a consequence, the exact halves that you have may no longer be exact halves once you store them in a double variable (regardless of how the iostreams are meant to round such numbers.)

    The following code illustrates my point:

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    
    {
      double rate = 1.115;
      cout << fixed << setprecision(20) << rate;
    }
    

    Output:

    1.11499999999999999112
    

    I would recommend taking a look at the FAQ.

    0 讨论(0)
  • 2020-12-15 09:56

    Why do you say that 1.105 should be 1.11? The C++ standard says nothing about it, but the default rounding mode on most of the usual machines (Intel, Sparc, etc.) is round to even, so 1.105 should be 1.10. In general, when the exact result is exactly between two representable values, the rule is to round to the one with an even least significant digit.

    I also wonder where you're getting these values. On the usual machines, 1.105 can't be represented, so you have something slightly larger or slightly smaller.

    And of course, the above comments apply for all of the other values you've cited.

    0 讨论(0)
  • 2020-12-15 09:59

    Some of the numbers you're printing may not be representable as a floating point number and may actually be lower or higher than you think, directly affecting the rounding.

    Since you're trying to format a floating point number to fixed point, have you considered actually USING a fixed point number (int/long scaled by say 1000 depending on your needs) that has its own insert operator defined? Then you'll always get accurate display and rounding without needing to rely on setprecision having any particular behavior (I couldn't find the relevant conversion section in the standard in a quick look).

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