Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Why does my 'less than' test fail? [closed]

Tags:

python

I have the following program:

def eigen(a, b, c, d, e, f):
    epsilon = 1E-9
    a = abs(float(a*e+b*f)/float(c*e+d*f) - float(e)/float(f))
    print a, epsilon
    print a < epsilon
    print abs((a*e+b*f)/float(c*e+d*f) - e/float(f)) < epsilon

Prints:

0.0 1e-09
True
False

when the values of a,b,c,...,f = 3, 1, 1, 3, 4, -4

I expected the last two lines to both print True as I thought they were equivelent statements. Will you kindly explain to me what is going on?

Thank you for your time.

Kind regards,

Marius

like image 251
Mikkel Rev Avatar asked Mar 27 '26 17:03

Mikkel Rev


1 Answers

The problem is that

a = abs(float(a*e+b*f)/float(c*e+d*f) - float(e)/float(f))

assigns a different value to a, which messes up the calculation that follows:

print abs((a*e+b*f)/float(c*e+d*f) - e/float(f)) < epsilon
#          ^ This is no longer the original a
like image 87
NPE Avatar answered Apr 02 '26 22:04

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!