c# - Double returning NaN but not all the time -


i baffled here. have function return double, returns correct value, of times return nan. understanding nan returned when have 0/0. below function returning nan, it's variables declared doubles.

public double superiorvalue(list<double> l)     {         valuekn[0] = 0;         valuekn[1] = 0;         valuekn[2] = 1.69;         valuekn[3] = 1.18;         valuekn[4] = 0.95;         valuekn[5] = 0.82;         valuekn[6] = 0.75;         valerkn[7] = 0.67;         valuekn[8] = 0.63;         valuekn[9] = 0.58;         valuekn[10] = 0.561;         valuekn[11] = 0.542;         valuekn[12] = 0.523;         valuekn[13] = 0.504;         valuekn[14] = 0.485;         valuekn[15] = 0.466;         valuekn[16] = 0.447;         valuekn[17] = 0.428;         valuekn[18] = 0.409;         valuekn[19] = 0.39;         valuekn[20] = 0.382;          xm = (l.sum()) / (l.count);          (int = 0; < l.count; i++)         {             sumtemporary = l[i] - xm;             sum = sum + sumtemporary;         }          kn = valuekn[l.count];          sx = math.sqrt((1 / l.count - 1) * (sum * sum));         vx = sx / xm;         xksup = xm * (1 + kn * vx);         xkinf = xm * (1 - kn * vx);          return xksup;      } 

what baffles me more never list made of less 3 elements , greater 15, , still returns nan said returns correct value. thoughts on ?

look @

math.sqrt((1 / l.count - 1) * (sum * sum))  

the 1 / l.count integer division, 0 l.count > 1 , computing sqrt(-1 * (sum * sum)) .

but (1.0 / l.count - 1) still negative, want (1.0 / (l.count - 1))

the fix then:

math.sqrt((1.0 / (l.count - 1)) * (sum * sum)) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -