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: Decimal sort - comp.lang.perl.miscIs 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 ... insert a decimal point - comp.soft-sys.sasDecimal sort - comp.lang.perl.misc insert a decimal point - comp.soft-sys.sas Decimal sort - comp.lang.perl.misc insert a decimal point - comp.soft-sys.sas I have this ... sorting even and odd numbers - comp.databases.filemakerDecimal sort - comp.lang.perl.misc Is there any way to sort floating point numbers in perl? Every example I have tried has failed. ... I suspect that reading the docs for ... DMS to DD - comp.sys.hp48Decimal sort - comp.lang.perl.misc DMS to DD - comp.sys.hp48 Decimal sort - comp.lang.perl.misc Decimal sort - comp.lang.perl.misc Converting from decimal degrees to DMS ... decimal to exact how? - comp.sys.hp48Decimal sort - comp.lang.perl.misc decimal to exact how? - comp.sys.hp48 Decimal sort - comp.lang.perl.misc Is there any way to sort floating point numbers in perl? How may decimal places SAS keep when do calculation - comp.soft ...Decimal sort - comp.lang.perl.misc How may decimal places SAS keep when do calculation - comp.soft ... Decimal sort - comp.lang.perl.misc How may decimal places SAS keep ... Decimal to Roman Numerals - comp.sys.hp48Decimal sort - comp.lang.perl.misc Decimal sort - comp.lang.perl.misc Decimal to Roman Numerals - comp.sys.hp48 Decimal sort - comp.lang.perl.misc Decimal sort - comp.lang ... dropping numbers after the decimal - comp.soft-sys.matlab ...Decimal sort - comp.lang.perl.misc dropping numbers after the decimal - comp.soft-sys.matlab ... Decimal sort - comp.lang.perl.misc dropping numbers after the decimal ... Rounding (up/down) to a certain number of decimal places - comp ...Decimal sort - comp.lang.perl.misc Rounding (up/down) to a certain number of decimal places - comp ... Decimal sort - comp.lang.perl.misc Rounding (up/down) to a certain ... Degrees, Minutes, Seconds on the 49G+ - comp.sys.hp48Decimal sort - comp.lang.perl.misc Degrees, Minutes, Seconds on the 49G+ - comp.sys.hp48 Decimal sort - comp.lang.perl.misc Degrees, Minutes, Seconds on the 49G+ - comp ... Decimal Sort - SharendipityThe goal of this game is to help you practice your ordering skills with decimals. When you get started, an angry cloud will blow the petals off the flower. It is your ... Unit 2 Section 2 : Decimals and Place ValueSort these numbers into order: 0.7, 0.27, 0.72, 0.702, 0.207, 0.2 Rounding to a certain number of decimal places If we want to round 4.732 to 2 decimal places, it will ... 7/25/2012 9:11:11 AM
|