Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

浮点数精度问题 #1

Open
seaswalker opened this issue Jan 15, 2019 · 0 comments
Open

浮点数精度问题 #1

seaswalker opened this issue Jan 15, 2019 · 0 comments

Comments

@seaswalker
Copy link
Owner

精度问题出现的本质原因是浮点数无法精确的表示大多数十进制小数,以下面的计算为例:

System.out.println(81.6 * 10);
System.out.println(81.6 * 100);

其输出结果是:

816.0
8159.999999999999

数字81.6在写出来之后就不是精确表示了,随后的计算所以也随之不准确。下面的除法是准确的:

System.out.println(816 / 100D);

因为对于整数,816和100都是可以精确表示的,所以最后的结果便是对的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant