+ 1
Why this java code giving this output?
The code is supposed to give 0.500 https://code.sololearn.com/cMr8467ch0d3/?ref=app
1 Answer
0
It's broken in the exact same way the decimal (base-10) notation you learned in grade school is broken, just for base-2.
To understand, think about representing 1/3 as a decimal value. It's impossible to do exactly! In the same way, 1/10 (decimal 0.1) cannot be represented exactly in base 2 (binary) as a "decimal" value;
A repeating pattern after the decimal point goes on forever. The value is not exact, and therefore you can't do exact math with it using normal floating point methods.