浮点数精度问题

Open
#1 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
20/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
java
Domain
documentation

Research direction

The issue contains Java examples explaining floating-point precision, but it names no file, test, or entry point and does not request a specific change. First identify where this explanation belongs in the repository; completion is undefined until the desired documentation update and its location are specified.

Written by the indexing model from the issue text.

Description

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

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都是可以精确表示的,所以最后的结果便是对的。

Dominant language
Java
Stars
2k
Forks
717
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from seaswalker/jdk-sourcecode-analysis

All issues in seaswalker/jdk-sourcecode-analysis

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.