All,
When programming a teensy 3.2, should I use floats or doubles for variable declaration? I see double is double precision, and float is single, and from what I read, the compiler uses double precision. I've been using floats, but I wounder if it should change to using doubles. I'm not really doing precise calculations, i'm more concerned over some internal conversion
Thanks
In the Teensh 3.2/LC, both
float and
double are handled via software emulation. In the Teensy 3.5/3.6,
float is handled in hardware, and
double is handled via software emulation. In AVR processors (Arduino Uno, Teensy 2.0, etc.), the compiler treats
double as
float.
Because the Teensy 3.5/3.6 has hardware support for
float, Paul has turned on the GCC switch that says all floating point constants are
float instead
double. This prevents the compiler from having to convert
float expressions to
double because you used a floating point constant in an expression. The downside of this decision is if you are using a
double double constant and expecting it to be a full double, you will find that it does not have the full precision. The work around is to use the suffix "
L" which creates a
long double constant (you could use lower case "
l", but that often looks like a one). On the 32-bit ARM chips, the compiler does not provide a larger (80/128-bit)
long double type, and instead uses the same representation for
double and
long double.
On the Teensy 3.2/LC, the emulation routines for
float are much faster than the emulation routines for
double. So, if you don't need the accuracy, I would say use
float everywhere. I would suggest adding a "
F" or "
f" suffix to all constants which makes them explicitly
float instead of
double, and may allow your code to be moved to other platforms easier.
If you use the math functions like '
sin', '
cos', '
sqrt', etc. you should use the version that takes
float inputs and returns a
float result. Because these return less precision, they are faster than the
double version. For most of the math functions, you just append a "
f" suffix to the call, i.e. '
cosf', '
sinf', '
sqrtf', etc.