Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Find out missing floating value in an array by roboticus
in thread Find out missing floating value in an array by manimarank

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (2)
    As of 2020-08-04 03:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (31 votes). Check out past polls.

      Notices?