0

I'm implementing an algorithm in Python (GoogleColab) and it outputs two lists; now, I would like to verify if this lists are equal, actualy I know that they should be equal.

enter image description here

The problem is that sometimes I get 4.099999999999998 and 4.099999999999994. I assume this is a problem with the floating point representation, how can I fix this? Also, because of this the value, Interval = 19.999999999999996 when it clearly should be 20.

didiegop
  • 139
  • 3
  • Does this answer your question? [compare two floats for equality in Python](https://stackoverflow.com/questions/33024258/compare-two-floats-for-equality-in-python) – sqz Oct 20 '21 at 15:39
  • 1
    4.099999999999998 and 4.099999999999994. are different values. nkeLeAdo, why do you want them to compare as the same? How different can 2 values be and still be "equal"? – chux - Reinstate Monica Oct 20 '21 at 19:35
  • In what sense should the lists be “equal”? In mathematics, two numbers are equal only if they are exactly the same. There is no such thing as two different numbers being equal just because they are close to each other. If your algorithm produces two lists that should have **identical** results, then the two lists are equal if and only if each pair of corresponding numbers in them is equal. If your algorithm computes some results using different algorithms with floating-point operations that produce approximations of real-number arithmetic, then generally the lists are not expected to be equal. – Eric Postpischil Oct 21 '21 at 11:03
  • If you want to test for bugs in the two algorithms, and it is acceptable that the results are affected by rounding errors, then the way to test for bugs is to examine the algorithms and the possible ranges of inputs and intermediate values and to calculate some bounds on what the errors in the final results could be. If the final results go outside those bounds, it indicates a bug. If they do not go outside those bugs, the results could be correct (or could be affected by a bug that has not caused the errors to go outside those bounds). – Eric Postpischil Oct 21 '21 at 11:04
  • That sort of testing for bugs is one of the few reasons it is useful to compare floating-point numbers for closeness. But the amount of error you should accept before declaring a bug is completely dependent on the operations performed and the data used. There is no general answer for how much to allow. – Eric Postpischil Oct 21 '21 at 11:06

0 Answers0