Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 DDMathParser 制作一个计算器应用程序。我面临的问题sin(pi())不是返回 0,而是返回一个微不足道的数字,例如 1.2246...e-16。我只是想知道有没有办法将其转换为0?谢谢你。
sin(pi())
-descriptionon 方法NSNumber不能准确地表示底层数字:
-description
NSNumber
NSLog(@"%f", sin(M_PI)); // logs 0.000000 NSLog(@"%@", @(sin(M_PI))); // logs 1.224646799147353e-16
要解决此问题,您可以拔出-doubleValue,NSNumber这应该给您0:
-doubleValue
0
NSLog(@"%f", @(sin(M_PI)).doubleValue); // logs 0.000000