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

      <bdo id='S6Kcq'></bdo><ul id='S6Kcq'></ul>

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

    <legend id='S6Kcq'><style id='S6Kcq'><dir id='S6Kcq'><q id='S6Kcq'></q></dir></style></legend>
    <tfoot id='S6Kcq'></tfoot>

      为什么long long n = 2000*2000*2000*2000;溢出?

      时间:2023-08-03

          <legend id='FpcMM'><style id='FpcMM'><dir id='FpcMM'><q id='FpcMM'></q></dir></style></legend>
          • <bdo id='FpcMM'></bdo><ul id='FpcMM'></ul>

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

              1. 本文介绍了为什么long long n = 2000*2000*2000*2000;溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                long long int n = 2000*2000*2000*2000;    // overflow
                
                long long int n = pow(2000,4);            // works
                long long int n = 16000000000000;         // works
                

                为什么第一个溢出(乘以整数文字常量以分配给 long long)?

                Why does the first one overflow (multiplying integer literal constants to assign to a long long)?

                它与第二个或第三个有什么不同?

                What's different about it vs. the second or third ones?

                推荐答案

                因为 2000 是一个 int 通常是 32 位的.只需使用 2000LL.

                Because 2000 is an int which is usually 32-bit. Just use 2000LL.

                使用 LL 后缀代替 ll 是@AdrianMole 在评论中建议的,现在已删除.请查看他的答案.

                Using LL suffix instead of ll was suggested by @AdrianMole in, now deleted, comment. Please check his answer.

                默认情况下,整数文字是可以保存其值但不小于 int 的最小类型.2000 可以很容易地存储在 int 中,因为标准保证它至少是一个有效的 16 位类型.

                By default, integer literals are of the smallest type that can hold their value but not smaller than int. 2000 can easily be stored in an int since the Standard guarantees it is effectively at least a 16-bit type.

                算术运算符总是使用存在的较大但不小于 int 的类型调用:

                Arithmetic operators are always called with the larger of the types present but not smaller than int:

                • char*char 将被提升为 operator*(int,int)->int
                • char*int 调用 operator*(int,int)->int
                • long*int 调用 operator*(long,long)->long
                • int*int 仍然调用 operator*(int,int)->int.
                • char*char will be promoted to operator*(int,int)->int
                • char*int calls operator*(int,int)->int
                • long*int calls operator*(long,long)->long
                • int*int still calls operator*(int,int)->int.

                至关重要的是,类型不依赖于结果是否可以存储在推断类型中.这正是您的情况发生的问题 - 乘法是用 ints 完成的,但结果溢出,因为它仍然存储为 int.

                Crucially, the type is not dependent on whether the result can be stored in the inferred type. Which is exactly the problem happening in your case - multiplication is done with ints but the result overflows as it is still stored as int.

                C++ 不支持像 Haskell 那样基于目的地推断类型,因此赋值无关紧要.

                C++ does not support inferring types based on their destination like Haskell does so the assignment is irrelevant.

                这篇关于为什么long long n = 2000*2000*2000*2000;溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 64 位中进行组合乘除运算的最准确方法是什么 下一篇:最广泛使用的 C++ 向量/矩阵数学/线性代数库是什

                相关文章

                最新文章

              2. <tfoot id='xBEHz'></tfoot>

                  • <bdo id='xBEHz'></bdo><ul id='xBEHz'></ul>

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

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

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