Objective-C浮点加法错误 [英] Objective-C floating point addition error

查看:135
本文介绍了Objective-C浮点加法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在Objective-C中浮动的问题


已经把这个问题简化到了我可以得到的那么简单。随意尝试相同的事情,并告诉我,如果你得到相同的错误,你可能有什么解决方案。

  float total = 200000.0f + 154196.8f;我已经在几台电脑上试过了。 
NSLog(@total:%f,total);

输出结果为:

 总计:354196.812500 

如果有人有任何合理的解释,分享它。

解决方案

我建议你刷上你的花车



http://www.altdevblogaday.com / 2012/05/20 / thats-not-normal-performance-of-odd-floats /



如果您需要更高的精确度,请使用double。 / p>

另外 http://randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero-to-100-digits-2/


Possible Duplicate:
Trouble with floats in Objective-C

I have broken this problem down to about as simple as i can get it. Feel free to try the same thing and tell me if you get the same error and what solution you might have. I have already tried it on several computers.

float total = 200000.0f + 154196.8f;
NSLog(@"total: %f", total);

The output is:

total: 354196.812500

If anyone has any sort of logical explanation, feel free to share it.

解决方案

I'd suggest you brush up on your floats

http://www.altdevblogaday.com/2012/05/20/thats-not-normalthe-performance-of-odd-floats/

If you need higher precision use a double.

Additionally http://randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero-to-100-digits-2/

这篇关于Objective-C浮点加法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆