Subtract timestamps over two lists

Hi I have two lists of files with UNIX timestamps like :

1707453361.999945126
1707453363.000005665
1707453364.000012006
1707453365.000004769
1707453365.999964768
1707453367.000002740
1707453368.000001631

and

1707453361.999999442
1707453363.000000053
1707453364.000001782
1707453364.999997854
1707453366.000002796
1707453366.999998237
1707453368.000002871

Now I used python with numpy with this simple code

p1 = numpy.loadtxt('/file1')
p2 = numpy.loadtxt('/file2')
freq = p2 - p1

but the result is not what I wanted. for instance I obtain this:

5.435943603515625000e-05
-5.722045898437500000e-06
-1.025199890136718750e-05
-6.914138793945312500e-06
3.814697265625000000e-05
-4.291534423828125000e-06
1.192092895507812500e-06
-2.145767211914062500e-06
I need to subtract the first timestamp of the list with the first timestamp of the second list.

For example :

1707453361.999945126 - 1707453361.999999442 = -0.000054316

How I can do it in bash or python?

In bash:

while read num1 <&3 && read num2 <&4; do echo "$num1 - $num2"; done 3< file1 4< file2 | bc

bc is needed due to the high precision.
bc omits a leading zero. I did not find a way to change this behavior. But you can add a pipe to sed:

| sed '/^-*\./ s/\./0./'
1 Like

Your problem is the accuracy to which the numbers are held. An IEEE 64-bit value is only accurate to 15 or 16 digits. As most of your pairs of values are identical in the first 14 digits, you can only get one or two correct digits in the result of the subtraction.

Obviously, these are Epoch-based seconds and nanoseconds, to 19-digit accuracy. I would separate off the first six digits of each value text, deal with the few that have a roll-over in the 10,000 seconds digit, discard the decimal point, do the arithmetic as integer, and label the result as nanoseconds.

1 Like

@BSD1
just needed a bit more mangling :slight_smile:
(could also check out the decimal module but numpy is the way to go ...)

cat example.py
import numpy as np
tstamp1 = np.loadtxt('./file1')
tstamp2 = np.loadtxt('./file2')
np.savetxt('results.txt',np.asarray(tstamp1-tstamp2,dtype=np.float64), fmt='%.10f')

python3 example.py

cat results.txt 
-0.0000543594
0.0000057220
0.0000102520
0.0000069141
-0.0000381470
0.0000042915
-0.0000011921
1 Like

thanks to all for the replies. solved.