title: Difference between two large numbers C#
body: <p>There are already solutions to this problem for <strong>small numbers</strong>:</p>
  
  <ul>
  <li>Here: <a href="https://stackoverflow.com/questions/339961/difference-between-2-numbers">Difference between 2 numbers</a></li>
  <li>Here: <a href="https://stackoverflow.com/questions/1111194/c-sharp-function-to-find-the-delta-of-two-numbers">C# function to find the delta of two numbers</a></li>
  <li>Here: <a href="https://stackoverflow.com/questions/916332/how-can-i-find-the-difference-between-2-values-in-c">How can I find the difference between 2 values in C#?</a></li>
  </ul>
  
  <p>I'll summarise the answer to them all:</p>
  
  <pre><code>Math.Abs(a - b)
  </code></pre>
  
  <p>The problem is <strong>when the numbers are large</strong> this gives the wrong answer (by means of an overflow). Worse still, if <code>(a - b) = Int32.MinValue</code> then <code>Math.Abs</code> crashes with an exception (because <code>Int32.MaxValue = Int32.MinValue - 1</code>):</p>
  
  <blockquote>
    <p>System.OverflowException occurred<br>
    HResult=0x80131516<br>
    Message=<strong>Negating the minimum value of a twos complement number is</strong>
    <strong>invalid</strong>.<br>
    Source=mscorlib<br>
    StackTrace:    at
    System.Math.AbsHelper(Int32 value)    at System.Math.Abs(Int32 value)</p>
  </blockquote>
  
  <p>Its specific nature leads to difficult-to-reproduce bugs.</p>
  
  <p>Maybe I'm missing some well known library function, but is there any way of determining the difference safely?</p>