ZeroDivisionError:浮点除以零?

2024-05-05 08:50:46 发布

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

def fx(x):
    v = float(x*x*x - 5*x - 7)
    return v


err = int(input("Enter zeroes in approxiamtion in solution: "))
b = float(input("Enter upper limit for root: "))
a = float(input("Enter lower limit for root: "))
c = 0.0
while fx(c) > (1 * 10 ** -err):
    c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
    print(a, b, c, sep=', ')
    a = b
    b = c
    # c = 0
    print()
print("The root is approximately = ", b)

为什么我在执行这段代码时会出现零除法错误,即使循环给出了正确的值,尽管有错误?你知道吗

编辑:我把^运算符改为**,去掉了c=0,但现在答案甚至不接近实际答案。^接线员奇怪地给了我正确的答案。你知道吗

带**运算符的输出:

Enter zeroes in approxiamtion in solution: 3
Enter upper limit for root: 2
Enter lower limit for root: 1
The root is approximately =  2.0

使用^运算符输出:

Enter zeroes in approxiamtion in solution: 3
Enter upper limit for root: 2
Enter lower limit for root: 1
1.0, 2.0, 6.5
Traceback (most recent call last):

2.0, 6.5, 2.1658986175115205
  File "C:/Users/anves/PycharmProjects/nt/nt.py", line 46, in <module>

    c = b - (((b - a)/(fx(b) - fx(a))) * fx(b))
6.5, 2.1658986175115205, 2.302797651275858

ZeroDivisionError: float division by zero
2.1658986175115205, 2.302797651275858, 2.934211734811194

2.302797651275858, 2.934211734811194, 2.705017710718186

2.934211734811194, 2.705017710718186, 2.7438299655247143

2.705017710718186, 2.7438299655247143, 2.7474171932954787

2.7438299655247143, 2.7474171932954787, 2.7473464241055843

2.7474171932954787, 2.7473464241055843, 2.7473465403033757

2.7473464241055843, 2.7473465403033757, 2.747346540307211

2.7473465403033757, 2.747346540307211, 2.747346540307211

最后一个值是未打印的正确值?你知道吗


Tags: inforinput运算符rootfloatupperlower
3条回答

当我添加使用abs()**的建议,然后捕获异常时,我得到如下代码:

def fx(x):
    v = float(x*x*x - 5*x - 7)
    return v


err = int(input("Enter zeroes in approxiamtion in solution: "))
b = float(input("Enter upper limit for root: "))
a = float(input("Enter lower limit for root: "))
c = 0.0

while abs(fx(c)) > ( 10 ** -err):
    try:
        c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
    except ZeroDivisionError:
        break
    print(a, b, c, sep=', ')
    a = b
    b = c
    c = 0
    print()
print("The root is approximately = ", b)

当我运行它时,我得到:

Enter zeroes in approxiamtion in solution: 3
Enter upper limit for root: 2
Enter lower limit for root: 1
1.0, 2.0, 6.5

2.0, 6.5, 2.1658986175115205

6.5, 2.1658986175115205, 2.302797651275858

( steps omitted )

2.7473464241055843, 2.7473465403033757, 2.747346540307211

2.7473465403033757, 2.747346540307211, 2.747346540307211

The root is approximately =  2.747346540307211

这是正确的答案吗?你知道吗

您可以从倒数第二行的输出中看到,最后两个数字是相同的(b和c),下一次循环a和b将有这些值,因此fx(b) - fx(a)将导致0.0和零分错误。你知道吗

我看到两个问题。 一个是表达式(1*10^-err)。Python中的运算符^表示按位异或,10^-err是一个负整数。因此,在找到根之后,循环不会结束eevn。 应该是(10**-err)。你还得把abs(f(c))和它比较一下。你知道吗

第二个问题是c=0作为根的初始值。它应该在(a,b)间隔内,而不是零。你知道吗

我刚刚运行了您的代码,发现fx(b)fx(a)是相同的值。所以当你计算fx(b)减去fx(a)你得到0 希望这有帮助。你知道吗

Terminal -----------------------------------
Enter zeroes in approxiamtion in solution: 5
Enter upper limit for root: 5
Enter lower limit for root: 5
93.0
93.0
Traceback (most recent call last):
  File "d:/PythonScripts/first_python_project/curso_python.py", line 13, in <module>
    c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
ZeroDivisionError: float division by zero

添加更多: 当您达到较低的值时,fx(b)fx(a)会变得非常相似。 像这样:

Enter zeroes in approxiamtion in solution: 3
Enter upper limit for root: 5
Enter lower limit for root: 3
93.0
5.0
3.0, 5.0, 2.8863636363636362

2.614751596543952
93.0
5.0, 2.8863636363636362, 2.825218326106125

1.424401534793665
2.614751596543952
2.8863636363636362, 2.825218326106125, 2.7520503761266304

0.08317571970829363
1.424401534793665
2.825218326106125, 2.7520503761266304, 2.747512887487003

0.002935214303143141
0.08317571970829363
2.7520503761266304, 2.747512887487003, 2.747346905212986

6.43830337132556e-06
0.002935214303143141
2.747512887487003, 2.747346905212986, 2.747346540335565

5.002736003234531e-10
6.43830337132556e-06
2.747346905212986, 2.747346540335565, 2.747346540307211

1.7763568394002505e-15
5.002736003234531e-10
2.747346540335565, 2.747346540307211, 2.747346540307211

1.7763568394002505e-15
1.7763568394002505e-15
Traceback (most recent call last):
  File "d:/PythonScripts/first_python_project/curso_python.py", line 13, in <module>
    c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
ZeroDivisionError: float division by zero

相关问题 更多 >