C ++加法不起作用

时间:2016-04-26 00:02:18

标签: c++ algorithm

我为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)

我对这种行为感到非常傻眼,并且非常感谢任何可能的解释。

2 个答案:

答案 0 :(得分:1)

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

如果你坚持避免整数(即固定点),你需要一个任意精度的数值库来达到最佳效果。在继续使用这些功能之前,您还应该阅读The Floating-Point Guide

但是,这里的数字都适合64位整数。为什么不只是使用它并省去一些麻烦?

答案 1 :(得分:0)

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

尽管Double的大小有很长的长度,它有一些专用于指数部分的位,因此精度小于long long的精度,这使float的精确度为7位十进制数,精度为16位十进制数。由于浮点数指定的位数后算术不精确

同时阅读
1。https://chortle.ccsu.edu/java5/Notes/chap11/ch11_2.html
2。http://codeforces.com/blog/entry/1521?#comment-28329(关于c ++中的pow)