1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
2
Rep Power
0

#### Help for very beginner - simple quadratic equation solver

Hello, I am just getting started with programming and figured the best way to learn was to get my hands dirty instead of memorizing tons of syntax. I made a very simple Quadratic Equation solver and I'm getting a strange answer at the end.

Here is my code:

Code:
```print ("Today we will be solving your quadratic equations")

# Ask the user for values A,B,and C.
a = input ("What is your A value?")
b = input ("What is your B value?")
c = input ("What is your C value?")

#Now convert the values to the "int" type.
a = int(a)
b = int(b)
c = int(c)

#Now we need to find the descriminant.
dis = (b**2) - (4*a*c)
dis = dis**0.5

#Next, we need to evaluate the answers.
ans1 = (-1*b) + dis
ans1 = ans1 / (2*a)

ans2 = (-1*b) - dis
ans2 = ans2 / (2*a)

print(ans1, ans2)
input ("Press<Enter>")```
Everything works fine until I get the answer. Here is it running:

(-0.49999999999999994+0.8660254037844386j) (-0.5-0.8660254037844386j)
Press<Enter>

I assume that I could float the decimals, but what is confusing me is why is not adding/subtracting the numbers. Also the j after the discriminant. Could someone please explain why the j is there, and how to get rid of it? Also why it isn't adding/subtracting the answers.

Thanks, Rob.
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
/dev/null
Posts
168
Rep Power
22
Originally Posted by rhouser
I assume that I could float the decimals, but what is confusing me is why is not adding/subtracting the numbers. Also the j after the discriminant. Could someone please explain why the j is there, and how to get rid of it? Also why it isn't adding/subtracting the answers.
Before we jump into the nuances of your python code, first lets understand the quadratic equation at hand.

You're assigning A, B and C as 2. Thus your equation would be
Code:
`2x^2 + 2x + 2 = 0`
assuming you're solving for x.

Now if you do solve this equation on paper, you will find out that the roots of this equation are complex numbers.

Code:
`-0.5 + (sqrt(-12) / 4) and -0.5 - (sqrt(-12) / 4)`
I'm sure you would have had training on complex numbers before quadratic equations. So, the roots are of the form (a + bi), where "bi" represents the imaginary part of the complex number. In python, it's (a + bj) (j instead of i).

There is nothing wrong with your program.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
2
Rep Power
0
Thank you so much! I feel really dumb now xD I should have noticed that before since I'm in college level calculus, but I was focusing on my coding and thinking I wrote it wrong. But thank you again very much!!
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
Usually Japan when not on contract
Posts
240
Rep Power
15
Originally Posted by rhouser
I should have noticed that before since I'm in college level calculus
After you finish your studies and become a working engineer or mathemetician you will be forever scratching your head over never-before-seen notations that say exactly what you hoped they did, but couldn't read.