'Convert from Radians to Degrees Wrong
I need to convert radians to degrees. I have tried this by x*180/Pi the problem is my output angles are in degrees minutes seconds so the small error in conversion leads to big problems.
For example: (aCos(0)*180)/Pi = 90.00000250
In the base 10 universe it should by 90.00000000
Solution 1:[1]
Use the following macro in your code to convert radians to degrees:
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
Example:
NSLog(@"Output radians as degrees: %f", RADIANS_TO_DEGREES(0.785398));
via this link.
Solution 2:[2]
The right solution is to import GLKit
Then you can use the following 2 functions already implemented by GLKit :GLKMathDegreesToRadians()GLKMathRadiansToDegrees()
With GLKit the values returned by the functions are good !
Objective-C :
import <GLKit.h>
float angleInRadian = GLKMathDegreesToRadians(360.0);
// Return 6.2831854820
float angleInDegrees = GLKMathRadiansToDegrees(angleInRadian);
// Return 360.0000000000
Swift :
import GLKit
let angleInRadian: Float = GLKMathDegreesToRadians(360.0)
// Return 6.2831855
let angleInDegrees: Float = GLKMathRadiansToDegrees(angleInRadian)
// Return 360.0
Solution 3:[3]
This worked for me. You can also #import <GLKit/GLKit.h>
and use GLKMathDegreesToRadians() or GLKMathRadiansToDegrees().
-(CGFloat) degreesToRadians:(CGFloat) degrees {
return degrees == 0.0 ? 0: degrees * M_PI / 180;
}
-(CGFloat) radiansToDegrees:(CGFloat) radians {
return radians == 0.0 ? 0: radians * 180 / M_PI;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | The Kraken |
| Solution 2 | |
| Solution 3 | Pedro Trujillo |
