Infinity where there shouldn't be an infinity
Hi,
I'm getting infinity in a calculation that never gives infinity. Can you please help me? The code is this:
var projects=@All of Project (calculation);
var status=@All of Status;
var amount=@All of Amount requested (before I.V.A.);
var purchases=@Sum of Total cost before I.V.A.;
var inventory_outputs=@Sum of Total inputs cost before I.V.A.;
var total=purchases+inventory_outputs;
var q_p=0;
var aux="";
for(var i=0;i<status.length;i++)
{
if(status[i]!="Not accepted")
{
q_p=0;
aux=String(projects[i]);
for(var j=0;j<aux.length;j+=2)
{
if(aux[j]+aux[j+1]=="//")
{
q_p+=1;
}
}
total+=amount[i]/q_p;
}
}
total
I need to clarify that q_p>=1, always (I've tested it). The only workaround I've found is writing this condition before the division:
if(q_p==0)
{
q_p=1;
}
As you can see, the problem is fixed. Nonetheless, it is really weird that Podio says it is infinity when it is not. I would like to know, am I doing something wrong?
-
Hi Eduardo,
q_p isn't always >=1.
if(aux[j]+aux[j+1] != "//")
(not equal to //) q_p will still be 0 - and total+=amount[i]/0 will result in infinity.It's hard to advise without knowing exactly what the data look like and what you want to achieve. But for me your code looks a bit to complicated. Maybe this works for you:
var q_p = 0;
var aux = "";
for(var i = 0; i < status.length;i++){
aux = String(projects[i]);
if(status[i] != "Not accepted" && aux.indexOf("//") > -1){
q_p += 1;
}
q_p = q_p == 0 ? 1 : q_p;
total += (amount[i]/q_p);
}
totalRainer
-
Hi Eduardo,
that's strange. The only reason I can think of is that the 3 arrays (status, project,s amount) don't have the same length. Did you check that?
Btw: the number of // in projects[i] you can get with: String(projects[i]).split("//").length - 1 (if you use it var aux must be = 0 not = "").var q_p = 0;
var aux = 0;
for(var i = 0; i < status.length;i++){
if(status[i] != "Not accepted"){
aux = String(projects[i]).split("//").length - 1;
q_p += aux;
}
q_p = q_p == 0 ? 1 : q_p;
total += (amount[i]/q_p);
};
total
Please sign in to leave a comment.
Comments
5 comments