acos和cos与度数没有给出正确的结果

Mru*_*ank 1 math trigonometry objective-c ios

我写下面的代码

NSLog(@"%f",acos(cos(1)));
Run Code Online (Sandbox Code Playgroud)


它返回1结果但是如果我试图从度而不是弧度获得结果然后面临如下问题

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
NSLog(@"%f",acos(cos(DEGREES_TO_RADIANS(1)))); 
Run Code Online (Sandbox Code Playgroud)


然后它返回0.017453而不是1.
如果我写下面的话

NSLog(@"%f",acos(cos(DEGREES_TO_RADIANS(1)))*180/M_PI);
Run Code Online (Sandbox Code Playgroud)


然后我得到了正确1的结果.
但如果我写下面的话

NSString *a1 = [NSString stringWithFormat:@"%f",(cos(DEGREES_TO_RADIANS(1)))];
NSString *a2 = [NSString stringWithFormat:@"%f",(acos([a1 doubleValue])*180/M_PI)];
NSLog(@"%f",[a2 doubleValue]);
Run Code Online (Sandbox Code Playgroud)

然后我得到0.998999了结果.
然后问题在哪里?
请帮我

Kev*_*vin 5

0.017453是关于一度有多少弧度.

如果您希望输出日志行1,则需要转换回度数.

#define RADIANS_TO_DEGREES(angle) ((angle) * 180.0 / M_PI)
NSLog(@"%f", RADIANS_TO_DEGREES(acos(cos(DEGREES_TO_RADIANS(1)))));
Run Code Online (Sandbox Code Playgroud)

请注意,由于三角函数的精度有限以及一般浮点数,您可能会得到0.9999的结果.