问题
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