One of my hot summer day activities in Hellaware, sorry Delaware is to read robot code. I’ve come across a few things that I’ve questioned. So I’m asking the developers to answer two simple questions
Why all of the highly precise constant defines?
Why all the doubles?
Constants
We all know that it’s faster to use things like the square root of 2 as constants rather than calling sqrt(2) lots of time.
Which leads to seeing things like this in the code
#define SQRT_2 1.414213562373095048801688724209698078569671875376948073176679737990733
#define PI 3.14159265358979323846264338327950288419716939937510582097494459
//Efficiency or something
Lets take the Pi example. We all know that the diameter of a wheel times Pi gives you the circumference. An 8” wheel has the following circumferences depending on what value of Pi you use.
24” where Pi =3
24.8” where Pi = 3.1
25.12” where Pi is 3.14
25.13” where Pi is 3.1416
Assume we want to run the length of the FRC field (55’) and assume a 4’ long robot from the wall, the front wheel needs to move 51’.
24” where Pi =3 needs to do 25.5 rotations
24.8” where Pi = 3.1 needs to do 24.7 rotations
25.12” where Pi is 3.14 needs to do 24.4 rotations (*)
25.13” where Pi is 3.1416 needs to do 24.25 rotations
Using Pi=3 vs Pi as 3.1416 is a difference of 1.25 rotations, over 2’, but the difference in Pi at 2 decimal places 3.14 vs 3.1416 is 0.15 a rotation or about 3 ½ inches of actual travel after going 55 feet.
Six decimal places (3.141593 reduces the error to about 1/2”. Given friction, battery, carpets, windspeed, etc that is pretty amazing.
Key point is that is at 6 decimal places. Using more that that isn’t really helping that much, you are slowly closing in to the final point, but you are pushing the ability to make it any better.
So my first question is why the high decimal digit constants? This isn’t a space shot (and BTW they make tiny course corrections as they go). Are you really doing something with those extra digits? Or are you just fooling yourself that 3.141592653589793238462643383279 is way better than 3.141593?
Which brings on my second question, why all the doubles? A float on an ARM processor has about 7 decimal digits of accuracy. Unless you are doing a huge number (ie 10K+) of calculations, the rounding error isn’t going to be a problem. On the other hand if you are trying to do real time calculations, the cost of doubles in every calculation can be a burden.
While the sages on Stack Overflow say “Meh, just use double”, my second question is “Why all the calculations as double?”.
Thanks for your time on this hot summer day.
(*) On a 12’ VRC or 8’ VIQ field and 4” stock wheels, Pi = 3.14 is more than enough for the distances they are moving.)