If sum is 0 there is something wrong with your histogram.
I think you should handle the special case above in the containing function.