Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Histogramming -- Floating point numbers as hash keysby dokkeldepper (Friar) |
on Dec 17, 2005 at 21:27 UTC ( [id://517520]=perlquestion: print w/replies, xml ) | Need Help?? |
dokkeldepper has asked for the wisdom of the Perl Monks concerning the following question: 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.
Back to
Seekers of Perl Wisdom
|
|