Skip to content

Instantly share code, notes, and snippets.

import math
def cube_root(x):
# negative number cannot be raised to a fractional power
res = math.copysign(abs(x) ** (1.0/3), x)
# 64 ** (1/3.0) gives us 3.9999999999999996
# and it breaks things up pretty bad. let's try finding int one
rounded_res = int(round(res))
if rounded_res ** 3 == x:
res = rounded_res