The following code displays -2.38419e-07 .... Why?
(Assuming your system uses IEEE-754 binary32 floating point) It just so happens that none of those floating point literals can be represented exactly on your system. You are not adding together those numbers that you've written. You're adding together these numbers, which are representable, and are quite close to your literals, but not exactly:
4.584129810333251953125
1.2249100208282470703125
-2.1051700115203857421875
-3.7038700580596923828125
In short: Not all numbers are representable in finite floating point representations, and if your input values cannot be represented exactly, then there will be precision errors in your floating point calculations. There are other causes for floating point accuracy errors too, which don't necessarily apply here.
However, on replacing int(arr[i]) with (arr[i]/1), it shows [output 0]. Why?
Because arr[i]/1
is same as arr[i]
and arr[i] - arr[i]
is 0
and 0 + 0 + 0 + 0
is 0
.
If you calculate something else, then it should hardly be surprising that the result is something else.