I have a floating point exception, and I don't know why.
the code is this:
void calcola_fitness(){
vector<double> f开发者_开发问答itness;
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. Any kinds of arithmetics exception will trigger SIGFPE. This includes divide-by-zero.
You should check if nodes
and edges
are empty.
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:
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.
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]. 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.
So as a guess, that's where the error is coming in... you're doing a getIndex() of the end() iterator.
精彩评论