how to convert The binary number floating point to decimal notation, as example,(1.10101010 * 2^1001 ) is equal to 852

3 Ansichten (letzte 30 Tage)
I want to use following decimal numbers:
  1. 14144
  2. 584
to convert it into the binary floating-point representations (1.b1b2b3...bn × 2^k).

Antworten (1)

James Tursa
James Tursa am 6 Sep. 2017
Bearbeitet: James Tursa am 6 Sep. 2017
You could start with this FEX submission by Toby Driscoll:
https://www.mathworks.com/matlabcentral/fileexchange/25326-ieee-754-binary-representation?s_tid=answers_rc2-1_p4_MLT
Or an alternate approach:
% convert double number to binary string and exponent
x = some non-negative number
[F,E] = log2(x);
binary_string = ['0.' dec2bin(F*2^53)];
exponent = dec2bin(E);
% convert binary string and exponent to double number
F = sum((binary_string(3:end) - '0') ./ 2.^(1:numel(binary_string)-2));
E = bin2dec(exponent);
x = F * 2^E;
Examples:
% double number to binary
>> x = 14144
x =
14144
>> [F,E] = log2(x)
F =
0.8633
E =
14
>> binary_string = ['0.' dec2bin(F*2^53)]
binary_string =
0.11011101000000000000000000000000000000000000000000000
>> exponent = dec2bin(E)
exponent =
1110
% binary to double number
>> F = sum((binary_string(3:end) - '0') ./ 2.^(1:numel(binary_string)-2))
F =
0.8633
>> E = bin2dec(exponent)
E =
14
>> x = F * 2^E
x =
14144
And to see if this method recovers that last bit
% convert double number to binary string and exponent
>> x = 1 + eps
x =
1.0000
>> num2hex(x)
ans =
3ff0000000000001
>> [F,E] = log2(x)
F =
0.5000
E =
1
>> binary_string = ['0.' dec2bin(F*2^53)]
binary_string =
0.10000000000000000000000000000000000000000000000000001
>> exponent = dec2bin(E)
exponent =
1
% convert binary string and exponent to double number
>> F = sum((binary_string(3:end) - '0') ./ 2.^(1:numel(binary_string)-2))
F =
0.5000
>> E = bin2dec(exponent)
E =
1
>> x = F * 2^E
x =
1.0000
>> num2hex(x)
ans =
3ff0000000000001
This approach assumes binary_string contains all of the bits (i.e., there is no leading hidden bit).

Kategorien

Mehr zu Encryption / Cryptography finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by