Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:06:47+00:00 2026-06-17T23:06:47+00:00

So we have a rates calculator class in our ASP.NET4 web app that uses

  • 0

So we have a rates calculator class in our ASP.NET4 web app that uses the Microsoft.VisualBasic.Financial.Rate to calculate a nominal rate (based on input parameters).

We noticed that for high values of NPer (total number of payment periods, e.g. 50 years x monthly payments = 600) the function would throw an exception: Cannot calculate rate using the arguments provided.

Searching around we did not find any solutions to this, so I am posting the solution here. A requirement for us was to maintain a function that as closely as possible implemented the same algorithm as the above, as we needed to produce exactly the same outputs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:06:49+00:00Added an answer on June 17, 2026 at 11:06 pm

    Answering my own question, for any future coders who run into this problem – we used dotPeek to decompile the module, which produced the following:

    public static double Rate(double NPer, double Pmt, double PV, double FV = 0.0, DueDate Due = DueDate.EndOfPeriod, double Guess = 0.1)
    {
      if (NPer <= 0.0)
        throw new ArgumentException(Utils.GetResourceString("Rate_NPerMustBeGTZero"));
      double Rate1 = Guess;
      double num1 = Financial.LEvalRate(Rate1, NPer, Pmt, PV, FV, Due);
      double Rate2 = num1 <= 0.0 ? Rate1 * 2.0 : Rate1 / 2.0;
      double num2 = Financial.LEvalRate(Rate2, NPer, Pmt, PV, FV, Due);
      int num3 = 0;
      do
      {
        if (num2 == num1)
        {
          if (Rate2 > Rate1)
            Rate1 -= 1E-05;
          else
            Rate1 -= -1E-05;
          num1 = Financial.LEvalRate(Rate1, NPer, Pmt, PV, FV, Due);
          if (num2 == num1)
            throw new ArgumentException(Utils.GetResourceString("Financial_CalcDivByZero"));
        }
        double Rate3 = Rate2 - (Rate2 - Rate1) * num2 / (num2 - num1);
        double num4 = Financial.LEvalRate(Rate3, NPer, Pmt, PV, FV, Due);
        if (Math.Abs(num4) < 1E-07)
          return Rate3;
        double num5 = num4;
        num1 = num2;
        num2 = num5;
        double num6 = Rate3;
        Rate1 = Rate2;
        Rate2 = num6;
        checked { ++num3; }
      }
      while (num3 <= 39);
      throw new ArgumentException(Utils.GetResourceString("Financial_CannotCalculateRate"));
    }
    
    private static double LEvalRate(double Rate, double NPer, double Pmt, double PV, double dFv, DueDate Due)
    {
      if (Rate == 0.0)
        return PV + Pmt * NPer + dFv;
      double num1 = Math.Pow(Rate + 1.0, NPer);
      double num2 = Due == DueDate.EndOfPeriod ? 1.0 : 1.0 + Rate;
      return PV * num1 + Pmt * num2 * (num1 - 1.0) / Rate + dFv;
    }
    

    We can see the error is thrown if num3 is exceeded, as it has a hard limit at 39. We tidied up the code a little, and increased the limit to 100:

    private static double CalculateUpfrontNominalRate(double numberOfPeriods, double payment, double presentValue, double futureValue = 0.0, DueDate Due = DueDate.EndOfPeriod, double Guess = 0.1)
        {
            if (numberOfPeriods <= 0.0)
            {
                throw new ArgumentException("CalculateUpfrontNominalRate: Number of periods must be greater than zero");
            }
    
            var rateUpperBoundary = Guess;
            var lEvalRate1 = LEvalRate(rateUpperBoundary, numberOfPeriods, payment, presentValue, futureValue, Due);
            var rateLowerBoundary = lEvalRate1 <= 0.0 ? rateUpperBoundary * 2.0 : rateUpperBoundary / 2.0;
            var lEvalRate2 = LEvalRate(rateLowerBoundary, numberOfPeriods, payment, presentValue, futureValue, Due);
    
            for (var i = 0; i < 100; i++)
            {
                if (lEvalRate2 == lEvalRate1)
                {
                    if (rateLowerBoundary > rateUpperBoundary)
                        rateUpperBoundary -= 1E-05;
                    else
                        rateUpperBoundary -= -1E-05;
    
                    lEvalRate1 = LEvalRate(rateUpperBoundary, numberOfPeriods, payment, presentValue, futureValue, Due);
                    if (lEvalRate2 == lEvalRate1)
                    {
                        throw new ArgumentException("CalculateUpfrontNominalRate: Inputs will cause a divsion by zero");
                    }
                }
    
                double temporaryRate = rateLowerBoundary - (rateLowerBoundary - rateUpperBoundary) * lEvalRate2 / (lEvalRate2 - lEvalRate1);
                double lEvalRate3 = LEvalRate(temporaryRate, numberOfPeriods, payment, presentValue, futureValue, Due);
    
                if (Math.Abs(lEvalRate3) < 1E-07)
                {
                    return temporaryRate;
                }
    
                lEvalRate1 = lEvalRate2;
                lEvalRate2 = lEvalRate3;
                rateUpperBoundary = rateLowerBoundary;
                rateLowerBoundary = temporaryRate;
            }
    
            throw new ArgumentException("CalculateUpfrontNominalRate: The maximum number of iterations has been exceeded, unable to calculate rate");
        }
    
        private static double LEvalRate(double Rate, double NPer, double Pmt, double PV, double dFv, DueDate Due)
        {
            if (Rate == 0.0)
                return PV + Pmt * NPer + dFv;
            double num1 = Math.Pow(Rate + 1.0, NPer);
            double num2 = Due == DueDate.EndOfPeriod ? 1.0 : 1.0 + Rate;
            return PV * num1 + Pmt * num2 * (num1 - 1.0) / Rate + dFv;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web app that lets a client define a daily rate for
I have a class that uses sockets to send and receive data asynchronously over
I have implemented a file transfer rate calculator to display kB/sec for an upload
I have a sql table of payroll data that has wage rates and effective
Do older iPods and iPhones have a frame rate of 60fps? I'm finding that
I have create the class that following, that calculating the time before a time
I have a bit of a design issue. I created a rate calculator as
I am trying to get an exchange rate from the iGoogle Calculator. I have
I need to create a Mortgage Calculator for my Java class, and I have
I'm building a certain type of calculator that does some stuff with interest rates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.