How can I set a range for a float?

Asked

Viewed 493 times

2

Trying to implement Newton’s method for finding roots in polynomials, using Horner’s method.

n=len(a)-1

print("k\t  a\t x\t  px")
for k in range (0, iterMax):
    b=a[0] 
    c=b
    for i in range (n-1,1):
        b=a[i]+(b*x)
        c=x*c+b
    b=b*x+a[n]   
    if x in range of (-epsilon, epsilon)
        print ("raiz encontrada")

on the line

if x in range of (-Epsilon, Epsilon)

Compiler warns that "float' Object cannot be Interpreted as an integer"

  • 4

    This syntax is wrong. If you want to check if the value is in this range, I advise you to do if -epsilon <= x <= epsilon.

1 answer

1

It doesn’t make sense logically for you to do a for with float numbers, so you should wind up.

Use the round method.

Example:

round(3.2)

Exit:

3

If you really need to work with float numbers in a range, use the repeat loop While as long as the condition is not found.

But in your case, you’re just asking if a number is in a domain, I suggest you do it here:

n=len(a)-1

print("k\t  a\t x\t  px")
for k in range (0, iterMax):
    b=a[0] 
    c=b
    for i in range (n-1,1):
        b=a[i]+(b*x)
        c=x*c+b
    b=b*x+a[n]   
    if (x>=(-epsilon) and x<=(epsilon)): # Aqui foi feita a mudança.
        print ("raiz encontrada")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.