Write a user-defined MATLAB function that converts real numbers in decimal form
ID: 3880762 • Letter: W
Question
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name the function b = deci TObina (d), where the input argument dis the number to be converted and the output argument b is a 30-element-long vector with 1s and 0s that represents the number in binary form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of b store the digits to the right of the decimal point. If more than 15 positions are required in the binary form for the digits to the right of the decimal point, the digits should be chopped. If the number that is entered as d is larger than can be stored in b, the function should display an error message. Use the deciTObina in the Command Window to convert the numbers 85.321, 0.00671, and 3006.42.
Explanation / Answer
Answer:
code:
function binary_dec = deciToBina(d);
if d>1
[f, e] = log2 (max(d));
binary = char(rem(floor(d*pow2(1-max(2, e):0)), 2)+ '0')
else
N = d;
for i = 1:10
D1 = N*2;
if D1<1
binary1(i) = 0;
N = D1;
else
binary1(i) = 1;
N = D1-1;
end
if N==0
break;
end
end
binary_dec = binary1
end
Please provide your valuable feedback.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.