precision - Java double addition rounding off -
in java following expression results into
new double(1.0e22) + new double(3.0e22) = 4.0e22
but
new double(1.0e22) + new double(4.0e22) = 4.9999999999999996e22
i expecting 5.0e22
. double limit 1.7976931348623157e308
. appreciate help. machine's architecture x64 , jvm 64 bit.
welcome planet of floating point units. unfortunately, in real world, have give precision speed , breadth of representation. cannot avoid that: double
approximate representation. actually, cannot represent number finite precision. still, it's approximation: less 0.00000000001% error. has nothing double
upper limits, rather cpu limits, try doing more math python:
>>> 4.9999999999999996 / 5. 1.0 >>> 5. - 4.9999999999999996 0.0
see? side note, never check equality on double
, use approximate equality:
if ((a - b) < epsilon)
where epsilon
small value. java library has more appropriate, idea.
if insterested in theory, standard floating point operations ieee754
Comments
Post a Comment