C ++中浮点数的精度是多少?
在C++中,浮点数的大小为4字节或8字节。因此它最多可以存储小数位。例如,1/3=0.333333……直到无穷大。如果我们将其存储在浮点型变量中,则它将存储一些有效数字。默认值为6。因此,C++中的浮点数通常最多可以显示6个小数位。
我们可以使用setprecision更改精度的大小。这存在于iomanip头文件中。让我们看一个例子来了解这个想法。
范例程式码
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double x = 2.3654789d;
cout << "Print up to 3 decimal places: " << setprecision(3) << x << endl;
cout << "Print up to 2 decimal places: " << setprecision(2) << x << endl;
cout << "Print up to 7 decimal places: " << setprecision(7) << x << endl;
}输出结果
Print up to 3 decimal places: 2.365 Print up to 2 decimal places: 2.37 Print up to 7 decimal places: 2.3654789
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志