Throw some tracing messages into your code to track the numbers to figure out the solution.
That or use a debugger. I like to run through the code myself first, (it helps, I think, to understand what's going on) but if I'm stumped I use a debugger to see what's really happening.