How much precision does your answer require to be a valid answer? If you're doing floating point math, the precision of each step in your algorithm can be off by a small amount and if those errors accumulate you may get a slightly wrong answer.
A common method of debugging is to print out or manually evaluate the numerical value, along with the expected value of your algorithm at each step. You'll be able to very efficiently discover precisely what step is going wrong using this method, or if you'd like to schedule my help I'd be happy to take a look at it.