title: How to overcome the NaN and continue the calculation in Javascript - Negative value for radicand of sqrt
body: <p>I have set up a js / three.js program for <a href="https://www.matheretter.de/rechner/zylinder/" rel="nofollow noreferrer">calculating cylinders</a> by only two given values.</p>
  
  <p>The only calculation that is quite difficult happens when <strong>volume</strong> and <strong>surface</strong> are given. From both values I need to calculate the radius or the height. </p>
  
  <p>To recall the formulas: </p>
  
  <p>Volume V = π·r²·h</p>
  
  <p>Surface A = 2·π·r·(r+h) </p>
  
  <p>If you do the math, you will get the cubic formula: <code>0 = r^3 + A/(-2*pi)*r + V/pi</code></p>
  
  <p>which I honestly could not solve, so I used <a href="http://www.wolframalpha.com/input/?i=solve%200%20%3D%20r%5E3%20-%20A%2F%282*pi%29*r%20%2B%20V%2Fpi%20for%20r" rel="nofollow noreferrer">wolframalpha</a> that gives this result for the radius r: </p>
  
  <p><img src="https://i.stack.imgur.com/NNODL.png" alt="formula cylinder for r by V and A"></p>
  
  <p>Note: There are three formulas for r, this is the first of them. See <a href="http://www.wolframalpha.com/input/?i=solve%200%20%3D%20r%5E3%20-%20A%2F%282*pi%29*r%20%2B%20V%2Fpi%20for%20r" rel="nofollow noreferrer">wolframalpha</a>.</p>
  
  <p>By trying to implement this equation in Javascript I realized that the radicand of <code>√(54πV^2 - A^3)</code> is negative and Javascript is returning <code>NaN</code>.</p>
  
  <p>This leads to my question: How can I overcome the <code>NaN</code> and continue the calculation - should I use complex numbers, how? What workarounds have you used? Can I just multiply the radicand by <code>*(-1)</code>, remember this value and <em>consider</em> it later on?</p>
  
  <p>I am a bit lost here, this is the first time I have to defeat NaN :-)</p>
  
  <p>Thanks in advance for all your tips, advices, solutions and code.</p>
  
  <p><br />Edit (reaching the goal): Is someone living on this earth feasable of solving the <a href="http://www.wolframalpha.com/input/?i=solve%200%20%3D%20r%5E3%20-%20A%2F%282*pi%29*r%20%2B%20V%2Fpi%20for%20r" rel="nofollow noreferrer">three equations</a> in Javascript and can post his code? I have generally googled <a href="https://www.google.com/#q=calculate+cylinder+by+surface+and+volume" rel="nofollow noreferrer">"calculate cylinder by surface and volume"</a> and it seems nobody has done it before...</p>