Note that there are some explanatory texts on larger screens.

plurals
  1. POCan batch files not process large numbers?
    primarykey
    data
    text
    <p>For the sake of my own amusement, I decided to try writing a batch file to calculate <a href="https://en.wikipedia.org/wiki/Hailstone_sequence" rel="nofollow noreferrer">Hailstone Sequences</a>. However, I ran into a small problem with some large-ish numbers.</p> <p>First, the code:</p> <pre><code>:START @ECHO OFF SETLOCAL SET /P InputVar="Input Number: " ECHO. ECHO %InputVar% SET ItCount=0 :COLLATZ SET /A ItCount=%ItCount%+1 SET /A Odd=%InputVar%%%2 IF %Odd% EQU 1 ( SET /A OutputNum=%InputVar%*3+1 ) ELSE ( SET /A OutputNum=%InputVar%/2 ) ECHO %OutputNum% IF %OutputNum% LSS 1 ( GOTO ERROR ) ELSE ( GOTO RECYCLE ) :ERROR ECHO. ECHO ERROR! GOTO END :RECYCLE IF %OutputNum% EQU 1 ( GOTO FINISH ) ELSE ( SET InputVar=%OutputNum% GOTO COLLATZ ) :FINISH ECHO. ECHO Completed in %ItCount% iterations. :END ENDLOCAL PAUSE </code></pre> <p>This works for several numbers I've tested. However, when I got around to testing a number that I new would take hundreds of iterations to complete, the system started returning <em>negative</em> outputs. Interestingly, the negative outputs eventually resolved to zero. However, this behavior of the script is not at all expected or intended.</p> <p>After adding in the error handling, this is the output I get for <code>8388607</code>.</p> <p><img src="https://i.stack.imgur.com/jYV49.jpg" alt="enter image description here"></p> <p>According to <a href="https://encrypted.google.com/search?&amp;q=725594111*3%2b1" rel="nofollow noreferrer">Google</a>, the next number should have been <code>2176782334</code>.</p> <p>Is this a natural limitation of the command processor's ability to handle large numbers? Similar operations run fine in Excel - there, I was able to determine the number should have resolved to 1 in 473 iterations.</p> <p>I'm running Windows 7 SP1 x64.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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