Joern Katzorke

float calculation problem

Discussion created by Joern Katzorke on Aug 17, 2010
Latest reply on Aug 17, 2010 by Joern Katzorke

hi guys

 

i got a problem with my hc12 and a calculation...

 

what i wanna to do: do a correlation of 2 signals

 

i found and codesnippet in an book for c# and hooked it over to my controller:

 

 

int i = 0;float sumx = 0.0;float sumy = 0.0;float sumxx = 0.0;float sumyy = 0.0;float sumxy = 0.0;float rho = 0.0;for(i = 0; i < length; i++){  sumx += x[i];  sumy += y[i];  sumxx += x[i] * x[i];  sumyy += y[i] * y[i];  sumxy += x[i] * y[i];    }rho = ((length * sumxy) - (sumx * sumy)) / (sqrtf(((length * sumxx) - (sumx * sumx)) * ((length * sumyy) - (sumy * sumy))));

 

 

the problem starts right at the beginning by calculating sumxx...

 

my x-array contains: { 1, 4, 2105,.....}

 

stepping through the function gives the sumxx values { 1, 17, -25406, ...}

 

the first two values are correct, but why does the third cracks up? should be something like 4431042... or something like 4.43102E06

 

because of the values the correlation becomes inverted, sometimes completely wrong and sometimes greater 1 :/

 

i got another realization of a correlation implemented which calculates correctly with same values but is far more runtime unfriendly. there the values go up toe 8,xyzE07 without a problem...

 

whats the problem with summing up floats that way?

Outcomes