浮点值后缀“f”有什么用
我想知道 C: 中这两个变量之间有什么区别
float price = 3.00;
,
float price = 3.00f;
在这种情况下后缀 f
有什么用?
I am wondering what the difference is between these two variables in C:
float price = 3.00;
and
float price = 3.00f;
What is the use of suffix f
in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
3.00
被解释为double
,而3.00f
被编译器视为float
。f
后缀只是告诉编译器哪个是float
哪个是double
。请参阅 MSDN (C++)
3.00
is interpreted as adouble
, as opposed to3.00f
which is seen by the compiler as afloat
.The
f
suffix simply tells the compiler which is afloat
and which is adouble
.See MSDN (C++)
除了已经说过的内容之外,跟踪 1.0 与 1.0f 比许多人意识到的更重要。如果你编写这样的代码:
那么 x 将被提升为双精度型,因为 2.0 是双精度型。不允许编译器优化该提升,否则会违反 C 标准。计算以双精度进行,然后结果被隐式截断为浮点型。这意味着计算将比您编写 2.0f 或 2 时慢(尽管更准确)。
如果您编写 2,常量将是 int 类型,它将被提升为 float,并且计算将以“浮点精度”完成。一个好的编译器会警告您有关此促销的信息。
在此处阅读有关“常用算术转换”规则的更多信息:
http://msdn.microsoft.com/en-us/library/3t4w2bkb%28v=vs.80%29.aspx
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:
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.
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.
Read more about the "usual arithmetic conversion" rules here:
http://msdn.microsoft.com/en-us/library/3t4w2bkb%28v=vs.80%29.aspx
因为无后缀的浮点文字是双精度数,而舍入意味着即使很小的文字在舍入为浮点数和双精度数时也可以采用不同的值。这可以在以下示例中观察到:
这将输出
no
,因为0.67
在舍入为 float 时的值与舍入为 double 时的值不同。另一方面:输出
yes
。可以使用大写或小写字母指定后缀。
也试试这个:
检查@codepade
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:
This will output
no
, because0.67
has a different value when rounded to float than it does when rounded to double. On the other hand:outputs
yes
.The suffix can be specified using either upper or lowercase letters.
Try this also:
Check @codepade
3.00 是双精度型,3.00f 是浮点数。
3.00 is a double, 3.00f is a float.
添加了一些浮点和双精度数据类型之间的比较组合。
输出:
Adding few more combination of comparisons between float and double data types.
Output:
这是因为 a 的默认类型
浮点数字文字 -
字符 3.00 是双精度而不是浮点数。
要进行编译,您必须添加
后缀 f(或 F)。
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).
通常差异并不重要,因为编译器无论如何都会将双精度常量转换为浮点型。但是,请考虑一下:
Often the difference isn't important, as the compiler will convert the double constant into a float anyway. However, consider this: