Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Find out missing floating value in an array

by roboticus (Chancellor)
on Dec 26, 2007 at 15:32 UTC ( #659051=note: print w/replies, xml ) Need Help??


in reply to Find out missing floating value in an array

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://659051]
help
Chatterbox?
and the web crawler heard nothing...

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










    Results (78 votes). Check out past polls.

    Notices?