[lug] Perl, sorting hashtables by value, and floating-point

Ed Hill ed at eh3.com
Mon May 31 19:19:03 MDT 2004

On Mon, 2004-05-31 at 20:16, Chris Riddoch wrote:

> This completely fails to work when those numbers are floating point.
> I've tried precomputing logarithms to make the numbers more easily
> computable, with no change in Perl's sorting ability.  I've tried 'use
> bignum' in case it might help, but it seems to overload everything but
> the spaceship (<=>) operator.
> I'm certain there's a way to do this.  

Hi Chris,

Heres an ugly implementation but it will work:

 1) write all the pairs of keys and their current ordinal 
    location to a temp file

 2) "cat temp_file | sort -n > sorted" ...

 3) permute as necessary using "sorted"


ps - Sorry to be pedantic, but those IEEE 754 "double precision 
  floats" that you're using are, in all likelihood, the same 
  width on 64-bit Opterons as they are on 32-bit Pentiums or 
  64-bit ALPHAs or 32-bit PPCs or 64-bit Itaniums or...   The
  64-bit-ed-ness really refers to the size of the CPU registers 
  and, thus, the size of the pointers that the CPU can gracefully 
  handle.  It has nothing to do with the size of "double" 
  precision floats...  unless you're on an older CRAY system!!!


Edward H. Hill III, PhD
office:  MIT Dept. of EAPS;  Rm 54-1424;  77 Massachusetts Ave.
             Cambridge, MA 02139-4307
emails:  eh3 at mit.edu                ed at eh3.com
URLs:    http://web.mit.edu/eh3/    http://eh3.com/
phone:   617-253-0098
fax:     617-253-4464
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20040531/629a82af/attachment.pgp>

More information about the LUG mailing list