In this case, equality is determined by two values being approximately equal, with a difference no greater than 0.000001 between the two values.
0.000001 is a threshold.
Since you may be dealing with numbers of arbitrary precision it's best to use Math::Decimal for performing arithmetic.use strict; use warnings; use File::Slurp; use Math::Decimal; my $THRESHOLD = '0.000001'; my ($FILE_1, $FILE_2) = ('file1.txt', 'file2.txt'); my $contents = { }; foreach my $file ($FILE_1, $FILE_2) { foreach ( (File::Slurp::read_file($file)) ) { chomp; push @{ $contents->{$file} }, (split /\s+/, $_) } } foreach my $val_1 ( @{ $contents->{$FILE_1} } ) { my $diff = Math::Decimal::dec_sub( $val_1, ( shift @{ $contents->{$FILE_2} } ), ); my $abs_diff = Math::Decimal::dec_abs($diff); my $cmp = Math::Decimal::dec_cmp( $abs_diff, $THRESHOLD ) +; die "Files NOT equal !!\n" if ($cmp > 0); } print "Files ARE equal !!\n";
In reply to Re: A small question on file comparison
by bluestar
in thread A small question on file comparison
by pavanvidem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |