为错误函数erf(x)编写while循环?

2024-04-16 13:04:37 发布

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

我知道python中有一个erfWikipedia)函数。但是在这个赋值中,我们被特别要求编写error函数,就像它在使用while loop时还没有在python中实现一样。你知道吗

erf (x)已经简化为:(2/ (sqrt(pi)) (x - x^3/3 + x^5/10 - x^7/42...)

必须添加序列中的项,直到绝对总数小于10^-20。你知道吗


Tags: 函数looppi序列errorsqrtwikipedia赋值
1条回答
网友
1楼 · 发布于 2024-04-16 13:04:37

首先-所以不是人们为你编码的地方,这里的人们帮你解决特定的问题不是整个任务

任何方式: 实现维基百科算法并不难:

import math


def erf(x):
    n = 1
    res = 0
    res1 = (2 / math.sqrt(math.pi)) * x
    diff = 1
    s = x
    while diff > math.pow(10, -20):
        dividend = math.pow((-1), n) * math.pow(x, 2 * n + 1)
        divider = math.factorial(n) * (2 * n + 1)
        s += dividend / divider
        res = ((2 / math.sqrt(math.pi)) * s)
        diff = abs(res1 - res)
        res1 = res
        n += 1
    return res


print(erf(1))

请仔细阅读源代码,并张贴所有你不明白的问题。你知道吗

Also you may check python sources并查看erf是如何实现的

相关问题 更多 >