Calculating cube root: OverflowError: ('Result too large')
<p>So I'm supposed to create a code that calculates the cube root of an inputted number with the approximation of up to 2 decimal places. This code above calculates the square root of a number with up to 2 decimal places:</p>

<pre><code>epsilon = 0.01
guess = num/2.0
while abs(guess**2 - num) &gt;= epsilon:
    guess = guess - abs(guess**2 - num) / (2 * guess)
print("Guess:", guess)
</code></pre>

<p>So apparently I am able to do the cube root with that criteria by modifying this code that was given and using this in the code:</p>

<pre><code>delta = abs(guess**3 - num) / 100.0
</code></pre>

<p>I tried using that line and modifying the code used for square root and I keep getting: </p>

<pre><code>OverflowError: ('Result too large') 
</code></pre>

<p>This is what my code looks like so far:</p>

<pre><code>num = float(input("Enter a number: "))
epsilon = 0.01
guess = num/2.0
while abs(guess**3 - num) &gt;= epsilon:
    guess = abs(guess - (guess**3 - num)/100.0)
print("Guess:", guess)
</code></pre>

<p>When I run that code above this is what happens:</p>

<blockquote>
  <p>runfile('C:/Users/100617828/Documents/CSCI1040U/edits.py', runfile('C:/Users/100617828/Documents/CSCI1040U/edits.py', wdir='C:/Users/100617828/Documents/CSCI1040U')</p>
  
  <p>Enter a number: 34 Traceback (most recent call last):</p>
  
  <p>File "", line 1, in runfile('C:/Users/100617828/Documents/CSCI1040U/edits.py', wdir='C:/Users/100617828/Documents/CSCI1040U')</p>
  
  <p>File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile execfile(filename, namespace)</p>
  
  <p>File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)</p>
  
  <p>File "C:/Users/100617828/Documents/CSCI1040U/edits.py", line 11, in while abs(guess**3 - num) >= epsilon:</p>
  
  <p>OverflowError: (34, 'Result too large')</p>
</blockquote>

<p><strong>Edit</strong>
<a href="http://i.stack.imgur.com/qT1nZ.png" rel="nofollow"><img src="http://i.stack.imgur.com/qT1nZ.png" alt="enter image description here"></a></p>

<p>This is what my assignment sheet is telling me to do but it seems I don't need to use <code>delta = abs(guess**3 - num)/100.0</code> ?</p>

<p>The method you are using is called <a href="https://en.wikipedia.org/wiki/Newton%27s_method" rel="nofollow">Newton-Raphson approximation</a>, and you should use the first derivative of the function you are trying to solve as the denominator. Because the first derivative of <code>x^3</code> is <code>3*x^2</code>,  the iteration line must be:</p>

<pre><code>guess = guess - abs(guess**3 - num) / (3 * guess**2)
</code></pre>

<p>See the working code at <a href="https://repl.it/DqZA/0" rel="nofollow">https://repl.it/DqZA/0</a></p>

python python-3.x