I have a bunch of data to which I've assigned units using Quantity, including some angular measurements in degrees. I'd like to be able to use these measurements with trigonometric functions, but it seems that trig functions don't "know" about Quantity, so I'm forced to convert those measurements to radians and then apply the trig function to the magnitudes, like this:

x = Quantity[23.4, "AngularDegrees"]
Cos[QuantityMagnitude @ UnitConvert[x, "Radians"]]

This works, but it's slow and seems clunky, at best. Is there a more elegant (and hopefully faster) approach?

  • 3
    $\begingroup$ What happens if you try x /. Quantity[a_, "AngularDegrees"] :> a °? $\endgroup$ Commented Apr 20, 2013 at 1:27
  • $\begingroup$ Have you tried using N? $\endgroup$
    – Spawn1701D
    Commented Apr 20, 2013 at 1:40
  • $\begingroup$ @J.M.: That's certainly faster--more than an order of magnitude in my quick check. $\endgroup$
    – Cassini
    Commented Apr 20, 2013 at 3:36
  • $\begingroup$ @Spawn1701D: How would I use N? $\endgroup$
    – Cassini
    Commented Apr 20, 2013 at 3:36
  • $\begingroup$ I don't have installed v9 yet so I can't verified it directly but i was thinking of something like N[Cos[x]]. $\endgroup$
    – Spawn1701D
    Commented Apr 20, 2013 at 3:48

1 Answer 1


As noted in the comments, one might consider avoiding UnitConvert[] for this purpose:

lp = LatitudeLongitude[GeoPosition[{40.11, -88.26}]]
   {Quantity[40.11, "AngularDegrees"], Quantity[-88.26, "AngularDegrees"]}

RepeatedTiming[UnitConvert[lp, "Radians"]]
   {0.00018, {Quantity[0.700052, "Radians"], Quantity[-1.54043, "Radians"]}}

RepeatedTiming[lp /. Quantity[x_?NumericQ, "AngularDegrees"] :> x °]
   {0.000013, {0.700052, -1.54043}}

For converting angles in DMS format, the method is a bit more elaborate:

sp = StarData["Polaris", "Declination"]
   Quantity[MixedMagnitude[{89, 15, 50.76`2.1985422820612186}],
            MixedUnit[{"AngularDegrees", "ArcMinutes", "ArcSeconds"}]]

sp /. Quantity[MixedMagnitude[m_],
               MixedUnit[{"AngularDegrees", "ArcMinutes", "ArcSeconds"}]] :> FromDMS[m] °

Despite the complexity, this is still faster than UnitConvert[].


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.