converting a decimal number to its binary.
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 15 Oct 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:
- redefine what it means to display the binary version of the number in such a way that your current approach works; or
- 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.
Réponses (4)
Walter Roberson
le 14 Oct 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?
0 commentaires
Guillaume
le 14 Oct 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 commentaires
Walter Roberson
le 14 Oct 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.
Stephen23
le 18 Oct 2018
Modifié(e) : Stephen23
le 18 Oct 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 commentaire
Guillaume
le 18 Oct 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.
H
le 21 Oct 2018
Modifié(e) : Walter Roberson
le 21 Oct 2018
5 commentaires
Walter Roberson
le 21 Oct 2018
This is still not related to converting decimal to binary and should have been a separate Question.
Walter Roberson
le 21 Oct 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.
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!