简体   繁体   中英

Radians to degrees with a roundoff value between 0-360 degress

I want to convert radians to degrees with a round off function. The degrees should come between 0-360 for all the radian values.

Say, the value I want to convert is 300(in radians)

So,

300*57.3 = 17190 degrees

No. of rotation on the plane = 17190/360 = 47.75

I wrote a radtodeg function:

radtodeg <- function(rad) {(rad * 180) / (pi)}                          
radtodeg(300)

How should I round off a degree equivalent to 17190 between 0 to 360.

Anyone with the round off function, please?

Use the modulus operator %% in your function if you want the answer to be modulo 360 (ie always between 0 and 360)

radtodeg <- function(rad) {((rad * 180) / (pi)) %% 360}

radtodeg(pi)
#> [1] 180

radtodeg(2 * pi)
#> [1] 0

radtodeg(3 * pi)
#> [1] 180

radtodeg(17190)
#> [1] 314.4498

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM