Jump to content

Cube root

From Wikiversity

Introduction

[edit | edit source]
Diagram of cube (in solid black lines.)
The diagram shows the relationship of width, depth or height to surface area and volume.

A cube is a regular solid in three dimensions with depth, width and height all equal.

In the diagram the figure defined by solid black lines is a cube. Width = depth = height = 2.

The length of one side is 2 units.

The surface area of one side or or or square units. There are 4 square units in each side.

The volume of the cube cubic units.

In this case you can see that 8 cubes of 1 unit each, when properly stacked together, form a cube with length of each side equal to 2 units and volume equal to 8 cubic units.

In mathematical terms

We can say that raised to the power of 3. Usually we say that cubed.


The calculation of cube root is the process reversed. Given or volume, what is the length of each side?

read as raised to the power Usually we say that or cube root of

In this case


When then


If is negative, then and

In theoretical math the cube root of any real number has 3 values, 1 real and 2 complex.

On this page, we'll refer to the one real value noting that the cube root of a negative real number is also a negative real number.

Calculation

[edit | edit source]

Preparation

[edit | edit source]

It is desired to calculate the cube root of real number


To simplify the process, and to make the implementation of the process predictable, reformat

where:

  • is integer.


Then:


To simplify the process further, we calculate cube root of abs(n) and restore negative sign to result, if necessary.

# python code.

NormalizeNumberDebug = 0

def NormalizeNumber (number) :
  '''
sign, newNumber, exponent = NormalizeNumber (number)
sign & exponent are both ints.
newNumber is Decimal object.
1000 > newNumber >= 1 and
exponent % 3 = 0.
This prepares number for cube root of number.
eg, 1234.56e-2 becomes 12.3456
    123.456e7 becomes 1.23456e9
'''

  number = D(str(number))+0
  if number == 0 : return (0, D(0), 0)

  sign, digits, exponent = tuple(number.as_tuple())

  digits = list(digits)

  # Remove leading zeroes.
  while (digits[0] == 0) : digits[:1] = []

  # Remove trailing zeroes.
  while (digits[-1] == 0) :
    digits[-1:] = [] ; exponent += 1

  # Ensure that there are at least 3 digits.
  while ( len(digits) < 3 ) :
    digits += [0];  exponent -= 1

  # Ensure that exponent is exactly divisible by 3.
  while exponent % 3 :
    digits += [0] ; exponent -= 1

  # Insert the decimal point so that there are exactly 1 or 2 or 3
  # digits to left of decimal point.
  len1 = len(digits) % 3
  if len1 == 0 : len1 = 3
  len2 = len(digits) - len1

  digits[len1:len1] = '.' ; exponent += len2

  # Produce number reformatted.
  str1 = ''.join( [ str(v) for v in digits ] )
  newNumber = D(str1)

  # If necessary, check.
  if NormalizeNumberDebug :
      v1 = D( ('', '-')[sign] + str1 + 'e' + str(exponent) )
      if v1 != number :
          print ('NormalizeNumber (number) : error', v1 , '!=', number)

  return sign, newNumber, exponent

Implementation

[edit | edit source]


To calculate calculate the real root of:


is well defined in the region

Newton's method is used to derive the root starting with

# python code.
simpleCubeRootDebug = 0

def simpleCubeRoot (N) :
    if simpleCubeRootDebug :
        print ('simpleCubeRoot (N): N =',N)
    if N == 0 : return D(0)
    if abs(N) == 1 : return D(str(N))

    sign1, n, exponent = NormalizeNumber (N)

    if 1 <= n < 1000 : pass
    else :
        print ('simpleCubeRoot (N) : internal error 1.')
        return None

    x = 5         # Starting value of x.
    y = x*x*x - n # Starting value of y.
    count = 33 ; L1 = []

    while count :
        count -= 1
        if simpleCubeRootDebug :
            print ('simpleCubeRoot (N) : x,y =',x,y)
        slope = 3*x*x
        delta_x = y/slope
        x -= delta_x
        if x in L1[-1:-5:-1] :
            # This value of x has been used previously.
            break
        L1 += [x]
        y = x*x*x - n

    if count == 0 :
        print ('simpleCubeRoot (N): count expired.')

    multiplier1 = (1,-1)[bool(sign1)]
    exponent1, remainder = divmod (exponent, 3)
    if remainder :
        print ('simpleCubeRoot (N): internal error 2.')
        return None

    multiplier2 = 10**D(exponent1)
    root3 = (multiplier1 * x * multiplier2).normalize() # The cube root.
    if simpleCubeRootDebug :
        print ('simpleCubeRoot (N): root3 =',root3)
    return root3

Examples

[edit | edit source]

(-27)^(⅓)

[edit | edit source]
Graph of cubic function used to calculate cube root of -27.
axis compressed for clarity.
Newton's method quickly finds result:
# python code.

import decimal
D = decimal.Decimal

simpleCubeRootDebug = 1

N = -27
v = simpleCubeRoot (N)
print ('cube root of',N,'=',v)
simpleCubeRoot (N): N = -27
simpleCubeRoot (N) : x,y = 5 98.000
simpleCubeRoot (N) : x,y = 3.693333333333333333333333333 23.37969303703703703703703703
simpleCubeRoot (N) : x,y = 3.122011871507368646651056170 3.43011897993565925386902153
simpleCubeRoot (N) : x,y = 3.004706248711930856949932920 0.12726815845223303319846682
simpleCubeRoot (N) : x,y = 3.000007367513256026917293862 0.00019892334643539087699845
simpleCubeRoot (N) : x,y = 3.000000000018093357946763316 4.8852066456555586E-10
simpleCubeRoot (N) : x,y = 3.000000000000000000000109123 2.94632E-21
simpleCubeRoot (N) : x,y = 3.000000000000000000000000000 0E-26
simpleCubeRoot (N): root3 = -3

cube root of -27 = -3

N small

[edit | edit source]
# python code.

import decimal
D = decimal.Decimal

simpleCubeRootDebug = 1

N = (D('654.12345')**3)* D('1e-234')
v = simpleCubeRoot (N)
print ('cube root of',N,'=',v)
simpleCubeRoot (N): N = 2.79884698523170070963625E-226
simpleCubeRoot (N) : x,y = 5 -154.884698523170070963625
simpleCubeRoot (N) : x,y = 7.065129313642267612848333333 72.7786652269624099669721028
simpleCubeRoot (N) : x,y = 6.579122227796059655581363000 4.8916155561565606595741185
simpleCubeRoot (N) : x,y = 6.541452268317643764895392436 0.0279543788313674002066771
simpleCubeRoot (N) : x,y = 6.541234507249539089853395935 9.305743731604330476E-7
simpleCubeRoot (N) : x,y = 6.541234500000000008034541024 1.0313397688E-15
simpleCubeRoot (N) : x,y = 6.541234500000000000000000000 0E-25
simpleCubeRoot (N): root3 = 6.5412345E-76

cube root of 2.79884698523170070963625E-226 = 6.5412345E-76

N with 102 decimal digits

[edit | edit source]
# python code.

import decimal
D = decimal.Decimal

prec = decimal.getcontext().prec = 110.   # Precision.

simpleCubeRootDebug = 0

N = D('91234567890.12345678901234567890123')**3

v = simpleCubeRoot (N)
print ('cube root of',N,'=',v)
cube root of 759413404032709802223035921205529.781633123988862756497856617560063741408069807576943069432557725290867 = 91234567890.12345678901234567890123
  • Result is achieved with 10 passes through loop.
  • In python this method is more than 3 times faster than raising a number to the power As size of increases, speed advantage increases.
  • This method produced the cube root exactly.

Links to related topics

[edit | edit source]

Cubic formula

Square Roots using Newton’s Method

Shifting nth root algorithm

Powers, roots, and exponents

Roots

Exercises