4/04 Is there a good way to diff 2 files that consist of columns of
floating point numbers, such that it only tells me if there's a
difference if the numbers on a given line differ by at least a given
ratio? Say, 1%?
\_ Use Excel.
1. Open foo.txt in Excel. It should convert all numbers to cells in
column A.
2. Open bar.txt in Excel. Ditto.
3. Create a new file in Excel.
4. In cell A1 of the new file, enter this:
=IF(foo.txt!A1=0, IF(bar.txt!A1=0, "same", "different"), IF(ABS((b\
ar.txt!A1-foo.txt!A1)/foo.txt!A1)<1%, "same", "different"))
5. Select cell A1. Hit Ctrl-C to copy.
6. Select the same number of cells as the number of cells that exist
in foo.txt or bar.txt. Hit Ctrl-V to paste.
7. Hit Ctrl-F. In "Find what:", enter "different". In "Look in:",
choose "Values". Click "Find All".
Another way is to write a C program to read the two files using
fscanf("%f") an do the arithmatic.
\_ does this help?
#!/usr/bin/python
import sys
threshold = float(sys.argv[3] if len(sys.argv) > 3 else 0.0000001)
list1 = [float(v) for v in open(sys.argv[1]).readlines()]
list2 = [float(v) for v in open(sys.argv[2]).readlines()]
line = 1
for v1, v2 in zip(list1, list2):
if abs((v1 - v2) / v1) >= threshold:
print "Line %d different (%f != %f)" % (line, v1, v2)
line += 1
\_ Something like this might work ($t is your threshold):
$ perl -e '$t = 0.1 ; while (<>) { chomp($_); ($i,$j) =
split(/ \t/, $_); if ($i > ((1+$t)*$j) || $i < ((1-$t)*j)) {
print "$_\n"; }}' < file |