problem with adding two numbers

13 ビュー (過去 30 日間)
shivam pranjale
shivam pranjale 2018 年 8 月 28 日
コメント済み: Wycliff Dembe 2018 年 8 月 28 日
I am trying to convert fractional decimal number into binary. having done conversion with integer and fractional part I want to finally add them and show the result. I am getting following result. Please guide me what is wrong with the code?
clear all
clc
N = input('Enter the decimal number - ');
%B = input('Enter the base - ');
int = floor(N); %integer part
V = N - int; %decimal part
i = 1;
while(int > 0)
R(i) = rem(int,2);
int = floor(int/2);
i = i+1;
end
b_n = fliplr(R);
disp('Binary equivalent of number is ');
disp(b_n);
x = 0;
k = 0.1;
while(V ~= 0)
V = (V-floor(V))*2;
x = x + floor(x + V)*k;
k = k/10;
end
f_n = V + x;
disp(f_n);
C = b_n + f_n;
disp(C)
output
Enter the decimal number - 100.25
Binary equivalent of number is
1 1 0 0 1 0 0
0.0100
Columns 1 through 6
1.0100 1.0100 0.0100 0.0100 1.0100 0.0100
Column 7
0.0100
  1 件のコメント
Wycliff Dembe
Wycliff Dembe 2018 年 8 月 28 日
Check this FileExhange by Gordon Amoako perhaps might help

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by