in reply to Histogramming -- Floating point numbers as hash keys
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
|
|---|