1、C语言数据类型中float有效位是6~7,这个到底是6还是7,怎么确定? 2、()运算符是怎么回事?

如题所述

我的理解是,如果不特别说明,一般输出的时候,对于float就是给出6位,但是实际上可以正确输出的是7位,这个可以具体设置。给你一段参考代码,自己试试看:
The following program shows cout truncating to 6 digits:

#include <iostream>
int main() {
using namespace std;
float fValue;
fValue = 1.222222222222222f;
cout << fValue << endl;
fValue = 111.22222222222222f;
cout << fValue << endl;
fValue = 111111.222222222222f;
cout << fValue << endl; }

This program outputs:

1.22222
111.222
111111
Note that each of these is only 6 digits.

However, we can override the default precision that cout shows by using the setprecision() function that is defined in a header file called iomanip.

#include <iostream>
#include <iomanip> // for setprecision()
int main() {
using namespace std;
cout << setprecision(16); // show 16 digits
float fValue = 3.33333333333333333333333333333333333333f;
cout << fValue << endl;
double dValue = 3.3333333333333333333333333333333333333;
cout << dValue << endl;

Outputs:

3.333333253860474
3.333333333333334
Because we set the precision to 16 digits, each of the above numbers has 16 digits. But, as you can see, the numbers certainly aren’t precise to 16 digits!

Variables of type float typically have a precision of about 7 significant digits (which is why everything after that many digits in our answer above is junk). Variables of type double typically have a precision of about 16 significant digits. Variables of type double are named so because they offer approximately double the precision of a float.追问

1.22222
111.222
111111这些都是6位
但3.333333253860474又是7位==>怎么不一致呢?
3.333333333333334作为double又是低位15而不是16?
你的这个是C++吧,我现在刚刚学C语言,两个一样吗?

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-08
float的有效位是6-7是指可以设置最多为7,具体可以自己写一段代码来试试。没有()这个运算符追问

int i a ;i=(a=2*3,a*5),a+6; i是多少?
float最多7位,那什么时候取六位,我自己写了啊可是更不明白啊?

第2个回答  2012-06-08
int i a ;i=(a=2*3,a*5),a+6; i是多少?
这个不是()运算符
这个是 , 表达式 其值 取 最右边表达式的值
i = a+6 a经过第一个表达式 a =6 所以 i = 6+6 =12;追问

根据优先级,答案应该是i=30啊

追答

不是吧
我的理解是 这样的,一步一步解析下来
i=(a=2*3,a*5),a+6;
i = a*5,a+6;
i = a+6;
相当于两个逗号表达式

第3个回答  2012-06-08
就是最多可以是7.