You don't need to actually use complex numbers, you just need to know how they work and be clever with some radical simplification.
i is the square root of -1. The part of the quadratic equation that could make complex numbers necessary is (sqr(b^2-4ac)). If (b^2-4ac) < 0 then you're going to need some complex numbers. For the example I'm about to give, let a=8, b=0, and c=2.Quote:
The Quadratic Formula. The quadratic equation ax^2+bx+c has the solutions
x=(-b +- sqr (b^2-4ac))/2a
b^2-4ac
0^2-4(8)(2)
-64
At this point, we're trying to find the square root of -64.
sqr(-64)==sqr(-1*64)==sqr(-1)*sqr(64)==8*sqr(-1)=8i
In your code, if you just take the absolute value of b^2-4ac, take the square root of that, and slap an i after it (if b^2-4ac evaluates to <0) you'll be fine. Then, just stick that number into the quadratic formula...to finish it off, we have this:
x=(-b +- sqr (b^2-4ac))/2a
x=(-0+-8i)/(2*8)
x=(+-8i)/16
x=8i/16 or -8i/16
x=.5i or -.5i
See, never needed to program anything with complex numbers.