Quadratic Complex roots solver - vb.net

Cannot seem to make the console print out the complex roots correctly.
If quad4ac < 0 Then
Dim quad4acComplex As Long = quad4ac * -1
Dim quadiComplex As Long = -1
Dim quadrComplex As Long
Console.WriteLine(vbNewLine & "Your Roots Are Complexed!" & vbNewLine)
quadrComplex = (-quadCoB / (2 * quadCoA) & "+" & (quad4ac * -1) ^ 0.5 / (2 * quadCoA))
Console.WriteLine(quadrComplex)
Seems to be causing the console to crash.

You are trying to assign a string value to a long value and then print the long value. I don't really know what you are trying to do. In case you want to print out the calculated value try:
quadrComplex = CType((-quadCoB / (2 * quadCoA) + (quad4ac * -1) ^ 0.5 / (2 * quadCoA)), Long)
Console.Writeline(quadrComplex.Tostring)
In case you want to print out the expression try
Dim quadrComplex as String
quadrComplex = "(-" & quadCoB.Tostring & " / (2 * " & quadCoA.ToString & ") + (" & quad4ac.ToString & " * -1) ^ 0.5 / (2 * " & quadCoA.ToString & "))"
Console.Writeline(quadrComplex)
You should consider enabling Option Strict in your program. That way you are forced by the compiler to use the correct type conversions yourself. This avoids many many errors.

What are you trying to do with ampersands & here?
....(2 * quadCoA) & "+" & (quad4ac * -1)...
Problem is that & is a string concatenation in VB.NET, which of course cannot be assigned to a Long.
What do you expect console to print at this line?
Console.WriteLine(quadrComplex)
The answer for you will be somewhere between these two questions.

Related

Why is (L \ 12) calculating to 0?

I am trying to do calculations through VBA.
I am doing it through the form itself because when Production creates one of these Job Tickets a number of fields can change on the fly. Which Unit of Measure we are running in, which type of Wood, how we Wrap it, etc.
All of these changes affect the total footage or amount of pieces we have to run, which is why I have If-Then-Else statements for them.
It works until I get to Wrap SQ Footage. I get a zero inserted into my field, but when I do the calculations on my own I never get 0.
I created this expression in the control source of one of the Wrap SQ Footages, and it comes out correctly.
=Abs(Int( (([Wrap_Slit1]/12) * [Quantity_Ordered] ) * ( [RIP_Scrap_Rate] + 1))))
Private Sub FTG_Calculations()
'Declare Variable
Dim L As Double
Dim Length As Double
Dim OrderFTG As Double
Dim UoM As String
Dim W As Double
Dim frm As Access.Form
Set frm = Forms!Frm_JobTicket
'Set L equal to Length from Tbl_JobTicketMould
L = DLookup("Length", "Tbl_JobTicketMould", "Access_ID =" & Forms!Frm_JobTicket!Part_Number)
'Convert Length to Feet
Length = (L \ 12)
'Find Unit of Measure for this part
UoM = DLookup("Unit_of_Measure", "Tbl_JobTicketUoM", "Access_ID =" & Forms!Frm_JobTicket!Part_Number)
'Mupltiply Length times Quantity to get Order Footage
OrderFTG = Int((Length * Me.Txt_Pcs_JobTicket))
'If UoM is PCS then insert that number. Otherwise set equal to Quantity Ordered divided by Length of piece(in FT)
If UoM = "PCS" Then Me.Txt_Pcs_JobTicket = Me.Quantity_Ordered Else: Me.Txt_Pcs_JobTicket = Abs(Int(Me.Quantity_Ordered \ Length))
'Define limits of the loop. Then runs through all Wrap SQ FTG fields and inputs calculation
For W = 1 To 3
'If UoM is PCS then calculate Order Footage to find Wrap Sqaure Footage. Otherwise take slit size in FT and multiply by Order Quantity and Scrap Rate
If UoM = "PCS" Then
frm("Txt_Wrap" & W & "SQFTG_JobTicket") = (((frm("Wrap_Slit" & W) \ 12) * OrderFTG) * (Round((frm("RIP_Scrap_Rate")), 3) + 1))
Else: frm("Txt_Wrap" & W & "SQFTG_JobTicket") = (((frm("Wrap_Slit" & W) \ 12) * frm(Quantity_Ordered)) * (frm(RIP_Scrap_Rate + 1)))
End If
Next W
I figured out the issue is in the (frm("Wrap_Slit" & W) \ 12) area. Wrap_Slit1 shows a value of 2 in the data tips, but when I divide by 12 it comes out to 0.
All of my data points are set to double, and the variables are declared as double. It is rounding down when it should come out to .16667.
Place the following code before the:
If UoM = "PCS" Then
Msgbox code:
MsgBox("Current State:" & vbCrLf & _
"UoM:" & vbTab & UoM & vbCrlf & _
"OrderFTGL" & vbTab & OrderFTG & _
"Wrap_Slit1:" & vbTab & Me.Wrap_Slit1 & _
... continue pattern for other desired values in calculation...
"Continue...", vbOK)

Compile Error At IfError in VBA Code of Nested WorksheetFunctions

So, I'm quite new to VBA and I'm trying to learn. I have several tables with fastener data that I'm trying to pull information from based on what's in the main input table. I have a formula that works, but as I add different fastener tables, the nested IF formula is getting unruly. I decided to try to convert the formula to VBA and I'm getting an error: "Compile error: Wrong number of arguments or invalid property assignment." It shows up at the IfError. I may also have called the table columns incorrectly. The formula that I am trying to convert is included below. I haven't yet determined how best to set up the If statement so that if the user selected IFF, it would switch to using the IFF table, but that's another post.
Dim tbl_Solid As ListObject
Dim Rep_Fast_Type
Dim Incoming_Dia
Dim BP_Max_Dia
Dim Test_Dia
Dim Rep_Fast
Dim Test_Value
Dim i
Set tbl_Solid = ThisWorkbook.Sheets("Fastener DB").ListObjects("tbl_Solid")
i = 1
Incoming_Dia = [tbl_Input].Cells(i, 2)
BP_Max_Dia = [tbl_Input].Cells(i, 3)
Rep_Fast_Type = [tbl_Input].Cells(i, 4)
If Incoming_Dia > BP_Max_Dia Then
Test_Dia = Incoming_Dia
Else
Test_Dia = BP_Max_Dia
End If
'Compile error at IfError in third line.
If Rep_Fast_Type = "Solid" Then
Rep_Fast = WorksheetFunction.Index([tbl_Solid].Range("Fastener"), _
WorksheetFunction.Aggregate(15, 6, WorksheetFunction.IfError( _
Rows([tbl_Solid].Range("Fastener")) / ((Test_Dia >= [tbl_Solid].Range("min")) * (Test_Dia <= [tbl_Solid].Range("max"))), _
Rows([tbl_Solid].Range("Fastener")) / (Test_Dia <= [tbl_Solid].Range("min")), 1)))
End If
'To test values
MsgBox "Incoming diameter is " & Incoming_Dia & vbCrLf & "B/P max diameter is " & BP_Max_Dia & vbCrLf & "Test diameter is " & Test_Dia & vbCrLf & "Repair fastener type is " & Rep_Fast_Type & vbCrLf & "Repair Fastener is " & Rep_Fast
Old Excel Formula:
=IF([#[rep type]]="Solid",INDEX(tbl_Solid[[#All],[Fastener]],AGGREGATE(15,6,IFERROR(ROW(tbl_Solid[Fastener])/(([#diameter]>=tbl_Solid[min])*([#diameter]<=tbl_Solid[max])),ROW(tbl_Solid[Fastener])/([#diameter]<=tbl_Solid[min])),1)),IF([#[rep type]]="IFF",INDEX(tbl_IFF[[#All],[Fastener]],AGGREGATE(15,6,IFERROR(ROW(tbl_IFF[Fastener])/(([#diameter]>=tbl_IFF[min])*([#diameter]<=tbl_IFF[max])),ROW(tbl_IFF[Fastener])/([#diameter]<=tbl_IFF[min])),1))))
Screenshot of Data
It was just a parenthesis wrongly placed:
replace this line:
Rows([tbl_Solid].Range("Fastener")) / (Test_Dia <= [tbl_Solid].Range("min")), 1)))
with for this:
Rows([tbl_Solid].Range("Fastener")) / (Test_Dia <= [tbl_Solid].Range("min"))), 1))
Nevertheless, I would suggest to use the With statement to wrap the formula, i.e.:
With WorksheetFunction
Rep_Fast = .Index([tbl_Solid].Range("Fastener"), _
.Aggregate(15, 6, .IfError( _
Rows([tbl_Solid].Range("Fastener")) / _
((Test_Dia >= [tbl_Solid].Range("min")) * (Test_Dia <= [tbl_Solid].Range("max"))), _
Rows([tbl_Solid].Range("Fastener")) / (Test_Dia <= [tbl_Solid].Range("min"))), 1))
End With
Note that this answer refers only to the syntax that gives the compile error, no test have been performed to the formula output.

VBS script return expected result, how to compare float value?

I am getting an interesting result when executing the following VB script.
Set StdOut = WScript.StdOut
Set wbemSvc = GetObject("winmgmts://" & "." & "/root/cimv2")
Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")
For Each biosObj In biosSet
StdOut.WriteLine "SMBIOSMajorVersion=" & biosObj.SMBIOSMajorVersion
StdOut.WriteLine "SMBIOSMinorVersion=" & biosObj.SMBIOSMinorVersion
Next
StdOut.WriteLine "Return value is: " & IsNewBiosVersion
Function IsNewBiosVersion()
On Error Resume Next
Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")
newBios = 0
For Each bios In biosSet
minorFloat = "." & bios.SMBIOSMinorVersion
If bios.SMBIOSMajorVersion > 2 OR (bios.SMBIOSMajorVersion = 2 AND minorFloat >= .6) Then
newBios = 1
End If
Next
IsNewBiosVersion = newBios
End Function
The result is as follow. This looks contradictory since the SMBIOSMinorVersion=4, according to the code logic in the script, the return value should be 0!!!
SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
Return value is: 1
I ran this same script on another system and got the expected correct result.
SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
Return value is: 0
So what is the problem here?
New update:
We execute the following script again on the system, and found that the CDbl() function does not convert the string "2.4" to double value correctly, instead it converts it to 24! Looks like the dot "." was lost when converting, what is wrong with this? An bug in CDbl or a violation when use it?
here is the script
Set StdOut = WScript.StdOut
StdOut.WriteLine ""
StdOut.WriteLine "Simple Function to Test BIOS Version"
StdOut.WriteLine ""
Set wbemSvc = GetObject("winmgmts://" & "." & "/root/cimv2")
Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")
For Each bios In biosSet
newBios = 0
StdOut.WriteLine "SMBIOSMajorVersion=" & bios.SMBIOSMajorVersion
StdOut.WriteLine "SMBIOSMinorVersion=" & bios.SMBIOSMinorVersion
temp = bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion
StdOut.WriteLine "major dot minor=" & temp
currentBios = CDbl(bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion)
StdOut.WriteLine ""
StdOut.WriteLine "currentBios=" & currentBios
If currentBios >= 2.6 Then newBios = 1
StdOut.WriteLine "return value is: " & newBios
Next
here is the output
Simple Function to Test BIOS Version
SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
major dot minor=2.4
currentBios=24
return value is: 1
Remove your error handling - it s probably suppressing an issue.
On Error Resume Next ' Not a good idea
For one thing you are comparing a string to a double in these lines:
minorFloat = "." & bios.SMBIOSMinorVersion
If bios.SMBIOSMajorVersion > 2 OR (bios.SMBIOSMajorVersion = 2 AND minorFloat >= .6) Then
Why not just convert the major.minor values to floating-point for your test? You're currently doing two separate tests, with one being a string vs float comparison, which is unusual.
Maybe try this instead?
currentBios = CDbl(bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion)
If currentBios >= 2.6 Then newBios = 1
You have to be careful comparing floating-point values when math operations are involved but for literal values you'll be fine.
And, as already mentioned, remove On Error Resume Next or you may never know why it works on one PC but not another.

Double For Loop in VB.NET

Dim ssi(11) As String
For i = 0 To 10
If ssi(i) = "" Then ssi(i) = "0"
For j = 0 To Val(ssi(11)) + i
ssi(i) = xuh(Val(ssi(i)))
Next
Next
If ssi(11) = "2" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10)))
ElseIf ssi(11) = "3" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10))) * (-1)
End If
I am new here and new to VB as well.
I am trying to understand this double loop in vb code.
ssi(i) is defined as a String variable. and each element is assigned to a specific number in a String. Hope I told it clearly.
My problem with this loop is below.
Since i ranges from 0 to 10, what does this j mean? Does j mean the new ssi(1-10) or another whatever number?
I think the best way to answer your question about understanding a double loop is to try looking at something simpler.
The first program I always write in each new version of BASIC that comes along is a 12 times table.
I've modified it a bit below to be a 12 x 10 table for the purpose of illustrating for you how a double loop works ... hope it helps:
For x As Integer = 1 To 12
For y As Integer = 1 To 10
Console.Write(x * y)
Console.Write(vbTab)
Next
Console.WriteLine()
Next

VBA Runtime error 1004 on Cells(..).Formula

I can't make this simple command work:
Cells(l, 7).Formula = "=" & var1 & " * " & var2 & " * " & var3 & " / 252"
I can paste the inspected formula string value in Excel and it works as expected.
Cells(l,7) is a proper reference, as I can inspect its value.
Inspected formula on debug:
"=86710597,9409 * 0,02 * 0,35 / 252"
The problem is when a variable is represented as string it will have the system's decimal point. In your case it is a comma so you need to convert it to dot, for example using replace() function.