简体   繁体   中英

ZeroDivisionError: float division by zero?

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)

Why am I getting a zero division error on executing this code even though the loop gives the correct values in spite of the error?

Edit: I changed the ^ operator to ** and removed the c = 0 but now the answer is not even close to the actual answer. ^ operator was giving me the correct answer strangely.

Output with ** operator:

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

Output with ^ operator:

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

The last value is the correct value which is not getting printed?

I just ran your code, and found that fx(b) and fx(a) are the same value. So when you calculate fx(b) minus fx(a) you get 0 Hope this helps.

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

Adding some more: When you reach lower values, just fx(b) and fx(a) turn pretty similar. Something like this:

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

you are getting this error because at some point in your code

lets say fx(a)=1.0000000000000001 and fy(b)=1.0

and instead of fx(a)-fy(b)=0.0000000000000001 you are getting 0.0.

so zero division error is coming beacuse of this

I see two problems. One is with the expression (1 * 10 ^ -err) . Operator ^ in Python means bitwise XOR and 10 ^ -err is a negative integer. So your loop does not end eevn after the root was found. It should be (10 ** -err) . Also you have to compare abs(f(c)) with it.

Second problem is with c = 0 as initial value for the root. It should be within (a, b) interval, not zero.

When I add the suggestions of using abs() and ** and then catch the exception I get code like this:

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)

When I run it I get this:

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

Does this produce the right answer?

You can see from the penultimate line of output where the last two numbers are identical (b and c) that the next time round the loop a and b will have these values and so fx(b) - fx(a) will result in 0.0 and a ZeroDivisionError.

You'll need to rework your logic, but I can tell you that (1 * 10 ** -err) does not do what you think it does. ^ is a bitwise exclusive or operator. For exponents, you need ** . So your code should be

(1 * 10 ** -err)

I tried running it, and it immediately stops due to fx(0) being a negative number, which is less than the very small positive value generated in the parentheses above.

Here's a working version, although I'm not sure what the code actually accomplishes...

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


err = int(input("Enter zeroes in approximation in solution: "))
b = float(input("Enter upper limit for root: "))
a = float(input("Enter lower limit for root: "))
c = 0.0
ctr = 0 #                           ^^^^^^^^^^^^^^^^^^
while abs(fx(c)) > (1 * 10 ** -err) and fx(b) != fx(a): # ^ is binary xor!
    ctr += 1
    c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
    print(ctr, a, b, c, sep=', ')
    a = b
    b = c
    c = 0
    print()
print("The root is approximately = ", b, ' after ', ctr, ' iterations')
input()

I tried running this three times, with range 1 to 10 and 1/3/8 zeroes. All three times the result ended up with b exactly the same. It just took a different number of iterations to get there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM