problem with adding two numbers

조회 수: 12 (최근 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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by