简体   繁体   English

为什么浮点异常?

[英]why Floating point exception?

I have a floating point exception, and I don't know why. 我有一个浮点异常,我也不知道为什么。

the code is this: 代码是这样的:

void calcola_fitness(){
    vector<double> fitness;
    int n=nodes.size();
    int e=edges.size();
    int dim=feasibility.size();
    int feas=(feasibility[dim-1])*100;
    int narchi=numarchicoll[dim-1]/e;
    int numero_nodi=freePathNode.size()/n;
    double dist_start_goal=node_dist(0,1);
    int i,f,t;
    double pathlenght=0;
    int siize=freePathNode.size();
    for(i=0;i!=siize-1; i++){
        f=freePathNode[i].getIndex();
        i++;
        t=freePathNode[i].getIndex();
        i--;
        pathlenght=pathlenght+node_dist(f,t);
    }        
    double pathlenghtnorm=pathlenght/10*dist_start_goal;
    double fit=((double)numero_nodi+pathlenghtnorm+(double)narchi)*((double)feas);
    fitness.push_back(fit);
}

Could anybody help me? 有人可以帮我吗? What's the problem? 有什么问题? I could I solve this? 我可以解决这个问题吗? thank you very much 非常感谢你

"Floating point exception" (SIGFPE) is actually a misnomer. “浮点异常”(SIGFPE)实际上是用词不当。 Any kinds of arithmetics exception will trigger SIGFPE. 任何一种算术异常都将触发SIGFPE。 This includes divide-by-zero. 这包括被零除。

You should check if nodes and edges are empty. 您应该检查nodesedges是否为空。

The fastest thing you can do is using a debugger to capture the exact place where the exception is being thrown. 您可以做的最快的事情是使用调试器来捕获引发异常的确切位置。 If you are using g++ you can use gdb and make it stop in the throw: 如果您使用的是g ++,则可以使用gdb并使其停止运行:

shell$ gdb binary
(gdb) catch throw
(gdb) run

Chances are that any of the divisors in the code is 0 and that is triggering the exception, but using a debugger will tell you the exact line and you can check the variable values. 可能代码中的任何除数均为0并触发异常,但是使用调试器将告诉您确切的行,并且您可以检查变量值。

In your code, you have the following: 在代码中,您具有以下内容:

int siize=freePathNode.size();
for(i=0;i!=siize-1; i++){
    f=freePathNode[i].getIndex();
    i++;
    t=freePathNode[i].getIndex();
    i--;
    pathlenght=pathlenght+node_dist(f,t);
}

Lets assume freePathNode.size() returns 2. On the first iteration, f will be the index of element [0], and t will be the index of element [1]. 假设freePathNode.size()返回2。在第一次迭代中,f将是元素[0]的索引,而t将是元素[1]的索引。 That's fine. 没关系。 On the next iteration, f will be the index of element [1], and t will be the index of element [2], which does not exist. 在下一次迭代中,f将是元素[1]的索引,t将是元素[2]的索引,该索引不存在。

So as a guess, that's where the error is coming in... you're doing a getIndex() of the end() iterator. 如此一来,错误就出在这里……您正在执行end()迭代器的getIndex()。

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

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