Can floating-point data types be used to store exact numbers at all?

萝らか妹 提交于 2019-12-13 05:18:33

问题


If I directly (not getting it as a result of a calculation), explicitly assign 0.0001 to a variable of a Double type, is it guaranteed to be exactly equal to 0.0001 wherever it goes?


回答1:


No... because it's simply impossible to represent 0.0001 as a (finite) binary fraction - you'll get a rounding error when the literal is compiled or interpreted.

However, integer numbers can be stored without rounding - up to the limit of precision, which is of course lower than for an integer type of the same size. And when the limit is reached, instead of an overflow, you start to lose precision, so at some point d == d+1



来源:https://stackoverflow.com/questions/8376908/can-floating-point-data-types-be-used-to-store-exact-numbers-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!