manimarank:
Here's a hint on how to do it with floats:
Sort the array, then check the intervals (adjacent values in the array). You'll either find the value, or you'll find an interval where it *should* be, but isn't.
Note: Since many floating point values aren't representable in a binary format, you'll want to use the trick of selecting an arbitrary amount of error you'll accept when you try to determine whether two floats match. For example:
#!/usr/bin/perl -w
use warnings;
use strict;
my $epsilon=0.000005;
my $desired=1.15;
while (<DATA>) {
chomp;
if (abs($desired - $_) < $epsilon) {
print "Match found: $_\n";
}
}
__DATA__
1.149
1.1499
1.14999
1.149999
1.1499999
1.14999999
1.149999999
Prints
$ perl 659004.pl
Match found: 1.149999
Match found: 1.1499999
Match found: 1.14999999
Match found: 1.149999999
... roboticus
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|