C语言 浮点值的后缀“f”有什么用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5026570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the use of suffix `f` on float value
提问by Alex
I am wondering what the difference is between these two variables in C:
我想知道 C 中这两个变量之间的区别是什么:
float price = 3.00;
and
和
float price = 3.00f;
What is the use of suffix fin this case?
f在这种情况下后缀的用途是什么?
采纳答案by James
3.00is interpreted as a double, as opposed to 3.00fwhich is seen by the compiler as a float.
3.00被解释为 a double,而不是3.00f编译器将其视为 a float。
The fsuffix simply tells the compiler which is a floatand which is a double.
该f后缀只是告诉编译器这是一个float和这是一个double。
See MSDN (C++)
请参阅MSDN (C++)
回答by Lundin
In addition to what has already been said, keeping track of 1.0 versus 1.0f is more important than many people realize. If you write code like this:
除了已经说过的内容之外,跟踪 1.0 与 1.0f 的关系比许多人意识到的更重要。如果你写这样的代码:
float x;
...
float y = x * 2.0;
Then x will be promoted to a double, because 2.0 is a double. The compiler is not allowed to optimize that promotion away or it would violate the C standard. The calculation takes place with double precision, and then the result is then implicitly truncated into a float. This means that the calculation will be slower (though more accurate) than it would have been if you had written 2.0f or 2.
然后 x 将被提升为双精度值,因为 2.0 是双精度值。不允许编译器优化该提升,否则会违反 C 标准。计算以双精度进行,然后结果被隐式截断为浮点数。这意味着计算将比您编写 2.0f 或 2 时更慢(尽管更准确)。
Had you written 2, the constant would be of int type, which would be promoted to a float, and the calculation would have been done with "float precision". A good compiler would warn you about this promotion.
如果你写了 2,常量将是 int 类型,它将被提升为浮点数,并且计算将使用“浮点精度”完成。一个好的编译器会警告你这个提升。
Read more about the "usual arithmetic conversion" rules here:
在此处阅读有关“通常的算术转换”规则的更多信息:
http://msdn.microsoft.com/en-us/library/3t4w2bkb%28v=vs.80%29.aspx
http://msdn.microsoft.com/en-us/library/3t4w2bkb%28v=vs.80%29.aspx
回答by Grijesh Chauhan
Because by unsuffixed floating-point literals are doubles, and rounding means that even small literals can take on different values when rounded to float and double. This can be observed in the following example:
因为无后缀的浮点文字是双精度值,而四舍五入意味着即使是小的文字在四舍五入为浮点数和双精度值时也可以采用不同的值。这可以在以下示例中观察到:
float f=0.67;
if(f == 0.67)
printf("yes");
else
printf("no");
This will output no, because 0.67has a different value when rounded to float than it does when rounded to double. On the other hand:
这将输出no,因为0.67四舍五入为浮点时的值与四舍五入为双倍时的值不同。另一方面:
float f=0.67;
if(f == 0.67f)
printf("yes");
else
printf("no");
outputs yes.
输出yes。
The suffix can be specified using either upper or lowercase letters.
可以使用大写或小写字母指定后缀。
Try this also:
也试试这个:
printf(" %u %u\n", sizeof(.67f), sizeof(.67));
Check @codepade
检查@代码板
回答by Erik
3.00 is a double, 3.00f is a float.
3.00 是双精度数,3.00f 是浮点数。
回答by Erik
That's because the default type of a floating point numeric literal - the characters 3.00 is double not float. To make this compile you have to add the suffix f (or F).
那是因为浮点数字文字的默认类型 - 字符 3.00 是 double 而不是 float。要进行此编译,您必须添加后缀 f(或 F)。
回答by Frederik Slijkerman
Often the difference isn't important, as the compiler will convert the double constant into a float anyway. However, consider this:
通常差异并不重要,因为编译器无论如何都会将双精度常量转换为浮点数。但是,请考虑:
template<class T> T min(T a, T b)
{
return (a < b) ? a : b;
}
float x = min(3.0f, 2.0f); // will compile
x = min(3.0f, 2); // compiler cannot deduce T type
x = min(3.0f, 2.0); // compiler cannot deduce T type
回答by SridharKritha
Adding few more combination of comparisons between float and double data types.
在 float 和 double 数据类型之间添加更多的比较组合。
int main()
{
// Double type constant(3.14) converts to Float type by
// truncating it's bits representation
float a = 3.14;
// Problem: float type 'a' promotes to double type and the value
// of 'a' depends on how many bits added to represent it.
if(a == 3.14)
std::cout<<"a: Equal"<<std::endl;
else
std::cout<<"a: Not Equal"<<std::endl;
float b = 3.14f; // No type conversion
if(b == 3.14) // Problem: Float to Double conversion
std::cout<<"b: Equal"<<std::endl;
else
std::cout<<"b: Not Equal"<<std::endl;
float c = 3.14; // Double to Float conversion (OK even though is not a good practice )
if(c == 3.14f) // No type conversion
std::cout<<"c: Equal"<<std::endl; // OK
else
std::cout<<"c: Not Equal"<<std::endl;
float d = 3.14f;
if(d == 3.14f)
std::cout<<"d: Equal"<<std::endl; // OK
else
std::cout<<"d: Not Equal"<<std::endl;
return 0;
}
Output:
输出:
a: Not Equal
b: Not Equal
c: Equal
d: Equal

