title: Cast double to int64_t and back without info lost
body: <p>This question has probably being asked, but I searched and could not find the answer.</p>
  
  <p>I'm implementing a toy virtual machine, where the OpCodes take the form:</p>
  
  <pre><code>std::tuple&lt;int8_t, int64_t, int64_t&gt; // instruction op1, op2
  </code></pre>
  
  <p>I'm trying to pack a double into one of the operands and read it back again when processing it. This doesn't work reliably.</p>
  
  <pre><code>double d = ...
  auto a = static_cast&lt;int64_t&gt;(d);
  auto b = static_cast&lt;double&gt;(a)
  // sometimes, b != d
  </code></pre>
  
  <p>Is there a way to pack the bit representation of the double into an int64_t, and then read that bit pattern back get the same exact double as before? </p>