converting a decimal number to its binary.

조회 수: 7 (최근 30일)
H
H 2018년 10월 8일
댓글: Walter Roberson 2018년 10월 21일
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
Walter Roberson
Walter Roberson 2018년 10월 15일
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.
H
H 2018년 10월 16일
ok, thanks

댓글을 달려면 로그인하십시오.

답변 (4개)

Walter Roberson
Walter Roberson 2018년 10월 14일
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 2018년 10월 14일
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
Walter Roberson
Walter Roberson 2018년 10월 14일
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 2018년 10월 15일
num2hex might get you there faster.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2018년 10월 18일
편집: Stephen23 2018년 10월 18일
>> 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
Guillaume
Guillaume 2018년 10월 18일
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
H 2018년 10월 21일
편집: Walter Roberson 2018년 10월 21일
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
Walter Roberson
Walter Roberson 2018년 10월 21일
This is still not related to converting decimal to binary and should have been a separate Question.
Walter Roberson
Walter Roberson 2018년 10월 21일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by