what kind of variable would you use to represent these?
Printable View
what kind of variable would you use to represent these?
an imaginary one?
is there a type of variable you could use to represent them? i dont really know a lot about imaginary numbers and i need to use them in a program i have to write
not that i can remember, but what is stopping you from creating your own class to represent imaginary numbers?
ok you got me.......i feel like an idiot but what exactly is an imaginary number?
std::complex maybe?
For imaginary numbers only i suppose you could set the real part to zero. Ive only used i as part of a complex number never alone.
I think the proper way to think about it is that an imaginary number is a complex number that isn't a real number, so it is always a complex number, regardless if the real part of it is zero or not.
...edit...
maybe not, the FAQ above explains:
"Imaginary numbers are numbers that can be written as a real number times i."
"Complex numbers are numbers like 7 + .4i; they're a real number plus an imaginary number."
ok i get what imaginary numbers are now. i have to write a program that solves the quadratic formula and i need to include imaginary numbers in the program......what do these numbers have to do with solving the quadratic formula?
Think of this. The square root of a negative number has to be imaginary. So
y=4a+3b+4 would be written.
Use what you learned in those tutorials to figure out how you would evaluate the problem above.Code:(-3(+/-)(sqrt((squared(3))-(4*4*4))))/(4*2)
assume #define squared(x) x*x
The quadratic formula will tell you the two roots of the equation. For example, for the quadratic y = x^2 - 1, the graph would intersect the x-axis at -1 and 1. These are the roots. If the graph were y = x^2 + 1 on the other hand, there would be no real roots because the graph of the quadratic doesn't intersect the x-axis. In this case you would have two imaginary roots, -i and i. We get this from the quadratic formula:
-b +/- sqrt( b^2 -4(a)(c)) / 2a
=
+/- sqrt(-4) / 2 = +/- 2i / 2 = +/- i
(remember i is the sqrt(-1))
I'm a beginner at programming myself, so I don't know which header it is that has support for complex numbers, but this is why you would need them.
Extol: remember that in your example, 4, 3, and 4 are equal to a, b, and c. I believe you want x's where you have your a and b
so to declare an imaginary number......i could just do this right?
double i = sqrt(-1);
Oh gosh. Yea, isn't it 4x^2+3x+4 even. I don't remember.
alright i think i actually need to use complex numbers......is therea way to declare these?
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.
First of all, I'd like to say that the solution proposed by Blackrat is IMO the best for your problem. Secondly, there is a complex number class in the standard header files. I believe it's complex.h, but I may be wrong. If you can't find that library or have trouble using it, write your own class for complex numbers. I did that for a class once. Not difficult at all if you know how to work with complex numbers. The only data members you need are double a, b;
Hmmm, I never know how to end these posts. Umm, good luck! :)
Yeah, include complex and declare a std::complex. A lot of what you will need has already been implemented for you. :)Quote:
Originally posted by Gil22
alright i think i actually need to use complex numbers......is therea way to declare these?
an easier way to find ou if a number is imaginary is to find out if the equation under the radical is negative. If so then output an i after the output of the answer. If not then just evaluate it regullarly. A little bit of the code would look like this:
There you go. Hope it helps you out.Code:
float determinant;
float root;
determinant = b*b - (4*a*c);
if(determinant < 0)
{
determinant = determinant * -1;
root1 = (b + Sqrt(determinant) )/ 2;
root 2 = (b - Sqrt(determinant) )/ 2;
cout << "The answers are: " << root1 << "i, " << root2 << "i." << endl;
}
else
{
root1 = (b + Sqrt(determinant) )/ 2;
root 2 = (b - Sqrt(determinant) )/ 2;
cout << "The answers are: " << root1 << ", " << root2 << endl;
}
-Bill