Suppose you have a single precision number a = randn('single'), then I increase its precision into double by b = double(a). In other programming languages, it will do the following three things regarding to the binary representation of a,
- Sign Bit: Remains the same.
- Exponent Bits: Extended to fill the larger space. In single precision, 8 bits are used for the exponent, while in double precision, 11 bits are used. The value of the exponent is adjusted accordingly to maintain the numerical value of the original number.
- Mantissa Bits: The mantissa is widened by adding additional bits with zeros.
Whereas, let us do an example in MATLAB
rng(1); a = randn('single'); b = double(a); format long; disp(a); disp(b);
This will gives
-0.6490138
-0.649013757705688
Now, let us use Julia (I didn't find out an easy way to convert from decimal to binary in MATLAB) to convert these numbers into their corresponding binary representations by
a = convert(Float32,-0.6490138);
b = convert(Float64,-0.649013757705688);
c = bitstring(a); println(a); println(c);
d = bitstring(b); println(b); println(d);
Then the result is
-0.6490138
10111111001001100010010111000101
-0.649013757705688
0100110001001011100001111111111111111111111111111100
Clearly, it does not simply adding zeros at the end of its binary representation.
Now, I wonder what MATLAB do exactly when dealting with such situation? It clearly not randomly inserting numbers since if I change the random number generator's seed, the extended number remain the same.