C++添加不起作用

C++ Addition Not Working

本文关键字:不起作用 添加 C++      更新时间:2023-10-16

我为Google Code Jam问题编写了一个解决方案,如下所示:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
        ifstream in;
        in.open(argv[1]);
        int t, c = 0;
        in >> t;
        while(c++<t) {
                string msg;
                in >> msg;
                map<char,int> m;
                int base = 0;
                for(char& ch : msg) {
                        if(!m[ch]) {
                                base++;
                                m[ch] = base == 1 ? base : (base == 2 ? -1 : base - 1);
                        }
                }
                if(base < 2)
                        base = 2;
                double total = 0;
                double p = pow(base, msg.size()-1);
                for(char& ch : msg) {
                        if(m[ch] != -1) {
                                if(c == 37) cout << "total=" << total << "+" << (m[ch] * p) << "=" << total + (m[ch] * p) << endl;
                                total = total + (m[ch] * p);
                        }
                        p /= base;
                }
                cout.precision(0);
                cout << fixed << "Case #" << c << ": " << total << endl;
        }
        in.close();
        return 0;
}

如您所见,我为案例 37 打印了一些调试语句,因为那里发生了一些奇怪的事情:

Case #36: 1000000000000000000
total=0+450283905890997376=450283905890997376
total=450283905890997376+100063090197999424=550346996088996800
total=550346996088996800+16677181699666570=567024177788663360
total=567024177788663360+5559060566555523=572583238355218880
total=572583238355218880+1853020188851841=574436258544070720
total=574436258544070720+1235346792567894=575671605336638592
total=575671605336638592+205891132094649=575877496468733248
total=575877496468733248+68630377364883=575946126846098112
total=575946126846098112+22876792454961=575969003638553088
total=575969003638553088+15251194969974=575984254833523072
total=575984254833523072+847288609443=575985102122132544
total=575985102122132544+564859072962=575985666981205504
total=575985666981205504+62762119218=575985729743324736
total=575985729743324736+20920706406=575985750664031168
total=575985750664031168+6973568802=575985757637600000
total=575985757637600000+129140163=575985757766740160
total=575985757766740160+28697814=575985757795437952
total=575985757795437952+1594323=575985757797032256
total=575985757797032256+177147=575985757797209408
total=575985757797209408+59049=575985757797268480
total=575985757797268480+6561=575985757797275072
total=575985757797275072+4374=575985757797279424
total=575985757797279424+729=575985757797280128
total=575985757797280128+81=575985757797280192
total=575985757797280192+2=575985757797280192
Case #37: 575985757797280192

如您所见,在某些时候加法只是工作不正确(例如575985757797279424+729 = 575985757797280153而不是575985757797280128)

我对这种行为感到非常目瞪口呆,非常感谢任何可能的解释。

您已达到所选浮点类型的精度限制。

如果你坚持避免整数(即不动点),你将需要一个任意精度的数字库来优化它。在继续使用这些功能之前,您还应该阅读浮点指南。

但是,您在此处的数字都适合 64 位整数。为什么不直接使用它并为自己省去一些麻烦呢?

Double 有 3 个分量符号、指数、分数。例如 1.2345 表示为 12345*10power-4

尽管 Double 的大小与 double 一样长,但它有一些专用于指数部分的位,因此精度小于 long long 的精度,这使得浮点精度为 7 位十进制数字,双精度为 16 位十进制数字。由于浮点运算在指定位数后不精确

另请阅读
1.https://chortle.ccsu.edu/java5/Notes/chap11/ch11_2.html
2.http://codeforces.com/blog/entry/1521?#comment-28329 (关于 C++ 中的 PoW)