I keep seeing students use float
in lab lessons:
For example, a simple physics calculation of kinematics. Many, many students use float
to hold the variables:
float y,g,t,v
etc.
In this specific lab, they idea is for them to see:
$$y(t_{n+1})=y(t_n)+\frac{\Delta y}{\Delta t}\Delta t\approx y(t_n)+v(t_n)\,(t_{n+1}-t_n)$$ with $$v(t_{n+1})\approx v(t_n)+g*(t_{n+1}-t_n)$$
and then run over times $\{t_i\}_{i=1}^n$ and see that the $y$ values they get are the same as:
$$y(t_n)\approx y(t_1)+v(t_1)\,t_n+\frac{1}{2}g\,t_n^2$$
However, because they use float
, they get rounding errors when dealing with small time differences ($\Delta t$). I don't want to get into the whole floating point arithmetics field, because they won't understand that (the word "exponent" would frighten them, to say the least).
On the other hand, if they use double
, then the rounding errors wouldn't be significant (because they aren't dealing with $\Delta t \le 10^{-5}$ of a second). So I try to explain that they should use double
, but just stating it as a "rule of thumb" isn't a good idea (because there are cases where one would prefer float
)
So, what explanation, as to why they should use double
for these things, would be compelling?
Better yet, is there any way I can show the students such an explanation? (Perhaps an online, interactive demonstration of floating point precision etc. - I couldn't find one)