title: Is the behaviour of casting a negative double to unsigned int defined in the C standard? Different behaviour on ARM vs. x86
body: <p>I have code that runs on different platforms that seems to get different results. I am looking for a proper explanation.</p>
  
  <p>I expected casting to <code>unsigned</code> to work the same for <code>float</code> or <code>double</code> as for <code>int</code><sup>1</sup>.</p>
  
  <p><strong>Windows :</strong></p>
  
  <pre><code>double dbl = -123.45; 
  int d_cast = (unsigned int)dbl; 
  // d_cast == -123
  </code></pre>
  
  <p><strong>WinCE (ARM):</strong></p>
  
  <pre><code>double dbl = -123.45; 
  int d_cast = (unsigned int)dbl; 
  // d_cast == 0
  </code></pre>
  
  <p><strong><em>EDIT:</em></strong></p>
  
  <p>Thanks for pointing in the right direction. </p>
  
  <p><strong><s>fix</s> workaround</strong> </p>
  
  <pre><code>double dbl = -123.45; 
  int d_cast = (unsigned)(int)dbl; 
  // d_cast == -123
  // works on both. 
  </code></pre>
  
  <hr>
  
  <p>Footnote 1:  Editor's note: converting an out-of-range <code>unsigned</code> value to a signed type like <code>int</code> is <em>implementation defined</em> (not undefined). C17 § 6.3.1.3 - 3.</p>
  
  <p>So the assignment to <code>d_cast</code> is also not nailed down by the standard for cases where <code>(unsigned)dbl</code> ends up being a huge positive value on some particular implementation.  (That path of execution contains UB so ISO C is already out the window in theory).  In practice compilers do what we expect on normal 2's complement machines and leave the bit-pattern unchanged.</p>