Skip to content

浮点数精度问题 #1

Description

@seaswalker

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions