Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need help with my code. Rather than repost everything here, please visit my posting over here which is unresolved. It contains the code which works and here below is the link to the FFEIC tool used to audit banks in the USA. There is a downloadable pdf document via the help link on the tool which shows the math involved for the "general equation". It's at the bottom of the document and the simple example is on page 6. My code has to compute the exact same apr as this tool. See my post and how I tried to add an outer loop. I'm using arrays to store the payments and number of payments.


See my posting at this forum here. The code is posted here and this is unresolved for me.

Public Overridable Function generalEquation1(ByVal period As Integer, ByVal numpmts As ArrayList, ByVal payment As ArrayList, ByVal initialPeriods As Double, ByVal fractions As Double, ByVal rate As Double) As Double
        Dim retval As Double = 0
        Dim x As Integer = 0
        Dim z As Integer = 0
        For y = 0 To numpmts.Count - 1 'number of payment streams
            For x = 0 To numpmts(y)
                If x = numpmts(y) Then
                    x = 0
                    Exit For
                End If
                retval += payment(y) / ((1.0 + fractions * rate) * Math.Pow(1 + rate, initialPeriods + x))
                z = z + 1
            Next x
        Next y
        Return retval
    End Function

    ''' <param name="amount" /> The initial amount A 
    ''' <param name="payment" /> The periodic payment P 
    ''' <param name="payments" /> The total number of payments n 
    ''' <param name="ppy" /> The number of payment periods per year 
    ''' <param name="APRGuess" /> The guess to start estimating from, 10% is 0.1, not 0.001 
    ''' <param name="odddays" /> Odd days, as a fraction of a pay period.  10 days of a month is 0.33333... 
    ''' <param name="full" /> Full pay periods before the first payment.  Usually 1. 
    ''' <returns> The calculated APR 
    Public Overridable Function findAPRGEQ1(ByVal amount As Double, ByVal payment As ArrayList, ByVal payments As ArrayList, ByVal ppy As Double, ByVal APRGuess As Double, ByVal odddays As Double, ByVal full As Double) As Double
        Dim result As Double = APRGuess
        Dim tempguess As Double = APRGuess
        Dim totpmts As Integer = 0
        Dim info As Decimal = 0
        For i = 0 To payments.Count - 1
            totpmts += payments(i)
            result = tempguess
            'Step 1
            Dim i As Double = tempguess / (100 * ppy)
            Dim A1 As Double = generalEquation1(totpmts, payments, payment, full, odddays, i)
            'Step 2
            Dim i2 As Double = (tempguess + 0.1) / (100 * ppy)
            Dim A2 As Double = generalEquation1(totpmts, payments, payment, full, odddays, i2)
            'Step 3
            tempguess = tempguess + 0.1 * (amount - A1) / (A2 - A1)
            info = Math.Abs(result * 10000 - tempguess * 10000) > 1
        Loop While Math.Abs(result * 10000 - tempguess * 10000) > 1
        Return result
    End Function

What I have tried:

added outer loop. See original code and my modified code at the link provided.
Updated 26-Oct-22 13:09pm
Richard MacCutchan 26-Oct-22 9:47am    
We are not going to follow links to other sites. Please provide all the required information (properly formatted) in your question above.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900