I have this code: http://pastebin.com/Sd9WKZFr
When i call something like rate(60, -6000, 120000) it returns me a NAN result, but the same function on MS Excel returns me 0,04678.... I have the same problem trying -5000, -4000, -3000 and -2000.
When i debug the code, i see that about the 8/9 iteration, the line number 29 begins to return a NAN result, making all of other results to turn NAN too.
BUT, when i call something like rate(60, -1000, 120000) it returns me a float -0.02044..., exactly the same result of MS Excel.
I have already tryed to convert all of math calculations into BCMath functions, but this way the results of -6000 is wrong (-1.0427… instead of 0,04678…) but using -1000 the result is correct, matching excel’s result.
Is there a way to make it work correctly?
Thanks in advance for any useful sight about that.
I’ll need to do some tests to ensure that there’s no adverse effects in other situations; but the following looks as though it might fix this problem, and certainly calculates the correct rate value for your arguments RATE(60, -6000, 120000) stabilises at 0.046781916422493 in iteration 15.