如何改变/控制gcc或g ++中的输出精度? [英] How to change/control the output precision in gcc or g++?

查看:133
本文介绍了如何改变/控制gcc或g ++中的输出精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码用c ++编写,并由g ++ 4.8编译。

  double x = 0.123456789; 
cout< x<< endl;

我不明白为什么我只得到输出

  0.1234567 

code> long double x 。

解决方案

使用精确度函数或 setprecision 操纵器设置有效数字的数量(或小数位数,如果还使用 fixed )。

  cout.precision // 10 significant figures 

  cout<<设定精度(10); // also 10 significant figures 

  cout<<固定<设定精度(10); //总是定点格式,小数点后10位

默认情况下,精度为6。 P>

I have the following piece of code written in c++ and compiled by g++ 4.8.

double x = 0.123456789;
cout << x << endl;

I don't understand why I only get the output

0.1234567

even I define x as long double x. It probably a quite naive question, but can any one give me some hits?

解决方案

Use the precision function or the setprecision manipulator to set the number of significant figures (or decimal places, if you also use fixed).

cout.precision(10);  // 10 significant figures

or

cout << setprecision(10); // also 10 significant figures

or

cout << fixed << setprecision(10);  // always fixed-point format, 10 decimal places

By default, the precision is 6.

这篇关于如何改变/控制gcc或g ++中的输出精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆