C++ 中浮点数截取精度,代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <iostream> #include <iomanip> #include <sstream> using namespace std; int main() { double pi = 3.1415926; cout << setprecision(3) << pi << endl; //有效位数为3位 cout << setiosflags(ios::fixed) << setprecision(3) << pi << endl; //保留小数点后3位 cout << ios::fixed << endl; //单独的数值是 8192 double sqrt2 = 1.41375;/*1.414213562373095;*/ stringstream ss; ss << setiosflags(ios::fixed) << setprecision(3) << sqrt2 << endl; ss >> sqrt2; cout << sqrt2 << endl; return 0; }
采用`cout`输出会截断,但是查看存储的数值,还是存在偏差的小数。
最后
以上就是眯眯眼裙子最近收集整理的关于C++ 浮点数截取精度的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复