我在jon skeet博客上阅读了此书,现在我想知道答案吗? [英] i read it on jon skeet blog and now i want to know the answer?

查看:61
本文介绍了我在jon skeet博客上阅读了此书,现在我想知道答案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
浮点错误示例
浮点比较

Possible Duplicate:
Floating point inaccuracy examples
Floating point comparison

计算机本应擅长算术,不是吗?为什么此打印为"False"?

double d1 = 1.000001;
double d2 = 0.000001;
Console.WriteLine((d1-d2)==1.0); 

否则c Sharp或Java的结果将有所不同

or there result will be different in c sharp or java

推荐答案

我认为结果可能会有所不同,具体取决于您使用的处理器和使用的语言.
这是因为(通常)浮点数与试图表示该小数的许多位存储在一起,但可能会有一些(很小)差异.
因此,当您执行(d1-d2)时,您可能会得到一个确实接近1.0的数字,但不完全是1.0!

I think result may vary depending on which processor and which language you're using.
This because float numbers (in general) are stored with a number of bits that try to represent that fractional number, but there can be some (very little) difference.
So when you do (d1-d2) you probably get a number really close to 1.0, but not exactly 1.0!!

这篇关于我在jon skeet博客上阅读了此书,现在我想知道答案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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