Azzera filtri
Azzera filtri

converting a decimal number to its binary.

4 visualizzazioni (ultimi 30 giorni)
H
H il 8 Ott 2018
Commentato: Walter Roberson il 21 Ott 2018
Hi, I wrote a code which converts a decimal number to its binary. However, when I convert to binary it doesn’t show 52 bits in the fraction. Thus, how do I get 52 bits after the radix point? Please let me know Thank you
  4 Commenti
Walter Roberson
Walter Roberson il 15 Ott 2018
You are using
num2str(x)
and breaking the displayed result up into integer and post-decimal-point parts. But num2str(x) rounds the input value instead of displaying it to full precision:
>> format long
>> num2str(753.153 + eps(753))
ans =
'753.153'
>> 753.153 + eps(753)
ans =
7.531530000000001e+02
Now, in IEEE 754, when you use up bits for the integer, fewer bits are available for the fraction, so the 10 bits used for 753 reduce the available fraction to about 42 bits. But when you break apart the .153, you do that as if it was a complete number by itself.
The integer part is not represented separately, though. 753.153 is represented as 512 times (1 plus a fraction)
So... you have two choices:
  1. redefine what it means to display the binary version of the number in such a way that your current approach works; or
  2. continue to aim for compatibility with IEEE 754 by completely reworking the way you calculate the bits (that is, divide by 2 until you get a number between 1 (inclusive) and 2 (exclusive) and then keep multiplying by 2 to figure out what bits are present.

Accedi per commentare.

Risposte (4)

Walter Roberson
Walter Roberson il 14 Ott 2018
if(integer=='0')
should be strcmp(integer, '0')
On the other hand, if it is that, then there is not going to be any difference between taking '0.' followed by str2, compared to taking strcat(int,'.',str2) so that entire "if" seems to be unneeded: you can just always do the "else" part.
"convert 753.153 into binary it doesn’t show 52 bits in the fraction as it should since I am using double precision"
You are not outputting a fraction. You are taking the vector of 0 and 1 and '.' values, and you are doing str2double() on it and returning that.
Consider for example if the input was decimal 5. You would find that the binary for that was '101' . You would str2double() that, which asks for the decimal number 101 to be evaluated . 101 decimal is between 64 and 128 so it would take a minimum of 7 bits binary to represent -- binary 1100101 .
Therefore, when when you use str2double() on the binary, you need more bits than you did originally.
Why not just output the character vectors?

Guillaume
Guillaume il 14 Ott 2018
The easy way to find the binary representation of a double number in matlab:
number = 753.153; %for example
dec2bin(typecast(number, 'uint64'), 64)
Now I realise this is probably homework and you're supposed to come up with your own conversion algorithm so the above is probably not acceptable
  4 Commenti
Walter Roberson
Walter Roberson il 14 Ott 2018
Could just be down to the fact that the source number will have 52 bits stored (plus one hidden bit). That is, it might be acknowledging that the source is IEEE 754, but still wanting binary integer period binary fraction output.
Greg Dionne
Greg Dionne il 15 Ott 2018
num2hex might get you there faster.

Accedi per commentare.


Stephen23
Stephen23 il 18 Ott 2018
Modificato: Stephen23 il 18 Ott 2018
>> N = pi; % example number
>> S = num2hex(N)
S = 400921fb54442d18
>> X = sscanf(S,'%1x');
>> B = num2cell(dec2bin(0:15),2);
>> [B{1+X}]
ans = 0100000000001001001000011111101101010100010001000010110100011000
Could easily be written in two lines, but the intermediate steps are worth taking a look at.
  1 Commento
Guillaume
Guillaume il 18 Ott 2018
I'm not convinced that this is any faster than a simple typecast to integer followed by a dec2bin.
In my opinion it's certainly more convoluted.

Accedi per commentare.


H
H il 21 Ott 2018
Modificato: Walter Roberson il 21 Ott 2018
Please tell me where is the error in the first line of the following code:
f=(x+1)*(exp(-(x+1)));
g=(1/(sqrt(2*pi)))*exp(-(x.^2)/2);
hold on
grid on
axis([-2 2 -0.4 1])
x=[-2:0.01:2];
plot(x,f(x))
plot(x,g(x))
  5 Commenti
Walter Roberson
Walter Roberson il 21 Ott 2018
This is still not related to converting decimal to binary and should have been a separate Question.
Walter Roberson
Walter Roberson il 21 Ott 2018
f=(x+1).*(exp(-(x+1)));
creates f as a vector, not as a function.
plot(x,f(x))
tries to use f as a function, not as an array.

Accedi per commentare.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by