フィルターのクリア

converting a decimal number to its binary.

4 ビュー (過去 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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by