c++ /clrIntelliSense: no operator "+" matches these operands. System::Double ^ + System::Double ^ - c++-cli

String ^ name;
Double^ math;
Double ^ physics;
Double ^ english;
Double ^ chemistry;
Double ^ cs;
Double ^ avrg;
Double ^ grade;
name = this->name->Text;
math = Convert::ToDouble(this->textmath->Text);
physics = Convert::ToDouble(this->phis->Text);
english = Convert::ToDouble(this->eng->Text);
chemistry = Convert::ToDouble(this->chem->Text);
cs = Convert::ToDouble(this->cstf->Text);
Double^ sum = (math + physics + english + chemistry + cs) / 5;
On the last operation with Double ^sum an error appears:
1 IntelliSense: no operator "+" matches these operands
operand types are: System::Double ^ + System::Double ^
Can you help me in finding an answer? Thanks!

Don't use objects!
String ^ name = this->name->Text;
Double math = Convert::ToDouble(this->textmath->Text);
Double physics = Convert::ToDouble(this->phis->Text);
Double english = Convert::ToDouble(this->eng->Text);
Double chemistry = Convert::ToDouble(this->chem->Text);
Double cs = Convert::ToDouble(this->cstf->Text);
Double sum = (math + physics + english + chemistry + cs) / 5.0;

Related

Calculating distance in kilometers between coordinates

I'm trying to calculate distance in kilometers between two geographical coordinates using the haversine formula.
Code:
Dim dbl_dLat As Double
Dim dbl_dLon As Double
Dim dbl_a As Double
dbl_P = WorksheetFunction.Pi / 180
dbl_dLat = dbl_P * (dbl_Latitude2 - dbl_Latitude1)
dbl_dLon = dbl_P * (dbl_Longitude2 - dbl_Longitude1)
dbl_a = Sin(dbl_dLat / 2) * Sin(dbl_dLat / 2) + Cos(dbl_Latitude1 * dbl_P) * Cos(dbl_Latitude2 * dbl_P) * Sin(dbl_dLon / 2) * Sin(dbl_dLon / 2)
dbl_Distance_KM = 6371 * 2 * WorksheetFunction.Atan2(Sqr(dbl_a), Sqr(1 - dbl_a))
I'm testing with these coordinates:
dbl_Longitude1 = 55.629178
dbl_Longitude2 = 29.846686
dbl_Latitude1 = 37.659466
dbl_Latitude2 = 30.24441
And the code returns 20015.09, which is obviously wrong. It should be 642 km according to Yandex maps.
Where am I wrong? Are the longitude and latitude in wrong format?
As far as I can tell, the issue is that the order of arguments to atan2() varies by language. The following works* for me:
Option Explicit
Public Sub Distance()
Dim dbl_Longitude1 As Double, dbl_Longitude2 As Double, dbl_Latitude1 As Double, dbl_Latitude2 As Double
dbl_Longitude1 = 55.629178
dbl_Longitude2 = 29.846686
dbl_Latitude1 = 37.659466
dbl_Latitude2 = 30.24441
Dim dbl_dLat As Double
Dim dbl_dLon As Double
Dim dbl_a As Double
Dim dbl_P As Double
dbl_P = WorksheetFunction.Pi / 180
dbl_dLat = dbl_P * (dbl_Latitude2 - dbl_Latitude1) 'to radians
dbl_dLon = dbl_P * (dbl_Longitude2 - dbl_Longitude1) 'to radians
dbl_a = Sin(dbl_dLat / 2) * Sin(dbl_dLat / 2) + _
Cos(dbl_Latitude1 * dbl_P) * Cos(dbl_Latitude2 * dbl_P) * Sin(dbl_dLon / 2) * Sin(dbl_dLon / 2)
Dim c As Double
Dim dbl_Distance_KM As Double
c = 2 * WorksheetFunction.Atan2(Sqr(1 - dbl_a), Sqr(dbl_a)) ' *** swapped arguments to Atan2
dbl_Distance_KM = 6371 * c
Debug.Print dbl_Distance_KM
End Sub
*Output: 2507.26205401321, although gcmap.com says the answer is 2512 km. This might be a precision issue --- I think it's close enough to count as working. (Edit it might also be that gcmap uses local earth radii rather than the mean radius; I am not sure.)
Explanation
I found this description of the haversine formula for great-circle distance, which is what you are implementing. The JavaScript implementation on that page gives this computation for c:
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
In JavaScript, atan2() takes parameters y, x. However, in Excel VBA, WorksheetFunction.Atan2 takes parameters x, y. Your original code passed Sqr(dbl_a) as the first parameter, as it would be in JavaScript. However, Sqr(dbl_a) needs to be the second parameter in Excel VBA.
A comment on naming
Building on #JohnColeman's point, there are lots of ways to name variables. In this case, I would recommend using the prefixes for unit rather than for type: e.g., deg_Latitude1, RadPerDeg = Pi/180, and rad_dLat = RadPerDeg * (deg_Latitude2 - deg_Latitude1). I personally think that helps avoid unit-conversion mishaps.
My VBA code that returns the answer in feet; However 'd' is the answer in kilometers.
Imports System.Math
Module Haversine
Public Function GlobalAddressDistance(sLat1 As String, sLon1 As String, sLat2 As String, sLon2 As String) As String
Const R As Integer = 6371
Const cMetersToFeet As Single = 3.2808399
Const cKiloMetersToMeters As Integer = 1000
Dim a As Double = 0, c As Double = 0, d As Double = 0
'Convert strings to numberic double values
Dim dLat1 As Double = Val(sLat1)
Dim dLat2 As Double = Val(sLat2)
Dim dLatDiff As Double = DegreesToRadians(CDbl(sLat2) - CDbl(sLat1))
Dim dLonDiff As Double = DegreesToRadians(CDbl(sLon2) - CDbl(sLon1))
a = Pow(Sin(dLatDiff / 2), 2) + Cos(DegreesToRadians(dLat1)) * Cos(DegreesToRadians(dLat2)) * Pow(Sin(dLonDiff / 2), 2)
c = 2 * Atan2(Sqrt(a), Sqrt(1 - a))
d = R * c
'Convert kilometers to feet
Return Format((d * cKiloMetersToMeters * cMetersToFeet), "0.##").ToString
End Function
Private Function DegreesToRadians(ByVal dDegrees As Double) As Double
Return (dDegrees * PI) / 180
End Function
End Module

Finding the x intercept of a 4th degree polynomial using small increments

I am trying to find the x intercept of a 4th degree function by incrementing the x value. I feel like this way doesnt work always and isnt the most efficient way to do this, is there another way I am missing?
My code is:
Sub Findintercept()
Dim equation As Double, x As Double, A As Double, B As Double, C As Double, D As Double, E As Double
A = 0.000200878
B = -0.002203704
C = 0.0086
D = -0.02333
E = 0.02033
x = 0
equation = A * x ^ 4 + B * x ^ 3 + C * x ^ 2 + D * x + E
While (equation > 0.00001 Or equation < -0.00001)
If (x > 5) Then
MsgBox "Could not find intercept"
equation = 0
Else
x = x + 0.0001
equation = A * x ^ 4 + B * x ^ 3 + C * x ^ 2 + D * x + E
End If
Wend
MsgBox x
End Sub
Sometimes it fails to find the intercept hence the IF condition in the while loop. (Im always expecting the intercept to be less than 5!
Your method suffers from two problems:
You assume a step size to change x. The step could be too large, causing you to "walk past" the value your are looking for. To deal with this, you make a small step size, which can mean an excessively large number of iterations are needed to find the solution.
You always assume the same direction to change x. Even with seemingly small values for your step size, you could "walk past" the solution, and have no means to change direction. Or, your initial guess may be on the wrong side of the solution, and you never find an answer.
The Newton-Raphson method handles both of these issues neatly. You do still need to choose your initial guess somewhat close to the root you are looking for.
This method does have potential problems, but for polynomials such as the one you are dealing with, it is quite good.
Below is a simple VBA sub that implements this method. It solves your problem in 4 iterations. I recommend adjusting the initial guess (xii) a lot to see how it impacts the solution you get.
Sub SimpleNewtonRaphson()
Const Tol As Double = 1E-06
Const MaxIter As Long = 50
Dim xi As Double, xii As Double, deriv As Double
Dim IterCount As Long
' Initialize
xi = 0#
xii = 1#
IterCount = 0
' Method
Do While IterCount < MaxIter And Abs(xii - xi) > Tol
xi = xii
deriv = myDeriv(xi)
If deriv = 0# Then Exit Do
xii = xi - myFunc(xi) / deriv
IterCount = IterCount + 1
Loop
' Results
If deriv = 0 Then MsgBox "Ran into a 0 derivative, modify initial guess"
If IterCount >= MaxIter Then MsgBox "MaxIterations reached"
If Abs(xii - xi) <= Tol Then MsgBox "Solution found #" & vbCrLf & "F(" & xii & ") = " & myFunc(xii)
End Sub
... and two VBA functions for your equation and it's derivative ...
Function myFunc(x As Double) As Double
Const A As Double = 0.000200878
Const B As Double = -0.002203704
Const C As Double = 0.0086
Const D As Double = -0.02333
Const E = 0.02033
myFunc = A * x ^ 4 + B * x ^ 3 + C * x ^ 2 + D * x + E
End Function
Function myDeriv(x As Double) As Double
Const A As Double = 0.000200878
Const B As Double = -0.002203704
Const C As Double = 0.0086
Const D As Double = -0.02333
myDeriv = 4 * A * x ^ 3 + 3 * B * x ^ 2 + 2 * C * x + D
End Function

Creating a line that follows the cursor with a set length

I'm trying to make a line with a fixed x1/y1, and when the user moves the cursor, its other end goes to the cursor. "cannon" is the line. When I try to use it, the line does indeed follow the cursor, but it's got a large offset and is inverted. I'm not really sure what's wrong with it, but it's probably very obvious.
Dim angle as Double = Math.Atan(Math.Abs(Cursor.Position.Y - cannon.Y1) / Math.Abs(Cursor.Position.X - cannon.X1))
Dim h As Double = Math.Sqrt((Math.Abs(Cursor.Position.X - cannon.X1) ^ 2) + Math.Abs(Cursor.Position.Y - cannon.Y1) ^ 2)
Dim a As Double = h * Math.Cos(angle)
Dim o As Double = h * Math.Sin(angle)
cannon.X2 = cannon.X1 + a
cannon.Y2 = cannon.Y1 - o
I suspect that Cursor.Position gives the mouse coordinate relative to the top left corner of your monitor - as does MousePosition.Try this..
PointToClient is a function that calculates the position of the mouse relative to your program window.
Dim angle as Double = Math.Atan(Math.Abs(Me.PointToClient(MousePosition).Y - cannon.Y1) / Math.Abs(Me.PointToClient(MousePosition).X- cannon.X1))
Dim h As Double = Math.Sqrt((Math.Abs(Me.PointToClient(MousePosition).X - cannon.X1) ^ 2) + Math.Abs(Me.PointToClient(MousePosition).Y - cannon.Y1) ^ 2)
Dim a As Double = h * Math.Cos(angle)
Dim o As Double = h * Math.Sin(angle)
cannon.X2 = cannon.X1 + a
cannon.Y2 = cannon.Y1 - o

Calling Overloaded Operator Function

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.

vb basic text is not a member of double

I have a problem of conversion. When I write txta1 = txta1.Text, it shows "text is not a member of double." I'm not sure why, can someone explain this to me?
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim treatments As String
Dim ssb, ssw, msb, msw, sigmaA, sigmaB, sigmaC, sigmaabc, squaresigmaA, squaresigmaB, squaresigmaC, squaresigmaabc, f As Double
Dim txta1, txta2, txta3, txta4, txta5, txtb1, txtb2, txtb3, txtb4, txtb5, txtc1, txtc2, txtc3, txtc4, txtc5 As Double
'Data Input
txta1 = txta1.Text
treatments = lblTreatments.Text
'Calaculation
sigmaA = txta1 + txta2 + txta3 + txta4 + txta5
sigmaB = txtb1 + txtb2 + txtb3 + txtb4 + txtb5
sigmaC = txtc1 + txtc2 + txtc3 + txtc4 + txtc5
sigmaabc = sigmaA + sigmaB + sigmaC
squaresigmaA = txta1 ^ 2 + txtb2 ^ 2 + txtb3 ^ 2 + txtb4 ^ 2 + txtb5 ^ 2
squaresigmaB = txtb1 ^ 2 + txtb2 ^ 2 + txtb3 ^ 2 + txtb4 ^ 2 + txtb5 ^ 2
squaresigmaC = txtc1 ^ 2 + txtc2 ^ 2 + txtc3 ^ 2 + txtc4 ^ 2 + txtc5 ^ 2
squaresigmaabc = squaresigmaA + squaresigmaB + squaresigmaC
ssb = ((sigmaA) ^ 2 / 5 + (sigmab) ^ 2 / 5 + (sigmac) ^ 2 / 5) - (sigmaabc) ^ 2 / 15
ssw = squaresigmaabc - ((sigmaA) ^ 2 / 5 + (sigmab) ^ 2 / 5 + (sigmac) ^ 2 / 5)
msb = ssb / (3 - 1)
msw = ssw / (15 - 1)
f = msb / msw
'Data output
lblSSB.Text = ssw
lblSSW.Text = ssw
lblMSB.Text = msb
lblMSW.Text = msw
lblF.Text = f
End Sub
As the compiler is trying to tell you, you declared txta1 as a Double, which has no Text property.
You should choose different names for your controls and your local variables.
You've declared txta1 as a Double in line 4. I assume you also have a TextBox called txta1 on your form. Use a different name like txta1Value for the Double, so it doesn't clash with the name of the TextBox. Then you can say
txta1Value = Double.Parse(txta1.Text)
(This will blow up if someone types something other than a valid Double; how you want to deal with that is up to you.)