The code finds how many steps is needed to walk the distance, using the constant.
float k = 1256.29; //Steps required to walk a distance unit
float dist = 100;
unsigned long steps = dist*k;
float cPos = 0; //Target position
float realPos = 0; //Real position
void setup() {
Serial.begin(115200);
cPos = steps * (1/k);
for(unsigned long i = 0; i < steps; i++)
{
realPos += (1/k);
}
Serial.println(cPos);
Serial.println(realPos);
}
When the code runs, the calculated distance (cPos) hits 100.0, but for some reason, the realPos hits 99.93. I know it is very close, but I need it a little bit more precise.
You have defined k in terms of fractional steps, but the variable steps is declared as an unsigned long. That is, the variable steps will always hold an integer number of steps, no fractions.
Besides, maybe I do not really understand the problem. In the sentence "I know it is very close, but I need it a little bit more precise," the word "it" occurs twice but I don't really understand what is being referenced.
You're getting drift. 1/k doesn't convert perfectly into a binary real number, so it is approximated as well as it can be in 4 bytes of data. But when you add it repeatedly, many, many, many times, that very small difference gets multiplied. By the time you have added it 125629 times, it's that far off.
This is not an Arduino specific problem. Running that calculation in a C program on your PC will give you the same results. The difference is that on your PC, you can simply declare your float variables as double variables, and your precision gets much better. On the Arduino, doubles are just turned into floats, and you're stuck with the precision you get.
This program looks like it was designed to exhibit this problem. To eliminate the drift, you are supposed to do the actual formula calculation inside the for loop. Like so:
for(unsigned long i = 0; i < steps; i++)
{
realPos = i / k;
// Do useful stuff here with realPos
}
In most cases, you actually use realPos in the loop, and this will give you accurate data in the loop. And, at the end of the loop, when i is equal to steps, the result will be the same as cPos. But then it's the exact same calculation, so it should be.