Decimal sort

  • Follow


Is there any way to sort floating point numbers in perl?  Every
example I have tried has failed.  Numerical sort appears to NOT to  be
a  numerical sort but rather an integer sort.

Thanks,

Marshall
0
Reply mdudley 3/3/2010 8:00:50 PM

mdudley wrote:
> Is there any way to sort floating point numbers in perl?  Every
> example I have tried has failed.  Numerical sort appears to NOT to  be
> a  numerical sort but rather an integer sort.

Perhaps you are sorting them as strings instead of numbers?



John
-- 
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway
0
Reply John 3/3/2010 8:41:08 PM


mdudley wrote:
> Is there any way to sort floating point numbers in perl?  Every
> example I have tried has failed.  Numerical sort appears to NOT to  be
> a  numerical sort but rather an integer sort.
> 

worksforme
perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))'
0.1
0.25
0.3
0.5
0.7
0
Reply Steve 3/3/2010 9:04:44 PM

mdudley <mdudley@king-cart.com> wrote:
>Is there any way to sort floating point numbers in perl?  Every
>example I have tried has failed.  Numerical sort appears to NOT to  be
>a  numerical sort but rather an integer sort.

Please post a minimal working sample program including sampe data as
needed that demonstrates your problem. Because every example that I have
tried has worked just fine. 

jue
0
Reply J 3/3/2010 9:26:08 PM

Steve C wrote:
> mdudley wrote:
>> Is there any way to sort floating point numbers in perl?  Every
>> example I have tried has failed.  Numerical sort appears to NOT to  be
>> a  numerical sort but rather an integer sort.
>>
> 

Oops.  Forgot about default sort function being string cmp.  Although that
tends to work in a surprising number of cases.

perl -e 'print join ("\n", sort {$a <=> $b} (0.1, 0.7, -0.3, 0.25, 0.5))'
-0.3
0.1
0.25
0.5
0.7
0
Reply Steve 3/3/2010 9:52:43 PM

mdudley <mdudley@king-cart.com> wrote:

> Is there any way to sort floating point numbers in perl?  Every
> example I have tried has failed.  Numerical sort appears to NOT to
> be a  numerical sort but rather an integer sort.

I suspect that reading the docs for sort() will help.  There are even 
examples.
0
Reply darkon 3/3/2010 9:55:10 PM

Steve C <smallpond@juno.com> writes:

> mdudley wrote:
>> Is there any way to sort floating point numbers in perl?  Every
>> example I have tried has failed.  Numerical sort appears to NOT to  be
>> a  numerical sort but rather an integer sort.
>>
>
> worksforme
> perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))'
> 0.1
> 0.25
> 0.3
> 0.5
> 0.7

Coincidence. That's why you must read documentation...

perl -e 'print join("\n", sort ( 0.0001, 0.000002, 0.1 ))'
0.0001
0.1
2e-06


Note that sort defaults to string compare:

perl -e 'print join("\n", sort { $a <=> $b } ( 0.0001, 0.000002, 0.1 ))'
2e-06
0.0001
0.1

-- 
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
0
Reply John 3/3/2010 9:55:48 PM

Steve C <smallpond@juno.com> wrote:
> mdudley wrote:
>> Is there any way to sort floating point numbers in perl?  Every
>> example I have tried has failed.  Numerical sort appears to NOT to  be
>> a  numerical sort but rather an integer sort.
>> 
>
> worksforme
> perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))'
> 0.1
> 0.25
> 0.3
> 0.5
> 0.7


Doesn't work for me...

    perl -e 'print join ("\n", sort (2.0, 12.0))'
12
2

Your's is not a numeric sort at all, it is a stringwise sort.

    perl -e 'print join ("\n", sort {$a <=> $b} (2.0, 12.0))'
2
12


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
0
Reply Tad 3/3/2010 9:56:32 PM

7 Replies
1124 Views

(page loaded in 0.217 seconds)

Similiar Articles:













7/25/2012 9:11:11 AM


Reply: