1

I am trying to use Python to compute the difference between two text files and print the first value and location where they start to diverge.

I am not sure how to use loadtxt:

import numpy as np
a = np.loadtxt("path/to/file", float)
b = np.loadtxt("path/to/file2", float)    
while np.absolute(a - b) !=0:


1
2
3
...

Not sure how to finish this? Is the start correct?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
dustin
  • 4,309
  • 12
  • 57
  • 79

1 Answers1

3

You could use

idx = np.where(np.abs(a-b) > 1e-6)[0]
firstidx = idx[0]

to find the first index where the values in a and b differ by more than some nominal amount like 1e-6:

import numpy as np

a = np.loadtxt("path/to/file", float)
b = np.loadtxt("path/to/file2", float)

idx = np.where(np.abs(a-b) > 1e-6)[0]
firstidx = idx[0]
print(firstidx, a[firstidx], b[firstidx])

Note that when dealing with floats, you rarely if ever want to compare with equality, such as with

np.abs(a-b) == 0

or the converse,

np.abs(a-b) != 0

because the inaccuracy of floating point representations can cause a and b to be slightly different even when their values should be exactly the same if their values were represented with infinite precision.

So use something like

np.abs(a-b) > 1e-6

instead. (Note that you have to choose a level of tolerance, e.g. 1e-6).


Here is a simple example demonstrating the pitfall of comparing floats using equality:

In [10]: 1.2-1.0 == 0.2
Out[10]: False
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677