r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
172 stars 27 forks source link

Unitless quantitities and functions (particularly exp) #328

Closed chrisbrunsdon closed 1 year ago

chrisbrunsdon commented 1 year ago

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.

Enchufa2 commented 1 year ago

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.

chrisbrunsdon commented 1 year ago

Thanks for the reply. And apologies, the second issue was an oversight on my part - if I enter for example: x <- set_units(pi/2,'rad') sin(x) then I get no warning, and the units of the result are '1' - ie unitless. That issue was down to my misunderstanding - thanks for putting me right! Chris

On 17 Nov 2022, at 13:50, Iñaki Ucar @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/r-quantities/units/issues/328#issuecomment-1318590075, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEOTPYTIVSBVDQEYLTH5T3WIYS25ANCNFSM6AAAAAASDGLNBQ. You are receiving this because you authored the thread.

Enchufa2 commented 1 year ago

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.

edzer commented 1 year ago

I think that warning should disappear, and the implicit assumption that unitless implies radians is good, just as with sin(3/2).

edzer commented 1 year ago

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.

Enchufa2 commented 1 year ago

but we don't cast to radians in case of unitless.

Yes, that's what I meant, thanks for pointing this out.

CGMossa commented 1 year ago

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