 # Weird float math

I am writing a custom library. While debugging it i placed some “Serial.println”.
The whole thing is weird…
One code chunk:

``````Serial.println(proportions_Sgn);
Serial.println(proportions_Sgn);

float acceleratingDistance_Pr = speedPin_PrDivS * accelerationTime_S;
float *distanceSegments_Pr = new float;
float speed_PrDivS;
if (acceleratingDistance_Pr < distanceTotal_Pr) {
distanceSegments_Pr = distanceTotal_Pr * 0.5F;
distanceSegments_Pr = 0;
duration_S = sqrt(distanceTotal_Pr / accelerationPin_PrDivSPow2);
duration_S = 0;
speed_PrDivS = duration_S * accelerationPin_PrDivSPow2;
}
else {
distanceSegments_Pr = acceleratingDistance_Pr * 0.5F;
distanceSegments_Pr = distanceTotal_Pr - acceleratingDistance_Pr;
duration_S = accelerationTime_S;
duration_S = distanceSegments_Pr / speedPin_PrDivS;
speed_PrDivS = speedPin_PrDivS;
}

for (uint8_t a = 0; a < DriverCount; a++) {
accelerationPin_SgnPrDivSPow2[a] = accelerationPin_PrDivSPow2 * proportions_Sgn[a];
speed_SgnPrDivS[a] = speed_PrDivS * proportions_Sgn[a];
for (uint8_t b = 0; b < 2; b++)
distanceSegments_SgnPr[a][b] = distanceSegments_Pr[b] * proportions_Sgn[a];
}
Serial.println(proportions_Sgn);
Serial.println(proportions_Sgn);
``````

Returns in serial:
1.00
1.00
0.00
1.00
And I have Absolutely no idea what is happening.  The thing that is worse is that if I add two prints more:

``````Serial.println(proportions_Sgn);
Serial.println(proportions_Sgn);

float acceleratingDistance_Pr = speedPin_PrDivS * accelerationTime_S;
float *distanceSegments_Pr = new float;
float speed_PrDivS;
if (acceleratingDistance_Pr < distanceTotal_Pr) {
distanceSegments_Pr = distanceTotal_Pr * 0.5F;
distanceSegments_Pr = 0;
duration_S = sqrt(distanceTotal_Pr / accelerationPin_PrDivSPow2);
duration_S = 0;
speed_PrDivS = duration_S * accelerationPin_PrDivSPow2;
}
else {
distanceSegments_Pr = acceleratingDistance_Pr * 0.5F;
distanceSegments_Pr = distanceTotal_Pr - acceleratingDistance_Pr;
duration_S = accelerationTime_S;
duration_S = distanceSegments_Pr / speedPin_PrDivS;
speed_PrDivS = speedPin_PrDivS;
}
Serial.println(proportions_Sgn);
Serial.println(proportions_Sgn);

for (uint8_t a = 0; a < DriverCount; a++) {
accelerationPin_SgnPrDivSPow2[a] = accelerationPin_PrDivSPow2 * proportions_Sgn[a];
speed_SgnPrDivS[a] = speed_PrDivS * proportions_Sgn[a];
for (uint8_t b = 0; b < 2; b++)
distanceSegments_SgnPr[a][b] = distanceSegments_Pr[b] * proportions_Sgn[a];
}
Serial.println(proportions_Sgn);
Serial.println(proportions_Sgn);
``````

Returns in Serial:
1.00
1.00
1.00
1.00
1.00
1.00
As if the program changed how things are calculated somehow?

I have been struggling with this for past day, and I am at wit’s end

I have been struggling with this for past day, and I am at wit's end

When you see different results when moving code around, it is almost always because somewhere in the code you didn't post, you are writing beyond the end of an array.

``````float *distanceSegments_Pr = new float;
``````

Why do you need to dynamically allocate memory?

Where do you free this memory?

The whole thing is weird incomplete...

don't post snippets..

you are messing around with arrays for which we have no size information... you might overflow somewhere

for example it seems you have 2 entries in `proportions_Sgn` but you access `proportions_Sgn[a]` with `a` varying between `0` and `DriverCount-1`... do you have `DriverCount` equals to 2 ?

with the info you shared, we can't say