我有一个通过一系列乘法计算得出的浮点数数组。该数组中的数字之一超过了float可以处理的最大数字。当我使用此方法在NSTextView中显示所有数字时:
- (void)updateResultLabel:(NSArray<NSNumber *> *)results {
NSMutableString *string = [NSMutableString stringWithCapacity:100];
for (NSNumber *number in results) {
[string appendFormat:@"%f\n", number.floatValue];
}
self.resultLabel.string = string;
}
我得到inf
了溢出的数字。我想让它显示更用户友好的文本。我尝试在调试器中键入以下命令:
print string
断点self.resultLabel.string = string;
在线时。它给了我这个:
(NSBigMutableString *) $0 = 0x0000618000041ec0 class name = NSBigMutableString
我试图用[string isEqualToString:@"inf"]
那没用。您如何检查它是否具有inf字符串,以便可以将其更改为更用户友好的字符串?
INFINITY
为此使用解决方案。用法示例是
float t = 1231231231889989899898232131231232312312.323132123123131231231231231231231321;
if (t == INFINITY) {
NSLog(@"inf it is");
}
它定义math.h
为
#define INFINITY HUGE_VALF
在你的情况下
for (NSNumber *number in results) {
if(number.floatValue != INFINITY)
[string appendFormat:@"%f\n", number.floatValue];
else {
//set the other text.
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句