Programador said:
I'm getting this error when running this program:
Cannot calculate rate using the arguments provided
Module Module1
Sub Main()
Rate(360, -694.44444444444446, 244274.69178082192)
End Sub
End Module
This is a vb console application, has anybody seen this before? how can I
fix this?
There is an indispensible .NET tool called Reflector (first result on
google for that word), which disassembles the CLR into your choice of
VB.NET or C#. If you ever need to know what the Framework is doing, or
how it does it, it should be your first port of call. This is its
output when asked to examine the Microsoft.VisualBasic.Financial.Rate
function:
Public Shared Function Rate(ByVal NPer As Double, ByVal Pmt As Double,
ByVal PV As Double, ByVal FV As Double = 0, ByVal Due As DueDate = 0,
ByVal Guess As Double = 0.1) As Double
Dim num2 As Double
If (NPer <= 0) Then
Throw New
ArgumentException(Utils.GetResourceString("Rate_NPerMustBeGTZero"))
End If
Dim num1 As Double = Guess
Dim num4 As Double = Financial.LEvalRate(num1, NPer, Pmt, PV, FV,
Due)
If (num4 > 0) Then
num2 = (num1 / 2)
Else
num2 = (num1 * 2)
End If
Dim num5 As Double = Financial.LEvalRate(num2, NPer, Pmt, PV, FV,
Due)
Dim num6 As Integer = 0
Do While True
If (num5 = num4) Then
If (num2 > num1) Then
num1 = (num1 - 1E-05)
Else
num1 = (num1 - -1E-05)
End If
num4 = Financial.LEvalRate(num1, NPer, Pmt, PV, FV,
Due)
If (num5 = num4) Then
Throw New
ArgumentException(Utils.GetResourceString("Financial_CalcDivByZero"))
End If
End If
num1 = (num2 - (((num2 - num1) * num5) / (num5 - num4)))
num4 = Financial.LEvalRate(num1, NPer, Pmt, PV, FV, Due)
If (Math.Abs(num4) < 1E-07) Then
Return num1
End If
Dim num3 As Double = num4
num4 = num5
num5 = num3
num3 = num1
num1 = num2
num2 = num3
num6 += 1
If (num6 > 39) Then
Throw New
ArgumentException(Utils.GetResourceString("Financial_CannotCalculateRate"))
End If
Loop
End Function
Now, since I know less than nothing about the purpose of this function,
or the meaning of the arguments, I can't say what is going wrong or
why. It looks like the loop in the second part of the function is
repeatedly run until num4 is very close to zero, and if this doesn't
happen within 40 iterations, the Exception you saw is thrown.
Presumably some kind of iterative solution is taking place, and the
fact that it doesn't converge in 40 iterations is used as a signal that
there is no solution.
At any rate, you should be able at the very least to now step through
this code yourself and work out what is going wrong.