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

前端 未结 14 2298
名媛妹妹
名媛妹妹 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:53

    These accuracy problems are due to the internal representation of floating point numbers and there's not much you can do to avoid it.

    By the way, printing these values at run-time often still leads to the correct results, at least using modern C++ compilers. For most operations, this isn't much of an issue.

提交回复
热议问题