Dear comonks,

for the purpose of building histograms for data sets containing some millions of numbers, that is counting for several millions of pairs ($key,$value) the number of $values for every value.

A standard approach is to build a hash %values and increment $values{$value}++ while iterating over the data. However, because the $value (s) are floats this may be misleading because of possibly varying internal representation of the same real number.

Or, as the Third Coming of The Camel said 'floating-point numbers are in native machine format only'.

Is there some way (module, idiom) to represent real numbers such that the same real number gets the same floating point representation, so that I can use them as hash keys for the approach above? Or, is there a way to rethink that problem, to get the same result, that is, a histogram?

May $values{pack("d",$value)} be a usefuld approach?

Finally, it would be very useful to have the $values preserved as numbers, because I will do computations with the histograms (WARPing, smoothing, estimation of spectra).

Thank you , deckel the doppel 8-)

-dokkeldepper

Remarks: In response to some replies: I ask for a consistent representation of floats not for precision.

A histogram has a priori nothing to do with graphics. For example, most advanced databases compute histograms of the data to guess the optimality of query execution plans. The graphical representation of a histogram is not my concern.


In reply to 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.