Calling Test1 in Excel gives 0 for any real A and B. Why does this occur?
Public Function Min(X As Double, y As Double, Optional y2 As Double, Optional y3 As Double) As Double
Min = Application.WorksheetFunction.Min(X, y, y2, y3)
End Function
Function Test1(A As Double, B As Double)
Test1 = Min(A, B)
End Function
In Excel: =Test1(5,2).
Optional parameters use their default value if not passed into the function.
For doubles the default value is 0. if you do not provide y2 or y3 essentially, you are testing Application.WorksheetFunction.Min(A,B,0,0).
If you want to change the default value, you can do something like this:
Public Function Min(X As Double, y As Double, Optional y2 As Double = 10000000, Optional y3 As Double = 10000000) As Double
Min = Application.WorksheetFunction.Min(X, y, y2, y3)
End Function
which makes y2 and y3 default to 10000000 which would be your new built in artificial min.
Related
I have to calculate the azimuth between two points given in latitude and longitude
is this fynction correct?
Function azimut(lat1, lat2, lon1, lon2)
azimut = WorksheetFunction.Degrees(WorksheetFunction.Atan2(
Cos(Application.WorksheetFunction.Radians(lat1)) *
Sin(Application.WorksheetFunction.Radians(lat2)) -
Sin(Application.WorksheetFunction.Radians(lat1)) *
Cos(Application.WorksheetFunction.Radians(lat2)) *
Cos(Application.WorksheetFunction.Radians(lon2 - lon1)),
Sin(Application.WorksheetFunction.Radians(lon2 - lon1)) *
Cos(Application.WorksheetFunction.Radians(lat2))))
End Function
Assuming your formula is correct (since I interpret it to the code below without checking it), then here is the code:
Function Azimuth(lat1 As Single, lat2 As Single, lon1 As Single, lon2 As Single) As Single
Dim X1 As Single, X2 As Single, Y As Single, dX As Single, dY As Single
With Application.WorksheetFunction
X1 = .Radians(lat1)
X2 = .Radians(lat2)
Y = .Radians(lon2 - lon1)
End With
dX = Math.Cos(X1) * Math.Sin(X2) - Math.Sin(X1) * Math.Cos(X2) * Math.Cos(Y)
dY = Math.Cos(X2) * Math.Sin(Y)
With Application.WorksheetFunction
Azimuth = .Degrees(.Atan2(dX, dY))
End With
End Function
Well, even if the formula turns out to be incorrect, at least the code above should give you the idea to start with.
I have the overloaded function
Vector3D^ Vector3D:: operator + (Vector3D^ toAdd) // Adding 2 vectors
{
Vector3D^ temp = gcnew Vector3D();
temp->x = x + toAdd->x;
temp->y = y + toAdd->y;
temp->z = z + toAdd->z;
return temp;
}
But whenever I call
double x1 = Convert::ToDouble(txtAx->Text);
double x2 = Convert::ToDouble(txtBx->Text);
double y1 = Convert::ToDouble(txtBx->Text);
double y2 = Convert::ToDouble(txtBx->Text);
double z1 = Convert::ToDouble(txtBx->Text);
double z2 = Convert::ToDouble(txtBx->Text);
Vector3D vectorA(x1, y1, z1);
Vector3D vectorB(x2, y2, z2);
Vector3D temp;
temp = vectorA + vectorB;
I get a no operator matches these functions errors. Every website suggests doing it this way.
I wrote user-defined function called myaverage, which calculates weighted average of 3 numbers:
Function myaverage(x, y, z) As Double
Dim a As Single
Dim x As Double
Dim y As Double
Dim z As Double
a = 0.4 * x + 0.5 * y + 0.1 * z
myaverage = a
End Function
But if I type =myaverage(A1:A3), I cannot see the result.
Simplify your function to something like this:
Function myaverage(x As Double, y As Double, z As Double) As Double
myaverage = 0.4 * x + 0.5 * y + 0.1 * z
End Function
In Excel cell, use this:
= myaverage(A1, B1, C1)
You had double declarations. I mean, x was alreaday declared in the signature and you re-declared it in the function body. So this will work:
Public Function myaverage(x As Double, y As Double, z As Double) As Double
myaverage = 0.4 * x + 0.5 * y + 0.1 * z
End Function
Do not forget the function must be placed in Module (e.g. Module1). See Step 2 in this tutorial.
If you change the code, always re-test your function for language errors using menu Debug > Compile. If errors are shown, fix them.
If you want to use range A1:C1 as the input argument then UDF has to be written accordingly:
Public Function RangeAverage(rRef As Range) As Double
With rRef
RangeAverage = 0.4 * .Cells(1).Value2 + 0.5 * .Cells(2).Value2 + 0.1 * .Cells(3).Value2
End With
End Function
I'm trying to find the number that gives me the result, here's the equation:
x=y mod z
y=?
In this equation, I know values of x and z but I need to find of y too, does anyone has an idea?
There are many solutions to your problem because mapping integers to values in ring Zz (z integer numbers: 0, 1, ..., z - 1) is not a bijection:
y = { x + n * z : n is integer }
So, the simplest function that will provide you with an answer can be as simple as that:
Function solution( ByVal x As Integer) As Integer
Return x
End Function
You can also write something that will return you next possible solution:
Function solution_next( ByVal x As Integer, ByVal z As Integer) As Integer
Static n As Integer = 0
i + z * n
n += 1
Return i
End Function
You can adjust this further given more conditions.
I have a function:
Public Function F(ByVal a As Double, ByVal b As Double,
ByVal c As Double, ByVal x As Double) As Double
y = ax ^ 3 + bx ^ 2 + cx + d
Return y
End Function
How could I create a function that allows me to read parameters a,b,c,d and return an unary function? For example a=1,b=1,c=3,d=4:
Public Function F(ByVal x As Double) As Double
y = 1x ^ 3 + 2x ^ 2 + 3x + 4
Return y
End Function
or in other words, how could I create a function that returns a function of type
Func(Of Double, Double)
Use a lambda function.
Public Function F(ByVal a As Double, ByVal b As Double, ByVal c As Double) As Func(of Double, Double)
Return Function(ByVal x As Double) As Double
Return ax ^ 3 + bx ^ 2 + cx + d
End Function
End Function