Difference in Macro vs Manual Output on same formula using LOG - excel

Well its rather a very strange question
I have a macro which generates the delta of a Option(d1):
Function dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
dOne = (Log(UnderlyingPrice / ExercisePrice) + (Interest - Dividend + (0.5 * Volatility ^ 2)) * Time) / (Volatility * (Sqr(Time)))
End Function
When I pass the the values to it, it generates the desired output:
However when I try to replicate this in Excel, it gives an entirely different output
I know that the calculations for output generated manually are correct.
However the desired values are those generated from VBA.
Please suggest what am I missing here.

The Log function in VBA is the natural log: ln(x).
The LOG function in the formula is log base 10: log10(x).
If you want log base 10 in VBA you will have to use the logarithmic identity for converting bases:
Log(x)/Log(10)
In your case
dOne = (Log(UnderlyingPrice / ExercisePrice) / Log(10) + (Interest - Dividend + (0.5 * Volatility ^ 2)) * Time) / (Volatility * (Sqr(Time)))

Related

Dealing with numbers outside of uint256 range

i am trying to build compound formula with solidity but I have came across a problem, given enough days, the amount is compounded, amount will overflow uint256 range.
I have read that as long as final result is in the range of uint256, it should be fine but it does not seem to be the case. here is the calculation that i am trying to do
(100 * (100+8)^100) * 10^(6 - 2*100)
as a formula it looks like this:
( BASE * ( (1 * 10^MULTIPLIER) + (YIELD * 10^MULITPLIER) ) ^ COMPOUNDED_DAYS ) * ( 10 ^ (DECIMALS - MULTIPLIER * COMPOUNDED_DAYS) )
as mentioned this is compound formula with 6 decimals, but when i run this in playground i get "0" as a result. what's the problem, and if there is problem how can i fix it?
The reason is that your number becomes smaller than one and the integer turns it into zero
The caret sign ^ represents a bitwise XOR - not an exponential multiplication. If you want to calculate "to the power of", use two asterisks **.
Example: "10 to the power of 2" in Solidity: 10 ** 2

Divide by a number which is not power of 2 in Verilog RTL coding

For multiplication and division, we can use the left and right shifts.
x>>2 // it will right shift by 2. ---> 2^2=4. (Multiply by 4 or divide by 4, depends on MSB/LSB)
However, if we want to divide by a number that isn't the power of 2, how can we achieve the required purpose?
Booth's algorithm is an additive one and can take a comparatively longer time than the multiplicative algorithms, like the Newton-Raphson algorithms found in this educational PDF.
Each next approximation is calculated using the previous approximation.
X(N+1) = X(N)(2 - b * X(N)), where x(0)=1
So, to find the inverse of b, i.e. 1/b, where b=0.6 (error=e(x)), it takes about 5 iterations.
X(000) = 1.000
X(001) = 1.000 * (2 - (1.000 * 0.6)) = 1.400
X(002) = 1.400 * (2 - (1.400 * 0.6)) = 1.624
X(003) = 1.624 * (2 - (1.624 * 0.6)) = 1.6655744
X(004) = X(003) * (2 - (X(003) * 0.6)) = 1.666665951
X(005) = X(004) * (2 - (X(004) * 0.6)) = 1.666666668
which approximates the answer, which is 1.6666666667.
I included this example in case the referenced PDF disappears. See the referenced PDF or look up the Newton-Raphson algorithm for more information.
By using Booth's restoring division algorithm

How does the Pow() function work in Python in a mathematical equation?

Creating a mortgage Calculator, including 3 programmer-specified inputs (amount borrowed, interest rate, and 30 year payback period).
I am trying to turn this mathematical equation
(p * (1 + r)n * r) / ((1 + r)n - 1)
into code using variables, n will represent the squared user input
then I have to print out a summary of the all three inputs and the sum of the above equation.
First I imported math and tried the following
math.pow(an, 2)
int(pow(an, 2))
int(math.pow(an, 2))
Even when I tried without the pow function it wouldn't work
(amt * (1+per)an** * per)/((1+per)an** - 1)
Below is my current code:
import math
#input values
amt = input("Amount Borrowed:")
per = input("Annual interest rate:")
an = input("Payback period:")
#output values
mo = (amt * (1+per)pow(an, 2) * per)/((1+per)pow(an, 2) - 1)
#summary
print("Amount Borrowed: ", amt)
print("Annual Interest Rate:", per)
print("Total Years of Payments:", an)
print("Your monthly mortgage payment is: {mo:.2f}")
Example of expected results:
Amount borrowed (programmer input) = $270000
Annual interest rate (programmer input) = 5.125%
Payback period (programmer input) = 30 Years
Monthly payment (calculated output) = $1470.11
Errors:
mo = (amt * (1+per)pow(an, 2) * per)/((1+per)pow(an, 2) - 1)
SyntaxError: invalid syntax
mo = (amt * (1+per)an** * per)/((1+per)an** - 1)
SyntaxError: invalid syntax
You can't write a(b) to multiply a and b. Python syntax doesn't exactly match typical math syntax.
You're trying to do that when you write
(1+per)pow(an, 2)
You need to explicitly put a * in there if you want to multiply:
(1+per) * pow(an, 2)

Division of Python Complex Numbers

I am trying to divide two complex numbers and not getting the desired result. How does division work for Python Complex numbers ?
Input values are: (2+3j) and (2+3j)
I have tried the below logic and this does not seem to work.
numerator_real = (real * conjugate.real) - (imag * conjugate.imag); // real
numerator_imag = (real * conjugate.imag) + (imag * conjugate.real); // imag
denom = x.real * x.real + x.imag * x.imag;
complex(numerator_real/denom, numerator_imag/denom)
Expected/Desired Result is: 1+0j
My output is 1.3+0j

VBA Excel sine cosine functions

I am trying to write a short trig function wanting to use sine and cosine functions in VB and I am very new to this as it's my first time coding in VB.
See what I'm trying to accomplish below:
Function XXX(HR, HR_RL, abc)
Const PI As Double = "3.141593"
AC = Sqr(HR ^ 2 + HR_RL ^ 2 - 2 * HR * HR_RL * Cos(abc * PI / 180))
VB is complaining about two things Cos and Sqr.
It seems they are both built-in VBA functions, but somehow I can't get them evaluated and as a result, AC variable = empty.
In fact, it doesn't seem that it even goes past "Cos(abc * PI / 180)". If I substitute "Cos(abc * PI / 180)" with a numeric value (just to see where else is another problem), surely enough, now VB complains about "Sqr".
Go into your VBA Editor menu and select Tools -> References..., then check the System library. It will give you access to all the math functions.

Resources