Math Error, Maybe

When I divide one number by another the result is around 1% off. Here's what I've got:

float SampleSum = 0;           // The sum of last 240 measurements.
float numSamples = 0;          // The number of samples in the array.

d is declared at the start of the function as in:

  float d = (p * depth1psi)+0.05; // Convert pressure to depth. d is uncalibrated depth at dock from sensor.

And then just prior to the print statements:

if (Sptr >= 240) {                 // Is true after 240 samples taken.
    Sptr = 0;                        // Wrap Sptr to 0 when 240.
    d = SampleSum / numSamples;  // Compute average of last 240 samples.  
}

Serial.print(d);
Serial.print("  ,  ");
Serial.print(SampleSum);
Serial.print("  ,  ");
Serial.println(numSamples);

And the results:

14.34 , 2218.91 , 158.00
14.34 , 2233.25 , 159.00
14.15 , 2247.40 , 160.00
13.74 , 2261.14 , 161.00
13.80 , 2274.94 , 162.00
14.04 , 2288.98 , 163.00

Pick any line and using a calculator divide SampleSum by numSamples and the result is off by around 1% or more. WTF? I was expecting a soft answer using float but nothing like this.

Suggestions?

Data type for p and depth1psi are not known.

Do not post snippets.

If Sptr<240, then the value of d will not be updated, so your Serial.print() statements will show current values of SampleSum and numSamples, paired with an old value of d. The calculation is accurate in the last line of your sample output:

14.04 , 2288.98 , 163.00

Post your Software Without that we do not have a clue as to how it is expected to operate.

My Process:

When I see a question, I spend a moment assessing it. If it’s missing critical information, I might ask for it. However, if it's repeatedly lacking important details, I may assume the questioner is not serious and move on to another query.

What You Need to Consider:

We don’t know your skill level or what resources you have available. If you’re missing key technical details or seem unprepared, it may indicate that you need to spend more time learning the basics before starting your project.

Providing the right information upfront will help you get the best possible assistance and avoid the frustration of running into dead ends. Let us help you by sharing what you have clearly and completely!

Try reading this: How to get the best out of this forum

Bingo. Thanks grb.

Glad I could help, @wingsy. To mark your question as answered, and point future readers of this thread in the right direction, please click the :white_check_mark: Solution button below the comment that solved the problem.

Isn't the number of samples always a whole number, an integer? float divided by int is a float.

Can't tell about the first sum with sample #158, but the subsequent results are the previous SampleSum plus the current sample value d.

# sample sum avg
158 14.34 2,218.91 14.043734
159 14.34 2,233.25 14.045597
160 14.15 2,247.40 14.046250
161 13.74 2,261.14 14.044348
162 13.80 2,274.94 14.042840
163 14.04 2,288.98 14.042822

For the last one, 2274.94 + 14.04 = 2288.98. By the time you've got 163 samples, the average has converged around 14.04, and that last sample may be just coincidentally about the same. (Only printing two decimal places for those.)

So maybe nothing is wrong, especially since you never got to 240. After the rollover happens, how do you preserve the sum of the "last" 240? Are you keeping an array of 240 and using it like a ring buffer; then adding them all every time? Or minimize the math: keep the sum up-to-date by subtracting the one that "fell off" before adding the new one?

Yes, that's what Im doing.