嵌套函数的递归表示有多高的精度

2024-04-24 16:35:35 发布

您现在位置:Python中文网/ 问答频道 /正文

为了让我的问题更容易理解,我将在这里使用一个python示例:

采用嵌套正弦函数的递归表示法:

def nestedSin(val, amount): ## val in degrees

    if amount == 1:

        return math.sinh(val)

    return math.sinh(nestedSin(val, amount-1))

如果我运行这个函数:

z = nestedSin(20, 3)

我会得到:

z = math.sinh(math.sinh(math.sinh(20)))

我的问题是…这将计算第一个正弦函数并返回它的舍入值(即浮点限制),然后计算该返回值的正弦等。?你知道吗

或者浮点限制只适用于递归函数的最终返回值?你知道吗

基本上,我是在问上述是否比:

x = math.sinh(20)
y = math.sinh(x)
z = math.sinh(y)

Tags: 函数in示例returndefmathvalamount
2条回答

根据你的问题

will this compute the first sine function and return it's rounded value (i.e. floating point limitation) and then compute the sine of that returned value etc

函数返回第一个浮点值,然后再次调用def nestedSin(val, amount)函数,并使用最新的四舍五入值调用它,当超出范围时,它将提升

OverflowError: math range error

所以这两个例子是一样的。你知道吗

OverflowError:-当算术运算的结果太大而无法表示时引发。你知道吗

实际上,3sinh操作是连续执行的,每个操作都有自己的舍入;就像您的x y z示例一样。你知道吗

所以,不,没有区别;我相信所有的方法都是同样准确的。你知道吗

相关问题 更多 >