If you use the pack 'd', $value; approach you will likely end up with every key in your hash having a value of 1. Even a single bit difference in the lowest significant bit will prevent 2.0 & 2.000000000000000001 being considered the same.

Your best bet might be to use sprintf to truncate and stringify the values into a standardised format to whatever precision fits with your needs. Eg.

$hash{ sprintf '%.6e', $number };

This will rationalise the continuous range of inputs into a set of discrete bands to whichever precision you need. This shows the effect of using 6 and 11 digits of precision on a range of pairs of values that have a small difference between them as input (and would be different keys using pack 'd'):

printf "%20s becomes %.6e\n", $_, $_ for map{ ( $_, ( $_ + 1e-10 ) ) } map{ "2e$_" } -300, -30, -3, 0, 3, 30, 300; 2e-300 becomes 2.000000e-300 1e-010 becomes 1.000000e-010 2e-30 becomes 2.000000e-030 1e-010 becomes 1.000000e-010 2e-3 becomes 2.000000e-003 0.0020000001 becomes 2.000000e-003 2e0 becomes 2.000000e+000 2.0000000001 becomes 2.000000e+000 2e3 becomes 2.000000e+003 2000.0000000001 becomes 2.000000e+003 2e30 becomes 2.000000e+030 2e+030 becomes 2.000000e+030 2e300 becomes 2.000000e+300 2e+300 becomes 2.000000e+300 printf "%20s becomes %.11e\n", $_, $_ for map{ ( $_, ( $_ + 1e-10 ) ) } map{ "2e$_" } -300, -30, -3, 0, 3, 30, 30 2e-300 becomes 2.00000000000e-300 1e-010 becomes 1.00000000000e-010 2e-30 becomes 2.00000000000e-030 1e-010 becomes 1.00000000000e-010 2e-3 becomes 2.00000000000e-003 0.0020000001 becomes 2.00000010000e-003 2e0 becomes 2.00000000000e+000 2.0000000001 becomes 2.00000000010e+000 2e3 becomes 2.00000000000e+003 2000.0000000001 becomes 2.00000000000e+003 2e30 becomes 2.00000000000e+030 2e+030 becomes 2.00000000000e+030 2e300 becomes 2.00000000000e+300 2e+300 becomes 2.00000000000e+300

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Histogramming -- Floating point numbers as hash keys by BrowserUk
in thread Histogramming -- Floating point numbers as hash keys by dokkeldepper

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, details, 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, summary, 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.