Search code examples
rtrigonometrydegreesradians

r - Is there a way to test if a number is either a radian or degree?


I am in the process of adding tests to my R package iemisc. Previously, I created functions to use degrees rather than radians for trigonometric functions.

Is there a method to determine if a given number is in radians versus degrees?

If so, then I would like to incorporate that method in my tests.

For example, based off of the recent Win-Vector Blog article written by John Mount, http://www.win-vector.com/blog/2019/03/unit-tests-in-r/, and the RUnit vignette, I wrote the following test for cosd:

test_cosd <- function() {

library("iemisc")
RUnit::checkEquals(cosd(360), 1)
RUnit::checkEquals(cosd(90), 0)
RUnit::checkEquals(cosd(0), 1)
RUnit::checkException(cosd(pi * 3 / 4))
RUnit::checkException(cosd("sq"))

  invisible(NULL)
}

However, I would like to have a checkException for numbers that are radian measurements rather than degrees measurements.

Thank you.


Solution

  • It is possible to take advantage of the difference in magnitude between the measures to make reasonable inference about whether the input is degrees or radians. A measurement in degrees is about 60 times the size of the same measurement in radians. This can be seen from the underlying relationship:

    180 degrees = pi (3.14159) radians
    

    There is a simple heuristic possible, if you can safely assume users will use the tool only with angles smaller than a full circle (not unreasonable in many physical product engineering applications, unless corkscrew helices are involved). In that case, it would be safe to assume values greater than 2*pi, or 6.28 represents degrees.

    Otherwise, the values themselves are indistinguishable.