LyanR
LyanR

Reputation: 31

Quadratic Complex roots solver

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.

Upvotes: 0

Views: 106

Answers (2)

Victor Zakharov
Victor Zakharov

Reputation: 26454

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.

Upvotes: 0

Jens
Jens

Reputation: 6385

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.

Upvotes: 2

Related Questions