LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sort -n, huge number (https://www.linuxquestions.org/questions/programming-9/sort-n-huge-number-857945/)

kaz2100 01-22-2011 05:42 AM

sort -n, huge number
 
Hya,

Question
With 'sort -n': How huge can numbers be?

So far, I did
1. Check man page (info page as well) -> Not quite informative. (locale may be important.)
2. Experiment
Code:

>cat testsort
1
11111
11111111111111
111111111111111111111
1111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111110
0.1
0.000000000000000000001
000.1
100.001
100.1
1.0
1.0001
1
1.00000000000000000000000000000000000000000000000000000000000000000000000000
1.00

> cat testsort | sort -n
0.000000000000000000001
000.1
0.1
1
1
1.0
1.00
1.00000000000000000000000000000000000000000000000000000000000000000000000000
1.0001
100.001
100.1
11111
11111111111111
111111111111111111111
1111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111110
11111111111111111111111111111111111111111111111111
>

It looks good, except for (unnecessary) leading and trailing zero.

Does anybody know the answer?

Happy Penguins!

Nominal Animal 01-22-2011 09:53 AM

The general_numcompare() function in sort from coreutils-8.9 converts the numbers to the native long double type using the standard strtold() library function. This is used when the -g option is used.

Option -n uses strnumcmp() defined in coreutils-8.9/lib/strnumcmp-in.h, which does not do any numerical conversions. It is purely string-based, and should be able to compare any decimal number strings that fit into memory.

I sincirely hope this was not homework,
Nominal Animal

gnashley 01-22-2011 10:17 AM

I'd guess they would be limited by something like MAX_LINE -probably at least 1024 chars long -or by how much RAM you have for storing it all. You could do without the 'cat' command for faster results:
Code:

sort -n testsort

Nominal Animal 01-22-2011 11:28 AM

@gnashley: Some sort implementations may have line length limits, but not GNU sort from the coreutils package. From info sort:
Quote:

GNU `sort' (as specified for all GNU utilities) has no limit on input line length or restrictions on bytes allowed within lines. In addition, if the final byte of an input file is not a newline, GNU `sort' silently supplies one. A line's trailing newline is not part of the line for comparison purposes.
So yes, the limit seems to be the available memory (RAM + swap, or resource limits, whichever are smaller).
Nominal Animal

kaz2100 01-23-2011 06:12 AM

Hya

thanks!

My bad! I did not think of checking the source code.

This is not my homework, it is rather my job (second job). Now I feel good.

The other day, I hit the size limit of long long int in C. Of course, it did not happen in test run, and it took several days to debug. I won't run into similar problem.

Happy Penguins!


All times are GMT -5. The time now is 05:28 PM.