0

I keep receiving this error message when compiling a simplified mandelbrot function.

main.c:86:43: error: invalid operands to binary > (have ‘complex double’ and ‘complex float’) else if(f2r > 1000 || f2i > 1000 * I) {

........^

There are other messages with this, but they are all the same problem.

I pretty much just started coding the other day and I'm just not familiar enough with the complex.h package to sort it out myself... and on top of that I can't find this answer anywhere online as far as I've looked - so I decided to ask.

Thanks in advance.

  • 2
    Complex numbers are unordered. You cannot compare them. There is no such operation in math, hence there is no such operation in C as well. – yeputons Jan 29 '21 at 18:27
  • 2
    Please provide a minimal, reproducible example, see https://stackoverflow.com/help/minimal-reproducible-example. – August Karlstrom Jan 29 '21 at 18:29
  • 2
    What you need to compare, is the *magnitude* of the value. Related: [How to work with complex numbers in C?](https://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c) – Weather Vane Jan 29 '21 at 18:36

2 Answers2

0

It looks like you are attempting to compare the real part of a complex number to a real number, and the imaginary part of a complex number to an imaginary number. The comparison operator, it appears, does not support comparison of complex numbers. However, you can just piece out the imaginary part of the complex values and compare those.

The double cimag(double complex d) function will give return the imaginary part of a complex value so that you can run your comparison, so try something like this:

else if(f2r > 1000 || cimag(f2i) > 1000) {
Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
  • That solution worked to remove the error messages, however the output is not showing up - just a black screen and blinker. The function is based on a 100 iteration limit (and whether the real or imaginary components reach an upper bound in that time). Would a possibly 100 iteration function take a long time if it ends up reaching that high? That might be why I'm not getting output. The compiling process is still running - the "Stop" button is usable. I think your solution was good, but now I'm at the next stage of problem solving. Thanks a lot! – Johnny Clifton Knight Jan 29 '21 at 20:28
  • 100 iterations are not very many. CPUs operate on the order of billions of operations per second. – Christian Gibbons Feb 03 '21 at 21:34
0

One of the ways to compare complex values is as its modulus. So you can use the following formulae:

modulus = sqrt(r * r + i * i)

Then you compare both values.

  • 2
    There is a function for the modulus, `cabs` – M.M Jan 29 '21 at 20:59
  • 1
    `sqrt(r * r + i * i)` is prone to overflow in the intermediate calculations even when the result is finite. Perhaps `cabs(x + iy)` or `hypot(x, y)`? Either of those might overflow too, but a quality implementation will not overflow so readily. – chux - Reinstate Monica Jan 29 '21 at 21:02