This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |