in reply to Comparing masses in two txt files
my $count = 0; for my $list1 (@List1) { for my $list2 (@List2) { if (abs($list1 - $list2) <= .2) { $count++ } } } print("$count\n");
But beware that checking floating point numbers for equality is basically impossibe — Does anyone have a good link they can provide here? — so you might not get the results you want. .2, for example, is a periodic number in binary (just like 1/3 is a periodic number in decimal). You might need to introduce a tolerance.
my $tol = 2**(-30); # ~ 0.000000001 my $count = 0; for my $list1 (@List1) { for my $list2 (@List2) { if (abs($list1 - $list2) - $tol <= .2) { $count++ } } } print("$count\n");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Comparing masses in two txt files
by CountZero (Bishop) on Nov 05, 2008 at 19:30 UTC | |
|
Re^2: Comparing masses in two txt files
by lostjimmy (Chaplain) on Nov 05, 2008 at 19:30 UTC |