I have a one dimensional numpy.array:
a = array([352305, 506833, 35384, 32278, 22453])
By dividing all elements in this array by an integer t = 949253
, I get another array b:
b = a/ti
Then after setting b[0] = 0
, I let b[0] = -sum(b)
.
Now the new array b should sum up to zero by doing sum(b)
; however, I can only get a very little number such as -2.0816681711721685e-17
. Is there any direct or indirect way to get exactly zero (not doing something like sum(b).round(5)
)?
I've tried something I learned from another answers using Decimal module
. This is what I did:
for bi in b:
bi = Decimal(bi).quantize(Decimal('.01'), rounding=ROUND_UP)
But I still get nonzero sum(b)
.
Additionally, when I did b[0]+sum(b[1:len(b)])
, I got 0.0
. Why is it different from the result of sum(b)?
Finally, if more precision couldn't work, is there any way to throw away some precision such as truncating the floating number and adjust them to sum up to exactly zero?