同一个函数却得到不同的值?

各位大佬,

为什么会出现这种情况?
同一个函数,直接把r输入进入就得到一个数
但是手动的计算得到r之后再输入函数,就得到了0

运行得到

image
为什么第一行与第三行不相等???

这函数是这样的

PS:我是单核CPU运行的

你的h 是多少?看起来if语句这里出现了问题。print可能只是一个数字的一部分而不是全部。

h就是1

I could not reproduce your results:

import math 
def test(r):
    res = 0.0
    h = 1.0
    if r < h:
        x = 1.0 - r / h 
        res = -30.0 / (math.pi * h **3) * x * x

    return res 

print(f"test(1.076429) = {test(1.076429)}")
r = 1.076429
print(f"test(r) = {test(r)}")

The results is shown as below:

test(1.076429) = 0.0
test(r) = 0.0