|
Boost : |
From: Leland Brown (lelandbrown_at_[hidden])
Date: 2006-06-09 17:40:43
Paul A Bristow <pbristow <at> hetp.u-net.com> writes:
> | (And BTW, it did find several bugs in my
> | computations by flagging dimensions problems!)
>
> Can you elaborate a little on the value of using a dimensional analysis by
> sharing some of these with us?
One example I was able to dig up was in approximating a function's derivatives
using finite differences. In simplified form, my code had looked something
like this:
length f( time t );
...
t3_quantity t = // some time value
t3_quantity dt = // some time value
...
t3_quantity x = f(t);
t3_quantity xdot = f(t+dt) - x; // incorrect!
...
velocity v = xdot; // runtime error caught here!
The erroneous line should have been:
t3_quantity ydot = ( f(t+dt) - x ) / dt;
I had left out the division, which changes the units.
Incidentally, when using t1_quantity or t2_quantity types, the error would be
caught at compile-time, and on the line actually containing the error:
time t = // some time value
time dt = // some time value
...
length x = f(t);
velocity xdot = f(t+dt) - x; // error here - caught by compiler!
(Because these errors are caught at compile-time, and thus usually before the
code is checked in, the fixes don't appear in the change history, so I can't
produce actual examples I've encountered for those cases.)
Another example is my mistake in an earlier post, where I tried to show code
taking sqrt(acceleration/length) to get a time value. But actually it gives a
frequency, so if the error were not corrected I'd get the reciprocal of the
value I want. Dimensional analysis would prevent the invalid assignment from
occurring.
-- Leland
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk