Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answers so far have pointed out most of the problems with your approach, namely explicit value casting, structure alignment, and array of structs vs. int array. I'll add one more point:</p> <p><code>int and float types are both of size 4 bytes, I have checked that...</code></p> <p>Int and float are represented in a completely different way in-memory.</p> <p>Integer values are represented in what is known as <a href="http://en.wikipedia.org/wiki/Two%27s_complement" rel="nofollow">Two's complement</a>, which allows integers to be added without extra operations and gives a single representation of the number <code>0</code>:</p> <ul> <li>Positive integers are simply converted to base 2. For example, <code>151</code> is represented as <code>00000000 00000000 00000000 10010111(2)</code> = <code>2^7 + 2^4 + 2^2 + 2^1 + 2^0</code>.</li> <li>Negative integers are represented as the binary representation of their absolute values, which then has all its bits inverted and 1 added to the resultant number. For example, <code>-151</code> is represented as <code>11111111 11111111 11111111 01101001(2)</code> (Compare with the positive number).</li> </ul> <p>Single-precision floating points, on the other hand, are represented as what is termed <a href="http://en.wikipedia.org/wiki/Single_precision_floating-point_format#IEEE_754_single-precision_binary_floating-point_format%3a_binary32" rel="nofollow">binary32</a> in the IEEE 754-2008 standard:</p> <ul> <li>The first bit is the sign bit: 0 for positive and 1 for negative.</li> <li>The next 8 bits are the exponent, in 2's complement. It is <strong>biased</strong>: subtract 127 from this value to get the actual exponent.</li> <li>The next 23 bits represent the fractional part of the normalized (having only 1 digit before the decimal point) fraction.</li> </ul> <p>So <code>151.0</code> is represented as <code>01000011 00010111 00000000 00000000</code>, having an exponent of <code>134</code> (which is <code>7</code> unbiased) and a mantissa of <code>0.1796875</code> (which is <code>1.1796875</code> because the integral part is implicit).</p> <p>This is why, if you cast a <code>float *</code> to an <code>int *</code>, which is equivalent to what you have done, you get garbage values when you dereference it later. <strong>H2CO3</strong> has pointed out the proper conversion method through the assignment operator.</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.
    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