I want to see the source code of Microsoft.Visualbasic.Financial.IRR
.
How can I decompile it to see the source code of the IRR method of financial class?
System{" df -k | awk '{sum += $4 }i; END {print sum} '"};
above line is not useful
I've tried to view it using different software but it will not work for me. Is there any software that will view the source code?
I've used ILSpy(it's free) to decompile the type Microsoft.Visualbasic.Financial.IRR
and it's method IRR:
' Microsoft.VisualBasic.Financial
Public Shared Function IRR(ByRef ValueArray As Double(), Optional Guess As Double=0.1) As Double
Dim upperBound As Integer
Try
upperBound = ValueArray.GetUpperBound(0)
Catch ex As StackOverflowException
Throw ex
Catch ex2 As OutOfMemoryException
Throw ex2
Catch ex3 As ThreadAbortException
Throw ex3
Catch ex_1B As Exception
Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", New String()() = { "ValueArray" }))
End Try
' The following expression was wrapped in a checked-expression
Dim num As Integer = upperBound + 1
If Guess <= -1.0 Then
Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", New String()() = { "Guess" }))
End If
If num <= 1 Then
Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", New String()() = { "ValueArray" }))
End If
Dim num2 As Double
If ValueArray(0) > 0.0 Then
num2 = ValueArray(0)
Else
num2 = -ValueArray(0)
End If
Dim arg_BF_0 As Integer = 0
Dim num3 As Integer = upperBound
Dim i As Integer
' The following expression was wrapped in a checked-expression
i = arg_BF_0
While i <= num3
If ValueArray(i) > num2 Then
num2 = ValueArray(i)
Else
If-ValueArray(i) > num2 Then
num2 = -ValueArray(i)
End If
End If
i = i + 1
End While
Dim num4 As Double = num2 * 1E-07 * 0.01
Dim num5 As Double = Guess
Dim num6 As Double = Financial.OptPV2(ValueArray, num5)
Dim num7 As Double
If num6 > 0.0 Then
num7 = num5 + 1E-05
Else
num7 = num5 - 1E-05
End If
If num7 <= -1.0 Then
Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", New String()() = { "Rate" }))
End If
Dim num8 As Double = Financial.OptPV2(ValueArray, num7)
i = 0
While True
If num8 = num6 Then
If num7 > num5 Then
num5 -= 1E-05
Else
num5 += 1E-05
End If
num6 = Financial.OptPV2(ValueArray, num5)
If num8 = num6 Then
Exit While
End If
End If
num5 = num7 - num7 - num5 * num8 / num8 - num6
If num5 <= -1.0 Then
num5 = num7 - 1.0 * 0.5
End If
num6 = Financial.OptPV2(ValueArray, num5)
If num5 > num7 Then
num2 = num5 - num7
Else
num2 = num7 - num5
End If
Dim num9 As Double
If num6 > 0.0 Then
num9 = num6
Else
num9 = -num6
End If
If num9 < num4 AndAlso num2 < 1E-07 Then
Return num5
End If
num2 = num6
num6 = num8
num8 = num2
num2 = num5
num5 = num7
num7 = num2
' The following expression was wrapped in a checked-statement
i += 1
If i > 39 Then
GoTo Block_17
End If
End While
Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValue"))
Block_17:
Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValue"))
End Function
While I realize this question is quite old, based on the number of views I believe there are many others searching for this. Now that Reference Source is available, we can view this method directly:
'-------------------------------------------------------------
'
' Name : IRR
' Purpose : This function uses an iterative procedure to find
' the Internal Rate of Return of an investment. The algorithm
' basically uses the secant method to find a rate for which the
' NPV of the cash flow is 0.
' This function raises an exception if the parameters are invalid.
'
' This routine uses a slightly different version of the secant
' routine in Rate. The basic changes are:
' - uses LDoNPV to get the 'Y-value'
' - does not allow Rate to go below -1.
' (if the Rate drops below -1, it is forced above again)
' - has a double condition for termination:
' NPV = 0 (within L_IT_EPSILON)
' Rate1 - Rate0 approaches zero (rate is converging)
' This last does not parallel Excel, but avoids a class of
' spurious answers. Otherwise, performance is comparable to
' Excel's, and accuracy is often better.
'
' Returns : Double
'
'-------------------------------------------------------------
'
Public Function IRR(ByRef ValueArray() As Double, Optional ByVal Guess As Double = 0.1) As Double
Dim dTemp As Double
Dim dRate0 As Double
Dim dRate1 As Double
Dim dNPv0 As Double
Dim dNpv1 As Double
Dim dNpvEpsilon As Double
Dim dTemp1 As Double
Dim lIndex As Integer
Dim lCVal As Integer
Dim lUpper As Integer
'Compiler assures that rank of ValueArray is always 1, no need to check it.
'WARSI Check for error codes returned by UBound. Check if they match with C code
Try 'Needed to catch dynamic arrays which have not been constructed yet.
lUpper = ValueArray.GetUpperBound(0)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch ex As System.Threading.ThreadAbortException
Throw ex
Catch
Throw New ArgumentException(GetResourceString(ResID.Argument_InvalidValue1, "ValueArray"))
End Try
lCVal = lUpper + 1
'Function fails for invalid parameters
If Guess <= (-1.0#) Then
Throw New ArgumentException(GetResourceString(ResID.Argument_InvalidValue1, "Guess"))
End If
If lCVal <= 1 Then
Throw New ArgumentException(GetResourceString(ResID.Argument_InvalidValue1, "ValueArray"))
End If
'We scale the epsilon depending on cash flow values. It is necessary
'because even in max accuracy case where diff is in 16th digit it
'would get scaled up.
If ValueArray(0) > 0.0# Then
dTemp = ValueArray(0)
Else
dTemp = -ValueArray(0)
End If
For lIndex = 0 To lUpper
'Get max of values in cash flow
If ValueArray(lIndex) > dTemp Then
dTemp = ValueArray(lIndex)
ElseIf (-ValueArray(lIndex)) > dTemp Then
dTemp = -ValueArray(lIndex)
End If
Next lIndex
dNpvEpsilon = dTemp * cnL_IT_EPSILON * 0.01
'Set up the initial values for the secant method
dRate0 = Guess
dNPv0 = OptPV2(ValueArray,dRate0)
If dNPv0 > 0.0# Then
dRate1 = dRate0 + cnL_IT_STEP
Else
dRate1 = dRate0 - cnL_IT_STEP
End If
If dRate1 <= (-1.0#) Then
Throw New ArgumentException(GetResourceString(ResID.Argument_InvalidValue1, "Rate"))
End If
dNpv1 = OptPV2(ValueArray, dRate1)
For lIndex = 0 To 39
If dNpv1 = dNPv0 Then
If dRate1 > dRate0 Then
dRate0 = dRate0 - cnL_IT_STEP
Else
dRate0 = dRate0 + cnL_IT_STEP
End If
dNPv0 = OptPV2(ValueArray, dRate0)
If dNpv1 = dNPv0 Then
Throw New ArgumentException(GetResourceString(ResID.Argument_InvalidValue))
End If
End If
dRate0 = dRate1 - (dRate1 - dRate0) * dNpv1 / (dNpv1 - dNPv0)
'Secant method of generating next approximation
If dRate0 <= (-1.0#) Then
dRate0 = (dRate1 - 1.0#) * 0.5
End If
'Basically give the algorithm a second chance. Helps the
'algorithm when it starts to diverge to -ve side
dNPv0 = OptPV2(ValueArray, dRate0)
If dRate0 > dRate1 Then
dTemp = dRate0 - dRate1
Else
dTemp = dRate1 - dRate0
End If
If dNPv0 > 0.0# Then
dTemp1 = dNPv0
Else
dTemp1 = -dNPv0
End If
'Test : npv - > 0 and rate converges
If dTemp1 < dNpvEpsilon AndAlso dTemp < cnL_IT_EPSILON Then
Return dRate0
End If
'Exchange the values - store the new values in the 1's
dTemp = dNPv0
dNPv0 = dNpv1
dNpv1 = dTemp
dTemp = dRate0
dRate0 = dRate1
dRate1 = dTemp
Next lIndex
Throw New ArgumentException(GetResourceString(ResID.Argument_InvalidValue))
End Function
You can do that with .NET Reflector, it works for visual basic.
You have 30 days trial
Would you like to step through the original Microsoft source code in the debugger when you call it?
Easy. You can set up Visual Studio to automatically download the source code as required, so that you can step into it, inspect variables, set breakpoints, etc. Instructions