带有大文件的C ++浮点异常

时间:2017-09-14 14:00:16

标签: c++ error-handling floating-point

在使用大量数据(大约2MB)执行以下代码时,我在第21行收到浮点异常。

    #include <bits/stdc++.h>
using namespace std;


int main() {
    std::ios_base::sync_with_stdio(false);
    int S, Q, type, seq, pos;
    int x, y, lastAnswer = 0;
    cin >> S >> Q;
    vector< vector <int> > seqList(S);

    for(int i=0; i<Q; i++){
        cin >> type >> x >> y;
        if( type == 1 ){
            seq = (x^lastAnswer) % S;
            seqList[seq].push_back(y);
            if(seq == 0) lastAnswer = 0;
        }
        else if( type == 2 ){
            seq = (x^lastAnswer) % S;
            pos = (int)(y % seqList[seq].size()); //Line 21
            lastAnswer = seqList[seq][pos];
            cout << lastAnswer << endl;
        }
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

pos = (int)(y % seqList[seq].size()); //Line 21

seqList[seq].size()0@Lưu Vĩnh Phúc

使用a%b,如果b == 0,则行为未定义。当整数除法或%为零时,许多平台会报告浮点异常。

重写以检测seqList[seq].size() == 0的时间。

同时检查S == 0(x^lastAnswer) % S @Jeremy Friesner

一起使用时的 from urllib2 import urlopen from bs4 import BeautifulSoup import datetime import random import re pages = set() def getLinks (pageUrl): global pages html = urlopen("http://www.mysite.ch" + pageUrl ) bsObj = BeautifulSoup(html, "html.parser") for link in bsOj.findAll("a" , href = re.compile("^(/f/)")): if 'href' in link.attrs: if link.attrs['href'] not in pages: newPage = link.attrs['href'] print(newPage) pages.add(newPage) getLinks(newPage) getLinks("")