题解 | #求解立方根# 最强解法(没有之一
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import struct
def fast_cube_root(num: float) -> float:
abs_num = abs(num)
x = abs(num)
i = struct.unpack('I', struct.pack('f', abs_num))[0] # evil floating point bit level hacking
i = (i >> 2) + (i >> 4) + (i >> 6) + (i >> 8) + (i >> 10) + (i >> 12)+ 0x2a555555 # what the fuck?
y = struct.unpack('f', struct.pack('I', i))[0]
y = (2 * y + x / (y * y)) *0.3332 # 1st iteration
# y = (2 * y + x / (y * y)) *0.3332 # 2nd iteration, this can be removed
y =-y if num < 0 else y
return round(y,1)
print(fast_cube_root(float(input())))

