How to handle overflow on long
<p>I am working with an API that provides some data in a variable of type long. The value of this variable is increasing and will rollover multiple times, unfortunately there is no way to modify the API. I'm attempting to detect and compensate for the overflow using code similar to the test harness I've included below, ultimately I'm going to cast the data to an unsigned long long and then to a double:</p>

<pre><code>void DetectRolloverOneNumber(long val)
{
static unsigned __int64 prevVal = 0;
//store value in an unsigned long
unsigned long newVal = val;

//we will eventually store in a long long
unsigned __int64 uiBigVal = newVal;

//max value for a 32 bit long
unsigned __int64 maxInt = 0xFFFFFFFF;
bool rollover = false;

//we will be adding max long + 1
maxInt++;

//detect the rollover
unsigned __int64 modVal = prevVal% maxInt;

if (newVal &lt; modVal)
{
    //account for the rollover
    uiBigVal += maxInt;
    rollover = true;
}

cout&lt;&lt; val &lt;&lt; "\t" &lt;&lt; newVal &lt;&lt; "\t" &lt;&lt; modVal &lt;&lt; "\t" &lt;&lt;
        uiBigVal &lt;&lt; "\t" &lt;&lt; prevVal &lt;&lt; "\t" &lt;&lt; rollover &lt;&lt; "\n";

//cache the value so we can check for rollover next time
prevVal = uiBigVal;
</code></pre>

<p>So this works ok but my long value is rolling over multiple times and I am not handling that properly.
To test this code I call it with the following</p>

<pre><code>first =2147483647;
for (int i = 0; i &lt; 100000; i ++)
{

    DetectRolloverOneNumber(first);
    first += 1000000;
</code></pre>

<p>Here is my output</p>

<pre><code>   val (long)       newVal (ulong) modVal           uiBigVal       prevVal    
   2147483647      2147483647      0               2147483647      0       
   -2146483649     2148483647      2147483647      2148483647      2147483647
   -2145483649     2149483647      2148483647      2149483647      2148483647
   -2144483649     2150483647      2149483647      2150483647      2149483647
   -2143483649     2151483647      2150483647      2151483647      2150483647
   -2142483649     2152483647      2151483647      2152483647      2151483647
</code></pre>

<p>so far so good, values increasing by 100000 as expected even though we passed the max value for a signed 32 bit long.</p>

<p>..further on down...</p>

<pre><code>   -2483649        4292483647      4291483647      4292483647      4291483647
   -1483649        4293483647      4292483647      4293483647      4292483647
   -483649         4294483647      4293483647      4294483647      4293483647      
   516351          516351          4294483647      4295483647      4294483647   (rollover detected here and we are still good)
   1516351         1516351         516351          1516351         4295483647      
   2516351         2516351         1516351         2516351         1516351 //oops!!!
   3516351         3516351         2516351         3516351         2516351 
</code></pre>

<p>so I'm getting this wrong. Anybody have any tips on how to properly deal with the rollover? 
Any advice would be appreciated. Thanks!</p>
