“2.00”和“2.00”之间是否存在功能差异?和“2.00f”?

发布于 2024-12-08 22:58:51 字数 244 浏览 1 评论 0原文

我问这个问题是因为我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

街角卖回忆 2024-12-15 22:58:51

f 后缀使其成为单精度(浮点)文字而不是双精度文字。这通常意味着 32 位而不是 64 位浮点数。

浮点常量默认为 double 类型。通过使用后缀 f 或 l(或 F 或 L — 后缀不区分大小写),可以分别将常量指定为 float 或 long double。

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.

Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively.

http://msdn.microsoft.com/en-us/library/tfh6f0w2(v=VS.100).aspx

美煞众生 2024-12-15 22:58:51

有一个区别。 2.00 的类型为 double2.00f 的类型为 float。其确切精度和格式影响取决于您的平台。使用其中一种方法是否会在代码中产生实际差异取决于使用它的上下文。

作为显式类型变量(基本数字类型)的初始值设定项,不会有任何区别,但在函数调用中使用时,它可能会影响使用哪个重载或模板专门化。

显然,当在使用 auto 类型说明符的声明中用作初始值设定项或在 decltype-说明符中用作表达式时,所声明的对象的类型将会受到影响。

decltype(2.00) x = 2.00f; // confusing
decltype(2.00f) y = 2.00; // also confusing

auto d = 2.00;
auto f = 2.00f;

There is a difference. 2.00 has type double and 2.00f has type float. 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.

decltype(2.00) x = 2.00f; // confusing
decltype(2.00f) y = 2.00; // also confusing

auto d = 2.00;
auto f = 2.00f;
撕心裂肺的伤痛 2024-12-15 22:58:51

只要将它们分配给 float,就绝对没有区别,因为 在所有数字类型中都可以精确且正确地表示。

重要的区别在于文字的类型2.0double2.0 为 float fint 代表 2。所以它对参数类型推导产生了影响:

void foo(int) { cure_cancer(); };
void foo(float) { wipe_hard_disk(); }
void foo(double) { exit(0); }

foo(2);
foo(2.0f);
foo(2.0);

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 for 2.0, float for 2.0f and int for 2. So it makes a difference in argument type deduction:

void foo(int) { cure_cancer(); };
void foo(float) { wipe_hard_disk(); }
void foo(double) { exit(0); }

foo(2);
foo(2.0f);
foo(2.0);
娇俏 2024-12-15 22:58:51

默认假设是double。指定 f 后缀可确保它将被解释为 float

The default assumption is double. specifying f suffix ensures that it will be interpreted as a float

与酒说心事 2024-12-15 22:58:51

2.00的类型为double2.00f的类型为float

后缀 f 将文字 2.00 转换为浮点类型,降低其精度。否则,文字是 double 类型。

The type of 2.00 is double, and the type of 2.00f is float.

The suffix f turns the literal 2.00 into a float type, decreasing its precision. Otherwise, literal is double type.

几味少女 2024-12-15 22:58:51
  • 后缀影响类型,这会改变语句的解析方式

    这不仅会像其他人所说的那样通过 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;

    那么 f1f2 将具有不同的值,因为后缀会阻止 双舍入(意思是舍入两次,而不是double类型)发生在f2(十进制→双精度→浮点)中。小数值直接在 f1 中四舍五入到最接近的 float 值(小数 → float),而不经过 double。更精确地打印这些值,您就会看到

    另一个例子:

    <预><代码>f1 = 7.038531e-26f;
    f2 = 7.038531e-26;

    您可以在 演示

另请参阅

  • 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 evaluated

    For example 2.0 * x / 3 is done in double precision, and 2.0f * x / 3 is done in float precision when x is float. 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 for float only, and it's a thing embedded programmers need to care about. Demo on Godbolt

    Here I'm assuming the FLT_EVAL_METHOD macro is set so that floating-point types are not evaluated in a higher precision. But even if FLT_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

    float f1 = 8388608.5000000009f;
    float f2 = 8388608.5000000009;
    

    then f1 and f2 will have different values because the suffix prevents double rounding (meaning rounding twice, not the type double) from happening in f2 (decimal → double → float). The decimal value is rounded to the nearest float value directly in f1 (decimal → float) without going through double. Print the values with more precision and you'll see

    Another example:

    f1 = 7.038531e-26f;
    f2 = 7.038531e-26;
    

    There are many other examples that you can see in the demo

See also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文