Are you sure my code dient work?  I also fester the code and the max difff was 1e-17 maybe enough to let it fail. I am at my phone at the moment but I think using multiply, sum and than cumsum should do the trick.