#!/afs/sipb/project/perldev/p -w 
#-0777
# output differences of numbers in two files

use strict;
use IO::File;

die "usage: numdelta fileA fileB\n" unless (@ARGV == 2);

my $ah = new IO::File $ARGV[0], 'r';
my $bh = new IO::File $ARGV[1], 'r';

my(@a,@b,$rah,$rbh);

my ($ax, $bx, $dead);
my ($maxerr) = 0;

LINE:
while (1) {
  $rah = <$ah>;  $rbh = <$bh>;
  (defined $rah || defined $rbh) or last LINE;
  defined $rah || die "mismatch: EOF A, file B has extra '$rbh'\n";
  defined $rbh || die "mismatch: EOF B, file A has extra '$rah'\n";

  @a = split /([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]\d+)?)/i, $rah;
  @b = split /([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]\d+)?)/i, $rbh;

 FRAG:
  while (1) {
    (@a || @b) || last FRAG;

    @a || die "mismatch: EOL A, file B has extra '".join('', @b)."'\n";
    @b || die "mismatch: EOL B, file A has extra '".join('', @a)."'\n";

    print length($a[0]) ? $a[0] : ' ';
    $ax = shift(@a);   $ax =~ s/^\s+//;   $ax =~ s/\s+$//;
    $bx = shift(@b);   $bx =~ s/^\s+//;   $bx =~ s/\s+$//;

    ($ax eq $bx) || die "mismatch: non-numbers '$ax' vs '$bx'\n";

    (@a || @b) || last FRAG;
    @a || die "mismatch: EOL A, file B has extra '".join('', @b)."'\n";
    @b || die "mismatch: EOL B, file A has extra '".join('', @a)."'\n";

    $ax = shift(@a);
    $bx = shift(@b);

    printf "%20.16e", ($ax - $bx);
    $maxerr = abs($ax - $bx) if $maxerr < abs($ax - $bx);
  }
}
warn "max err = $maxerr\n";
