<i id='TjRVg'><tr id='TjRVg'><dt id='TjRVg'><q id='TjRVg'><span id='TjRVg'><b id='TjRVg'><form id='TjRVg'><ins id='TjRVg'></ins><ul id='TjRVg'></ul><sub id='TjRVg'></sub></form><legend id='TjRVg'></legend><bdo id='TjRVg'><pre id='TjRVg'><center id='TjRVg'></center></pre></bdo></b><th id='TjRVg'></th></span></q></dt></tr></i><div id='TjRVg'><tfoot id='TjRVg'></tfoot><dl id='TjRVg'><fieldset id='TjRVg'></fieldset></dl></div>
        <bdo id='TjRVg'></bdo><ul id='TjRVg'></ul>

    1. <tfoot id='TjRVg'></tfoot>

      <legend id='TjRVg'><style id='TjRVg'><dir id='TjRVg'><q id='TjRVg'></q></dir></style></legend>

        <small id='TjRVg'></small><noframes id='TjRVg'>

      1. 为什么需要在分配给 C++ 常量的值后附加 L 或 F?

        时间:2023-08-29

      2. <tfoot id='0Nuxt'></tfoot>
        • <bdo id='0Nuxt'></bdo><ul id='0Nuxt'></ul>

                  <tbody id='0Nuxt'></tbody>
                <legend id='0Nuxt'><style id='0Nuxt'><dir id='0Nuxt'><q id='0Nuxt'></q></dir></style></legend>
                • <small id='0Nuxt'></small><noframes id='0Nuxt'>

                  <i id='0Nuxt'><tr id='0Nuxt'><dt id='0Nuxt'><q id='0Nuxt'><span id='0Nuxt'><b id='0Nuxt'><form id='0Nuxt'><ins id='0Nuxt'></ins><ul id='0Nuxt'></ul><sub id='0Nuxt'></sub></form><legend id='0Nuxt'></legend><bdo id='0Nuxt'><pre id='0Nuxt'><center id='0Nuxt'></center></pre></bdo></b><th id='0Nuxt'></th></span></q></dt></tr></i><div id='0Nuxt'><tfoot id='0Nuxt'></tfoot><dl id='0Nuxt'><fieldset id='0Nuxt'></fieldset></dl></div>
                  本文介绍了为什么需要在分配给 C++ 常量的值后附加 L 或 F?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在网上看了很多地方,似乎找不到很好的解释来说明为什么我们应该在分配给 C++ 常量的值后附加 F 或 L.例如:

                  I have looked at quite a few places online and can't seem to find a good explanation as to why we should append an F or L after a value assigned to a C++ constant. For example:

                  const long double MYCONSTANT = 3.0000000L;
                  

                  谁能解释为什么这是必要的?类型声明不是暗示分配给 MYCONSTANT 的值是 long double 吗?上面这行和

                  Can anyone explain why that is necessary? Doesn't the type declaration imply the value assigned to MYCONSTANT is a long double? What is the difference between the above line and

                  const long double MYCONSTANT = 3.0000000;        // no 'L' appended
                  

                  哇!

                  推荐答案

                  浮点常量在 C++ 中默认为 double 类型.由于long doubledouble 更精确,当long double 常量转换为double 时,您可能会丢失有效数字代码>.要处理这些常量,您需要使用L 后缀来保持long double 精度.例如,

                  Floating-point constants have type double by default in C++. Since a long double is more precise than a double, you may lose significant digits when long double constants are converted to double. To handle these constants, you need to use the L suffix to maintain long double precision. For example,

                  long double x = 8.99999999999999999;
                  long double y = 8.99999999999999999L;
                  std::cout.precision(100);
                  std::cout << "x=" << x << "
                  ";
                  std::cout << "y=" << y << "
                  ";
                  

                  此代码在我的系统上的输出,其中 double 是 64 位,long double 96,是

                  The output for this code on my system, where double is 64 bits and long double 96, is

                  x=9
                  y=8.9999999999999999895916591441391574335284531116485595703125
                  

                  这里发生的是 x 在赋值之前四舍五入,因为常量被隐式转换为 double,而 8.99999999999999999 不是可表示为 64 位浮点数.(请注意,作为 long double 的表示也不完全精确.9 的第一个字符串之后的所有数字都试图近似十进制数 8.99999999999999999 尽可能使用 96 个二进制位.)

                  What's happening here is that x gets rounded before the assignment, because the constant is implicitly converted to a double, and 8.99999999999999999 is not representable as a 64-bit floating point number. (Note that the representation as a long double is not fully precise either. All of the digits after the first string of 9s are an attempt to approximate the decimal number 8.99999999999999999 as closely as possible using 96 binary bits.)

                  在您的示例中,不需要 L 常量,因为 3.0 可以精确表示为 double长双.double 常量值被隐式转换为 long double 而不会损失任何精度.

                  In your example, there is no need for the L constant, because 3.0 is representable precisely as either a double or a long double. The double constant value is implicitly converted to a long double without any loss of precision.

                  F 的情况并不那么明显.正如 Zan Lynx 指出的那样,它可以帮助解决超载问题.我不确定,但它也可以避免一些微妙的舍入错误(即,编码为 float 可能会与编码为 double 产生不同的结果,然后四舍五入为 float).

                  The case with F is not so obvious. It can help with overloading, as Zan Lynx points out. I'm not sure, but it may also avoid some subtle rounding errors (i.e., it's possible that encoding as a float will give a different result from encoding as a double then rounding to a float).

                  这篇关于为什么需要在分配给 C++ 常量的值后附加 L 或 F?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C++ 中存储类型 下一篇:结构的地址是否与其第一个成员的地址相同?

                  相关文章

                • <i id='mWdr1'><tr id='mWdr1'><dt id='mWdr1'><q id='mWdr1'><span id='mWdr1'><b id='mWdr1'><form id='mWdr1'><ins id='mWdr1'></ins><ul id='mWdr1'></ul><sub id='mWdr1'></sub></form><legend id='mWdr1'></legend><bdo id='mWdr1'><pre id='mWdr1'><center id='mWdr1'></center></pre></bdo></b><th id='mWdr1'></th></span></q></dt></tr></i><div id='mWdr1'><tfoot id='mWdr1'></tfoot><dl id='mWdr1'><fieldset id='mWdr1'></fieldset></dl></div>

                      <bdo id='mWdr1'></bdo><ul id='mWdr1'></ul>
                  1. <small id='mWdr1'></small><noframes id='mWdr1'>

                    <tfoot id='mWdr1'></tfoot>

                      <legend id='mWdr1'><style id='mWdr1'><dir id='mWdr1'><q id='mWdr1'></q></dir></style></legend>