Unitless quantitities and functions (particularly exp)
chrisbrunsdon opened this issue · 7 comments
Although under many circumstances, functions like sin(), exp() are not meaningful for arguments with units, they are if the arguments are the result of an expression involving units that is itself unitless. However, this sometimes leads to issues:
library(units)
x <- set_units(1,'m')
y <- set_units(2,'m')
exp(x/y)
gives an error, and
sin(x/y)
gives a warning: "In Math.units(x/y) : Operation sin not meaningful for units"
However I think both expressions are well defined. The error actually breaks things, however I still think the warning in the situation where the expression is unitless is not appropriate because although the operation is not meaningful for units, in this expression the argument has no units.
Thanks for considering.
Thanks. In the first case, that's an unexpected error, so there's some bug. In the second case, we could treat this as radians.
My point was that this currently raises a warning:
> x <- set_units(3, m)/set_units(2, m)
> sin(x)
[1] 0.997495
Warning message:
In Math.units(x) : Operation sin not meaningful for units
But this doesn't:
> sin(set_units(x, rad))
0.997495 [1]
So the question is should we try to convert to radians the argument of any trigonometric call, or should this be done explicitly by the user.
I think that warning should disappear, and the implicit assumption that unitless implies radians is good, just as with sin(3/2)
.
We already convert to radians:
> sin(set_units(3, degree))
0.05233596 [1]
> sin(set_units(3, rad))
0.14112 [1]
but we don't cast to radians in case of unitless.
but we don't cast to radians in case of unitless.
Yes, that's what I meant, thanks for pointing this out.
Even this is a problem:
> exp(set_units(1, 1))
Error in if (length(u$numerator) > 1 || !grepl("\\(re", u$numerator) || :
missing value where TRUE/FALSE needed