Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?

前端 未结 14 2015
名媛妹妹
名媛妹妹 2020-11-21 23:41
double r = 11.631;
double theta = 21.4;

In the debugger, these are shown as 11.631000000000000

14条回答
  •  感情败类
    2020-11-21 23:50

    According to the javadoc

    "If at least one of the operands to a numerical operator is of type double, then the
    operation is carried out using 64-bit floating-point arithmetic, and the result of the
    numerical operator is a value of type double. If the other operand is not a double, it is
    first widened (§5.1.5) to type double by numeric promotion (§5.6)."

    Here is the Source

提交回复
热议问题