# Quadatic equation solver with complex coefficients

**URL:** <https://fortran-lang.discourse.group/t/quadatic-equation-solver-with-complex-coefficients/82>\
**Category:** Help\
**Created:** [May 30, 2020, 4:04pm UTC](https://fortran-lang.discourse.group/t/quadatic-equation-solver-with-complex-coefficients/82 "2020-05-30T16:04:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ELNS](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/elns/32/3338_2.png) [@ELNS](https://fortran-lang.discourse.group/u/ELNS)\
**Post date:** [May 30, 2020, 4:04pm UTC](https://fortran-lang.discourse.group/t/quadatic-equation-solver-with-complex-coefficients/82/1 "2020-05-30T16:04:52Z")

</div>

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

 ![QESCMPLX](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/844c05119f0112880e5a4fbdf45414b11a161cd5.png)  
Did i implement it correctly?

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [May 30, 2020, 4:13pm UTC](https://fortran-lang.discourse.group/t/quadatic-equation-solver-with-complex-coefficients/82/2 "2020-05-30T16:13:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ELNS](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/elns/32/3338_2.png) [@ELNS](https://fortran-lang.discourse.group/u/ELNS)\
**Post date:** [May 30, 2020, 5:49pm UTC](https://fortran-lang.discourse.group/t/quadatic-equation-solver-with-complex-coefficients/82/3 "2020-05-30T17:49:11Z")

</div>

I code mostly in python and got used to indentation 🙂  
Thanks for your valuable tips 🙏

---

<div class="post-metadata">

**Author:** ![parekhvs](https://avatars.discourse-cdn.com/v4/letter/p/9dc877/32.png) [@parekhvs](https://fortran-lang.discourse.group/u/parekhvs)\
**Post date:** [May 30, 2020, 5:55pm UTC](https://fortran-lang.discourse.group/t/quadatic-equation-solver-with-complex-coefficients/82/4 "2020-05-30T17:55:52Z")

</div>

@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](https://rosettacode.org/wiki/Roots_of_a_quadratic_function#Fortran_90)

---

<div class="post-metadata">

**Author:** ![nshaffer](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nshaffer/32/161_2.png) [@nshaffer](https://fortran-lang.discourse.group/u/nshaffer)\
**Post date:** [June 11, 2020, 4:26pm UTC](https://fortran-lang.discourse.group/t/quadatic-equation-solver-with-complex-coefficients/82/5 "2020-06-11T16:26:16Z")

</div>

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.
