Setprecision is Confusing

后端 未结 3 1752
既然无缘
既然无缘 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 
    #include 
    
    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.

提交回复
热议问题