# How to calculate complex roots?

**URL:** <https://forum.kirupa.com/t/how-to-calculate-complex-roots/659455>\
**Category:** Uncategorized\
**Created:** [June 19, 2023, 3:16pm UTC](https://forum.kirupa.com/t/how-to-calculate-complex-roots/659455 "2023-06-19T15:16:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 19, 2023, 3:16pm UTC](https://forum.kirupa.com/t/how-to-calculate-complex-roots/659455/1 "2023-06-19T15:16:18Z")

</div>

```auto
let a = Number(prompt("Enter the value of a"));
let b = Number(prompt("Enter the value of b"));
let c = Number(prompt("Enter the value of c"));
let x, y;
if (b * b - 4 * a * c == 0) {
  x = -b / (2 * a);
  console.log(x);
}
else if (b * b - 4 * a * c > 0) {
  x = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
  y = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
  console.log(x);
  console.log(y);
}
else {
  x = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
  y = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
  console.log(x);
  console.log(y);
}

```

**I am getting NaN.** I can’t eat it as I don’t have the chutney rn lol. jk.  
I know this is wrong, but can you give me a no-code answer to solve this problem? I want to write code myself, I want hints. I don’t want to look into google or chatgpt as they’ll solve this problem 1000 times already in google it’s available.

---

<div class="post-metadata">

**Author:** ![PAEz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/paez/32/19607_2.png) [@PAEz](https://forum.kirupa.com/u/PAEz)\
**Post date:** [June 19, 2023, 3:41pm UTC](https://forum.kirupa.com/t/how-to-calculate-complex-roots/659455/2 "2023-06-19T15:41:04Z")

</div>

Clues…  
What does `b * b - 4 * a * c` give you?  
Now read this…

> **[Math.sqrt() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt)**
>
> The Math.sqrt() static method returns the square root of a number. That is

EDIT: It took me long enough but I just got your joke, must be a cultural thing 😜

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 19, 2023, 3:59pm UTC](https://forum.kirupa.com/t/how-to-calculate-complex-roots/659455/3 "2023-06-19T15:59:55Z")

</div>

I understand that Math.sqrt(-value) returns NaN. I don’t understand how we can calculate the complex roots.

---

<div class="post-metadata">

**Author:** ![PAEz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/paez/32/19607_2.png) [@PAEz](https://forum.kirupa.com/u/PAEz)\
**Post date:** [June 19, 2023, 4:16pm UTC](https://forum.kirupa.com/t/how-to-calculate-complex-roots/659455/4 "2023-06-19T16:16:29Z")

</div>

Ah, ok, complex roots is a thing 😜  
Im not the one for this, me dumb, i didnt even finish year 10.  
If it was me Id just use Math.js or is it complex.js

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 19, 2023, 9:55pm UTC](https://forum.kirupa.com/t/how-to-calculate-complex-roots/659455/5 "2023-06-19T21:55:20Z")

</div>

```auto
let a = Number(prompt("Enter the value of a"));
let b = Number(prompt("Enter the value of b"));
let c = Number(prompt("Enter the value of c"));
let realPart, imaginaryPart;
let x, y;
if (b * b - 4 * a * c == 0) {
  x = -b / (2 * a);
  console.log(x);
}
else if (b * b - 4 * a * c > 0) {
  x = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
  y = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
  console.log(x);
  console.log(y);
}
else {
  realPart = -b / (2 * a);
  imaginaryPart = Math.sqrt(4 * a * c - b * b) / (2 * a);
  console.log(
    "Complex roots:",
    realPart + " + " + imaginaryPart + "i",
    realPart + " - " + imaginaryPart + "i"
  );
}

```

Problem is solved.
