Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The mnemonics such as <code>-lt</code> might be said to come from the original Fortran comparators such as <code>.LT.</code> from the late 1950s.</p> <p>Yes, in the shell, <code>-lt</code> does a 'less than' numeric comparison. (Be aware, though, that in Perl, the numeric comparisons are <code>&lt;</code> etc, and the string comparisons are denoted by the alphabetic operators such as <code>-lt</code>!)</p> <p>However, in some, perhaps many, shells the conversion and comparison may well be done in the local long integer format. If you're on a 32-bit machine, the values you quote exceed the 32-bit (signed) range by a factor of 10 or so. On a 64-bit machine, or with a shell that uses <code>long long</code>, you'd be OK.</p> <p>The hex equivalents of the decimal numbers are 021715402012 = 0x50E56BD1C and 021815402012 = 0x5144C9E1C; they can't be octal because of the 8. (However, if the shell does interpret the leading zero as 'octal', then the second number is just 021 or 17 decimal because the 8 ends the octal number. However, the 64-bit shells I tested on (Mac OS X 10.7.3 and RHEL 5) both seemed to treat them as decimal, not octal.)</p> <p>The example code below, compiled under 64-bit gives the following output:</p> <pre><code>021715402012 = 240565532 = 0x050E56BD1C 021815402012 = 340565532 = 0x05144C9E1C </code></pre> <p>Compiled under 32-bit, it gives the following output:</p> <pre><code>021715402012 = 2147483647 = 0x007FFFFFFF 021815402012 = 2147483647 = 0x007FFFFFFF </code></pre> <p>If this was what happened in your shell, then the resulting behaviour of <code>-lt</code> would be explained. You could confirm it by testing whether the two values are <code>-eq</code>; counter-intuitively, this would probably evaluate to true under the hypothesis that you are using a 32-bit shell that limits its arithmetic to <code>long</code> (32-bit) signed integers.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(void) { char *array[] = { "021715402012", "021815402012" }; for (int i = 0; i &lt; 2; i++) { int j = atoi(array[i]); long k = strtol(array[i], 0, 10); printf("%-10s = %10d = 0x%.10lX\n", array[i], j, k); } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload