Quadatic equation solver with complex coefficients

Hi all
I am going to write a fortran program to obtain the solution of the quadratic equation where all the quantities are complex.


Did i implement it correctly?

3 Likes

I think so. It uses single precision. Instead of 4.0 * a * c you can just write 4 * a * c the same with 2.0 * a -> 2 * a. You probably don’t need the cmplx there, as everything is already complex.

I personally do not indent the body of the main program, but I know lots of people here do, so that’s fine.

I don’t have any other suggestions.

1 Like

I code mostly in python and got used to indentation :slight_smile:
Thanks for your valuable tips :pray:

2 Likes

@ELNS,

You may know a significant amount of code snippets for several of the common tasks such as solutions to quadratic, cubic, transcendental equations, etc. are available at many sites online, especially math and computational departments at universities that have a strong focus on engineering and physics-related disciplines. Also of interest, particularly for the case of your immediate interest, is the Rossetta Code site which offers a worked solution that looks comprehensive: https://rosettacode.org/wiki/Roots_of_a_quadratic_function#Fortran_90

2 Likes

One thing I see which is not “incorrect” but is probably not a good idea is the call to csqrt for the complex square root. You can replace this with the ordinary sqrt and it will do the right thing when given a complex argument.

Many math functions have variants like sqrt, dsqrt, csqrt. These were necessary back in the Fortran 77 days, but they are not necessary any more. On top of that, if you change your program to use higher precision complex numbers, then csqrt will either (a) cause a compiler error or (b) cause a loss of precision in the square root calculation. Whether you get (a) or (b) depends on your compiler and what compilation flags you have set.

It is best to avoid these d- and c-prefixed functions altogether.

2 Likes