“2.00”和“2.00”之间是否存在功能差异?和“2.00f”?
我问这个问题是因为我使用的是 Box2D 库,它主要需要浮点参数。尽管我看到很多使用 0.00f 格式的示例代码,但我不太确定它与普通的旧 0.00 之间是否存在实际差异。
如果不附加额外的f,我以后会伤害自己吗?是速度的问题吗?是否有某种内涵需要 f 加数,而其他则不需要?
TL;DR:为什么我应该使用 0.00f 而不是 0.00?
I ask because I am using the Box2D library, which calls for mostly float arguments. Although I see a lot of example code that uses the 0.00f format, I am not quite sure if there is an actual difference between that and plain old 0.00.
Will I be hurting myself later on by not appending the additional f? Is it a speed thing? Is there some sort of connotation that would need the f addend when others wouldn't?
TL;DR: Why should I be using 0.00f instead of 0.00?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
f
后缀使其成为单精度(浮点)文字而不是双精度文字。这通常意味着 32 位而不是 64 位浮点数。http://msdn.microsoft.com/en-我们/库/tfh6f0w2(v=VS.100).aspx
The
f
suffix makes it a single precision(float) literal instead of a double precision literal. This usually means 32 bit instead of 64 bit floats.http://msdn.microsoft.com/en-us/library/tfh6f0w2(v=VS.100).aspx
有一个区别。
2.00
的类型为double
,2.00f
的类型为float
。其确切精度和格式影响取决于您的平台。使用其中一种方法是否会在代码中产生实际差异取决于使用它的上下文。作为显式类型变量(基本数字类型)的初始值设定项,不会有任何区别,但在函数调用中使用时,它可能会影响使用哪个重载或模板专门化。
显然,当在使用
auto
类型说明符的声明中用作初始值设定项或在 decltype-说明符中用作表达式时,所声明的对象的类型将会受到影响。There is a difference.
2.00
has typedouble
and2.00f
has typefloat
. The exact precision and format implications of this depends on your platform. Whether the use of one over the other makes a practical difference in your code depends on the context in which it is used.As an initializer for an explicity typed variable (of a fundamental numeric type), there will be no difference but when used in a function call it might potentially affect which overload or template specialization is used.
Obviously, when used as an initializer in a declaration using the
auto
type-specifier or as an expression in a decltype-specifier, the type of the object being declared will be affected.只要将它们分配给
float
,就绝对没有区别,因为 值 在所有数字类型中都可以精确且正确地表示。重要的区别在于文字的类型,
2.0
为double
,2.0 为
和float
fint
代表2
。所以它对参数类型推导产生了影响:As long as you assign them to a
float
, there's absolutely no difference, since the value is representable precisely and correctly in all numeric types.The important difference is the type of the literal, which is
double
for2.0
,float
for2.0f
andint
for2
. So it makes a difference in argument type deduction:默认假设是
double
。指定f
后缀可确保它将被解释为float
The default assumption is
double
. specifyingf
suffix ensures that it will be interpreted as afloat
2.00
的类型为double
,2.00f
的类型为float
。后缀
f
将文字2.00
转换为浮点类型,降低其精度。否则,文字是double
类型。The type of
2.00
isdouble
, and the type of2.00f
isfloat
.The suffix
f
turns the literal2.00
into a float type, decreasing its precision. Otherwise, literal isdouble
type.后缀影响类型,这会改变语句的解析方式
这不仅会像其他人所说的那样通过
decltype
和函数重载选择改变类型推导结果,而且还会表达式的方式已评估例如,当 x 为
float< 时,
2.0 * x / 3
以双精度完成,而2.0f * x / 3
以浮点精度完成/代码>。这可能会导致不同的结果,因为以更高的精度进行操作会减少任何中间误差。它还会极大地影响无 FPU 系统或仅具有浮点型 FPU 的系统的性能,这是嵌入式程序员需要关心的事情。 <一href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(文件名:%271%27,fontScale:14,fontUsePx:%270% 27,j:1,lang:c%2B%2B,选择:(endColumn:50,endLineNumber:6,positionColumn:50,positionLineNumbe r:6,selectionStartColumn:50,selectionStartLineNumber:6,startColumn:50,startLineNumber:6),来源:%27%23include+%3Ciostream%3E%0A%23include+%3Ciomanip%3E%0Aint+main()%0A%7B% 0A++++浮点+x+%3D+0.4f%3B%0 A++++std::cout+%3C%3C+std::set precision(20)+%3C%3C+2.0+*+x+/+3+%3C%3C+!%27%5Cn!%27%0A++++++++++++++ %3C%3C+std::set precision(20)+%3C%3C+2.0f+*+x+/+3+%3C%3C+!%27%5Cn!%27%3B%0A%7D%0A%27) ,l:%275%27,n:%2 70%27,o:%27C%2B%2B+源+%231%27,t:%270%27)),k:38.27563683866754,l:%274%27,m:100,n:%270%27, ○:%27%2 7,s:0,t:%270%27),(g:!((h:编译器,i:(编译器:g112,过滤器:(b:%270%27,二进制:%271%27,仅注释: %27 0%27,分解:%270%27,指令:%270%27,执行:%270%27,英特尔:%270%27,库代码:%270%27,修剪:%27 1%27),flagsViewOpen:%271%27,fontScale:14,fontUsePx:%270%27,j:1,lang:c%2B%2B,libs:!(),选项:%27-O3 %27,选择:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),来源:1,树:%271%27) ,l:%275%27,n: %270%27,o:%27x86-64+gcc+11.2+(C%2B%2B,+编辑器+%231,+编译器+%231)%27,t:%270%27),(h:输出,i :(编译lerName:%27x86-64+gcc+11.2%27,editorid:1,fontScale:14,fontUsePx:%270%27,j:1,wrap:%271%27),l:%275%27 ,n:%270%27,o:%27输出+of+x86-64+gcc+11.2+(编译器+%231)%27,t:%270%27)),头:(),k:61.72436316133 246,l:%274%27,n:%270%27,o:%27%27,s:1,t:%270%27)),l:%272%27,n:%270%27, o:%27%27,t:%270%27)),版本:4" rel="nofollow noreferrer">Godbolt 演示这里我假设
FLT_EVAL_METHOD
< /a> 宏的设置使得浮点类型不会以更高的精度进行计算。但即使FLT_EVAL_METHOD >= 1
,带后缀和不带后缀的结果可能仍然不同,因为下一点:后缀也会影响值
在 C++ 中,该值必须正确舍入为目标类型中最接近的值。例如,当我们有
<预><代码>浮点f1 = 8388608.5000000009f;
浮动f2 = 8388608.5000000009;
那么
f1
和f2
将具有不同的值,因为后缀会阻止 双舍入(意思是舍入两次,而不是double
类型)发生在f2
(十进制→双精度→浮点)中。小数值直接在f1
中四舍五入到最接近的float
值(小数 → float),而不经过double
。更精确地打印这些值,您就会看到另一个例子:
<预><代码>f1 = 7.038531e-26f;
f2 = 7.038531e-26;
您可以在 演示
另请参阅
f
有什么用The suffix affects the type, which changes how a statement is parsed
This not only changes type deduction result via
decltype
and function overload selection as others said, but also how an expression is evaluatedFor example
2.0 * x / 3
is done in double precision, and2.0f * x / 3
is done in float precision when x isfloat
. That may lead to different results because doing in a higher precision will reduce any intermediate errors. It also affects performance greatly on FPU-less systems or systems with an FPU forfloat
only, and it's a thing embedded programmers need to care about. Demo on GodboltHere I'm assuming the
FLT_EVAL_METHOD
macro is set so that floating-point types are not evaluated in a higher precision. But even ifFLT_EVAL_METHOD >= 1
then the result might still be different with and without the suffix, because of the next point:The suffix also affects the value
In C++ the value must be rounded correctly to the nearest value in the destination type. So for example when we have
then
f1
andf2
will have different values because the suffix prevents double rounding (meaning rounding twice, not the typedouble
) from happening inf2
(decimal → double → float). The decimal value is rounded to the nearestfloat
value directly inf1
(decimal → float) without going throughdouble
. Print the values with more precision and you'll seeAnother example:
There are many other examples that you can see in the demo
See also
f
on float value