import math
r = 0.1 # 球的半径
rho = 0.638 # 球的密度
rho_w = 1.0 # 水的密度
s = rho / rho_w # 球的比重
def f(h):
Vh = (math.pi / 3) * (3 * r * h ** 2 - h ** 3) # 高度为h的体积
Vs = (4 / 3) * math.pi * r ** 3 # 球的体积
Vw = Vs - Vh # 排出水的体积
return s * Vs - Vw
a = 0.0 # 初始区间左端点
b = 2 * r # 初始区间右端点
while b - a > 1e-5: # 精度控制
c = (a + b) / 2
if f(c) < 0:
a = c
else:
b = c
print("球体浸入水中的深度为:", round(b - r, 5), "m")