0

This is a part of the code that is relevant:

if msgheader.startswith("Election"):
    electionMessage = msgheader.split()
    electionMessage = electionMessage[1]
    print "---My iD: %s Incoming Message: %s" %(iD, electionMessage)

    sleep(5)
    print "this is the result: " + str(float(iD) - float(electionMessage))
    if iD == electionMessage: #This is the actual code that i want to run but 
#never runs so i tried the test above to subtract the float values.

This is the result that i get from the code above:

---My iD: 0.716176767833 Incoming Message: 0.716176767833
this is the result: 4.5263792714e-13

How is this possible? As described in the code i want to compare if iD equals to electionMessage but it never entered that if statement so i started testing with outputs and subtracting and now i get this? Anyone knows why?

Timo Cengiz
  • 3,367
  • 4
  • 23
  • 45

1 Answers1

1

From https://docs.python.org/2.7/tutorial/floatingpoint.html

"It’s easy to forget that the stored value is an approximation to the original decimal fraction, because of the way that floats are displayed at the interpreter prompt. Python only prints a decimal approximation to the true decimal value of the binary approximation stored by the machine"

So i guess the numbers are not really equal, they just seems to be on the prompt

I suggest np.isclose() from numpy

Gábor Erdős
  • 3,599
  • 4
  • 24
  • 56