On Thu, 2016-07-07 at 21:09 +0100, Clive Page wrote:
> Another advantage of having the tag on the actual argument is that it
> allows expressions in the actual arguments of procedure calls, like
> call some_trig_function( my_angle_in_degs * pi/180.0[rads])
> which your proposal for tagging the variables at the declaration
> point
> seems not to allow. That seems a pity, and might lead to a bit of
> code
> bloat. But maybe that's something else that I didn't understand
> properly?
If your code generally works in, for example degrees, and you have
variables with degree units, and you give them to a trig function that
expects arguments in radians, a compile-time error occurs. If you
define a Degree unit that is a conversion unit related to Radian, you
automatically get its inverse too. So instead of the above you would
write
var = some_trig_function ( radian ( my_angle_in_degs ) )
This seems clearer.
It might or might not inline multiplication by pi/180, depending on your
compiler. If your compiler inlines statement functions, I would expect
it to inline unit conversions too, since they're a trivial subset of
statement functions. In any case, the conversion is explicit, so you
might expect a performance penalty, even if you don't know exactly how
the conversion works until you look at the unit definition.
|