NPER - (domain error occured)

Hi

Hilare assisted with the creation of a NPER Function that works similar to excel.  

This is the NPER Function

create function NPERTEST(@rate float, @payment float,@capital float)
returns float
as
begin
return log10(@payment/(@payment+@capital*@rate))/log10(1+@rate)
end
go

select dbo.NPER(.203/12, 500, -5000)  ---> returns 11.047592792208778

This works well and I haven't noticed a problem until this morning.  Based on the value of the payment the procedure throws out a domain error occurred

Try this

Select dbo.NPERTEST(.203/12, 75, -5000) - Payment amount is R 75

Your urgent assistance is required

Thanks for your help in advance

Regards
TAZI

LVL 1
TAZIAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HilaireCommented:
TAZI, I'm afraid these values are simply not possible ....

Excel fails to get a value too with these parameters
it gives #number instead of a readable value
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TAZIAuthor Commented:
Hi Hilare

Thanks a million for your prompt response.

How would this be handled then.  I didn't test it in Excel, but have been bothered by it all morning.

Basically, the procedure is being used in an Debt Collection Environment. In the example above, the Debtor owes R5000.00 and the interest rate is 20.3%.  If the debtor wants to pay  R 75.00 per month, how would we determine the number of periods then.

The payment amount is dynamic ... one does not know how much the debtor wants to pay.

Thanks for your help in advance
Regards
TAZI
0
HilaireCommented:
when (@payment+(@capital*@rate)) is negative,
log10(@payment+(@capital*@rate)) can't be computed
using microsoft's algorithm ...

best I can do is return a default value (eg 0) when a correct NPER can't be computed

create function NPERTEST(@rate float, @payment float,@capital float)
returns float
as
begin
return case  
               when @payment+@capital*@rate < 0 then 0
               else log10(@payment/(@payment+@capital*@rate))/log10(1+@rate)
         end
end
go
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

HilaireCommented:
Yet a default value of 0 might generate more errors if you use this NPER in other calculations,
so maybe another dummy value like 9999999 would be better.

Anyway you'll have to handle this care somewhere in your code and alert the user on the fact that NPER can't be computed with these parameters values
0
HilaireCommented:
i meant "handle this case"
0
TAZIAuthor Commented:
Hi Hilare

Thank you so much for your help.  

I did the following to overcome the problem. I work out the minimum payment that a debtor can make against his outstanding debt and if the amount that the debtor wants to pay is less then mimimum payment  I know I am going to get an error and return out of the procedure with the minimum value.  I then display this on screen informing the collector that he/she has to capture a value greater then the minimum payment.

Thanks anyway for your help.

It is greately appreciate.

I will assign the 50 points to you .. you deserve it with all the help that you've given me with these procedures.

Thanks
Regards
TAZI
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.