-4

I'm having a problem with the output of my division. When I enter both real parts of 5 and 3 and imaginary parts of 4 and 2, I get 2.000000 + -2.000000i instead of 1.769231 + 0.153846i.

Here's the case for dividing in the main program:

case 4:
     num1=Read_Complex();
     num2=Read_Complex();
     if ((num2.real=0)&&(num2.img=0));
     printf("Error. Cannot divide by 0.\n");
     }
     else{
        ans=Divide_Complex(num1,num2);
     }

Here's where Read_Complex is called:

complex Read_Complex(void)
{
complex num;

printf("Enter real part:");
scanf("%d", &num.real);
printf("Enter img part:");
scanf("%d", &num.img);

return num;
}

And here's the division:

complex numer, denom;
float ans_real ans_img;

complex Divide_Complex(complex num1, complex num2)
{
   numer=Multiply_Complex(num1, Conjugate_Complex(num2));
   denom=Multiply_Complex(num2, Conjugate_Complex(num2));

   ans_real=numer.real/denom.real;
   ans_img=numer.img/denom.img;

   printf("%f + %fi\n", ans_real, ans_img);
}

Any help would be great.

Complex.h:

#ifndef COMPLEX_H_
#define COMPLEX_H_

typedef struct {
    int real;
    int img;
} complex;

#endif
JadC
  • 19
  • 5
  • 2
    Undefined behavior for incorrect `scanf()` conversion specifier? – EOF Mar 02 '17 at 19:32
  • 1
    Integer format specifiers for complex numbers is useless. It should be either `scanf("%f", &num.real);` or `scanf("%lf", &num.real);` depending on whether the definition uses `float` or `double`. Ditto with `num.img`. – Weather Vane Mar 02 '17 at 19:34
  • I do not think that's the problem though. %d works with the multiplication, conjugate, negate, add and subtract parts. Just not division. The other outputs can't be floats or doubles, they must be integers. Division's output has to be floats. – JadC Mar 02 '17 at 19:41
  • Please show the definition of `complex`. It is unlikely to have `int` type members. If the code "sometimes works" that is just lucky. – Weather Vane Mar 02 '17 at 19:43
  • Does not your compiler warning about `scanf("%d", &num.real);`? – chux - Reinstate Monica Mar 02 '17 at 19:44
  • I included complex.h. No, it doesn't give me any warnings. – JadC Mar 02 '17 at 19:46
  • 1
    Hah! In that case the division fails because it is doing *integer division*. Please try `ans_real=(float)numer.real/denom.real;` etc. – Weather Vane Mar 02 '17 at 19:47
  • Just tried it. I got 2.0000 and -2.50000i. Any other ideas? – JadC Mar 02 '17 at 19:51
  • Note that the `complex.h` you have posted is nonconforming and probably not the correct header for your platform. – EOF Mar 02 '17 at 19:56
  • Hmm. Well it's what I have to use for class so I don't think I can really change that. – JadC Mar 02 '17 at 19:59
  • 2
    @JadC your complex type **cannot hold real values**. The intermediate calculations to `float` (assuming you made the casts for the division) will be truncated when you assign them to the `int` members of the complex struct. Forget about `int` members, go back to the drawing board, using `double` struct members. Never use `float` in the 21st century when you can use `double`. – Weather Vane Mar 02 '17 at 20:07
  • So there's not really a way to get a float value out of this? :( I would use double if I could. Unfortunately, for this particular project, I can't.. – JadC Mar 02 '17 at 20:10
  • 2
    Is the provided code realistic ? What is the use of `if ((num2.real=0)&&(num2.img=0));` ? Where both `num2.real` and `num2.img` are assigned to zero (`=0`) instead of compared to zero (`==0`) ? – J. Piquard Mar 02 '17 at 20:10
  • The use of if ((num2.real=0)&&(num2.img=0)); is supposed to prevent division of zero. – JadC Mar 02 '17 at 20:12
  • @ J. Piquard You actually solved my problem. Should've been ==. – JadC Mar 02 '17 at 20:15

1 Answers1

3

You have several problems, many of them covered in the comments on your question, but at least one important one not. You seem to have already recognized the problem in your check for division by zero. Beyond that,

In the first place, your function Divide_complex() is declared to return a value, but control reaches the end of the function without a return statement being executed. If the caller attempts to use the return value then undefined behavior results; your compiler should be emitting a warning about this.

In the second place, your complex data type is wholly inadequate for representing complex numbers, because it affords only integer values for the real and imaginary components. In particular, it cannot represent anything close the quotient you have specifically asked about.

In the third place, Divide_Complex() performs integer division when it divides components of your complex numbers, necessarily affording integer results. That's OK in the sense that your complex data type can hold only integer components anyway, but it cannot yield results close to what you are expecting.

But fourth, you've coded the math wrong. You compute the revised denominator as the product of the original denominator and its conjugate, which is fine, but the result is a real number, with no imaginary component. That's the whole point -- it turns the complex / complex quotient into a complex / real quotient. You should be computing the imaginary part of the quotient as ans_img = (float) numer.img / denom.real (dividing by denom.real, not denom.img). The way you do it now ought to cause an integer division by zero, since denom.img will be zero if everything else is working correctly.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • 1
    Your last para, earlier I constructed the missing functions in the question (casting the division as `(float)`) and the OP's input values of `5 + 4i` and `3 + 2i` gave me `1.769231 + infi`. The real part matches OP's req, but not the imaginary part. After I made your correction, the answer was the expected `1.769231 + 0.153846i` – Weather Vane Mar 02 '17 at 20:59