简体   繁体   English

C++ 中的错误添加

[英]Wrong addition in C++

double *out;
int size = size1+size2;
string s = "";
int c = 0;
int curr = 0;
for(int i=size-2; i >= 0; i--) {
    cout << "\nP: " << out[i]/size << "\n";
    cout << "Carry: " << c <<"\n";
    curr = out[i]/size + c;
    s += char(curr%10 + '0');
    cout << "Out: " << curr <<"\n";
    c = curr / 10;
}

In this snippet I am trying to add "P" ( out[I]/size ) and "Carry" ( c ) into "Out" ( curr ).在这个片段中,我试图将“P”( out[I]/size )和“Carry”( c )添加到“Out”( curr )中。 As you can see in the output for 41 and 2, the addition comes out to be 42. Can someone explain how?正如您在 41 和 2 的 output 中看到的那样,加法结果是 42。有人可以解释一下吗?

Output: Output:

P: 20 Carry: 0 Out: 20 P:20 进位:0 出:20

P: 41 Carry: 2 Out: 42 P:41 进位:2 出:42

P: 37 Carry: 4 Out: 40 P:37 进位:4 出:40

P: 43 Carry: 4 Out: 47 P:43 进位:4 出局:47

P: 49 Carry: 4 Out: 53 P:49 进位:4 出局:53

P: 83 Carry: 5 Out: 88 P:83 进位:5 出局:88

Looking past the fact that this is not a well-formed code example due to out being an uninitialized pointer, I'm sure you just chucked it there for illustration instead of actually demonstrating your actual code.回顾一下由于out是一个未初始化的指针,这不是一个格式良好的代码示例这一事实,我敢肯定您只是将它放在那里进行说明,而不是实际演示您的实际代码。

But anyway, if you are doing floating-point division (which is the case with out[i] / size ) then when you output that value with std::cout it will round it to whatever the output precision is.但是无论如何,如果您正在进行浮点除法( out[i] / size就是这种情况),那么当您使用std::cout output 时,它会将其四舍五入为 output 精度。 In reality it may be slightly less or slightly more than 41.实际上,它可能略小于或略大于 41。

Similarly, when you take that value and add c you have the same thing: a floating point value that might be slightly more or slightly less than 43. When you now truncate that to an integer then if it's less than 43 the value will become 42.同样,当您获取该值并添加c时,您会得到相同的结果:浮点值可能略大于或略小于 43。当您现在将其截断为 integer 时,如果它小于 43,则该值将变为 42 .

Try this:尝试这个:

curr = static_cast<int>(std::round(out[i] / size + c));

In your code, out is not initialized.在您的代码中, out未初始化。 So your code has undefined behavior (since that variable contains some unknown / unpredictable pointer).因此,您的代码具有未定义的行为(因为该变量包含一些未知/不可预测的指针)。

If you compiled with GCC invoked as g++ -Wall -Wextra -g , you should get some warnings.如果您使用GCC编译为g++ -Wall -Wextra -g ,您应该会收到一些警告。

You could consider coding你可以考虑编码

int size = size1+size2;
double* out = new double [size];

Then compile your program with all warnings and debug info, and use a debugger (like GDB ) to understand the behavior of your executable.然后使用所有警告和调试信息编译您的程序,并使用调试器(如GDB )来了解可执行文件的行为。

Take more time to read some good C++ reference , and some good C++ programming book花更多的时间阅读一些好的C++ 参考和一些好的C++ 编程书

Be sure to read the documentation of your C++ compiler.请务必阅读 C++ 编译器的文档。 For GCC it is here .对于 GCC 它在这里 For Clang it is there .对于Clang它就在那里

You could later read some C++ draft standard, like n3337 or buy the latest C++ standard from ISO .您可以稍后阅读一些 C++ 标准草案,例如n3337或从ISO购买最新的 C++ 标准。

You could find many open source projects coded in C++ (eg FLTK , RefPerSys , Ghudi , Qt , ninja and many others....).您可以找到许多以 C++ 编码的开源项目(例如FLTKRefPerSysGhudiQtninja等等......)。 Take some time to study their source code (for inspiration)花一些时间研究他们的源代码(以获得灵感)

Regarding floating point operations don't forget to read the Floating Point Guide .关于浮点运算不要忘记阅读浮点指南

Consider (if so allowed) using the Clang static analyzer on your code.考虑(如果允许)在您的代码上使用Clang static 分析器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM