Decimal number and signed binary fixed-point decimal conversion MATLAB code
I wrote this MATLAB code when I was doing an FPGA competition. The reason is that I encountered a lot of fixed-point decimal calculations in the FPGA, and manual format conversion is too cumbersome, so I solved it through code.
1. Convert decimal to binary:
clc;
clear;
integer = 4; %整数位数
decimal = 14; %小数位数
sum_before_point = 0;
sum_after_point = 0;
sum = 0;
before_point_arr = zeros(1, integer + 1);
after_point_arr = zeros(1, decimal);
while (1)
prompt = 'What is the deciaml value? ';
num = input(prompt);
lenrth_data = length(num);
%------------------------------%
if (num < 0)
before_point_arr(integer + 1) = 1;
num = abs(num);
flag = 1;
else
before_point_arr(integer + 1) = 0;
flag = 0;
end
%------------------------------%
before_point = fix(num);
for i = 1:integer
before_point_arr(i) = mod(before_point, 2);
before_point = fix(before_point / 2);
if (before_point <= 1)
before_point_arr(i + 1) = before_point;
break;
end
end
%------------------------------%
after_point = num - fix(num);
for j = 1:decimal
after_point_arr(j) = fix(after_point * 2);
after_point = after_point * 2;
if (after_point >= 1)
after_point = after_point - 1;
end
end
%------------------------------%
if (flag == 1)
for i = 1:integer
switch (before_point_arr(i))
case 0
before_point_arr(i) = 1;
case 1
before_point_arr(i) = 0;
end
end
for k = 1:integer
before_point_arr(k) = before_point_arr(k) + 1;
if (before_point_arr(k) == 1)
break;
else if(before_point_arr(k) == 2)
before_point_arr(k) = 0;
end
end
end
for j = 1:decimal
switch (after_point_arr(j))
case 0
after_point_arr(j) = 1;
case 1
after_point_arr(j) = 0;
end
end
end
%------------------------------%
if (flag == 1)
fprintf('%d', 1);
else if(flag == 0)
fprintf('%d', 0);
end
end
for i = 1:integer
fprintf('%d', before_point_arr(integer + 1 - i));
end
for j = 1:decimal
fprintf('%d', after_point_arr(j));
end
fprintf('\n');a
%------------------------------%
before_point_arr = 0;
after_point_arr = 0;
flag = 0;
end
Explanation: The highest bit (the leftmost bit) of the output binary fixed-point decimal is the sign bit.
As shown in the figure above, just input the decimal number directly, and the output result is a series of consecutive 0 and 1, and the circle is the highest bit sign bit, because I set four integer bits, so the terminal box is an integer bit, So the total number of binary digits is 1+4+14=19 bits.
2. Convert binary to decimal
clc;
clear;
format long g;
%正数原码位数限制integer + decimal < 23,负数补码位数限制integer + decimal < 16
integer = 1; %整数位数
decimal = 21; %小数位数
sum_before_point = 0;
sum_after_point = 0;
sum = 0;
while (1)
prompt = 'What is the binary value? ';
num = input(prompt);
lenrth_data = length(num);
before_point_arr = zeros(1, integer + 1);
after_point_arr = zeros(1, decimal);
%------------------------------%
tempt = num;
z = 10.^decimal;
tempt = tempt / z;
for i = 1:integer + 1
before_point_arr(i) = fix(mod(tempt, 10));
tempt = tempt / 10;
end
if (before_point_arr(integer + 1) == 1)
flag = 1;
else
flag = 0;
end
%------------------------------%
if(mod(decimal, 2))
odd = 1;
else
odd = 0;
end
if(odd)
x = (decimal - 1) / 2;
y = (decimal + 1) / 2;
z = 10.^x;
tempt = fix(mod(num, z));
for j = 1:x
after_point_arr(j) = fix(mod(tempt, 10));
tempt = tempt / 10;
end
tempt = num / z;
for j = y:decimal
after_point_arr(j) = fix(mod(tempt, 10));
tempt = tempt / 10;
end
else
x = decimal / 2;
y = x + 1;
z = 10.^x;
tempt = mod(fix(num), z);
for j = 1:x
after_point_arr(j) = mod(fix(tempt), 10);
tempt = tempt / 10;
end
tempt = num / z;
for j = y:decimal
after_point_arr(j) = mod(fix(tempt), 10);
tempt = tempt / 10;
end
end
%------------------------------%
if (flag == 1)
for i = 1:integer
switch (after_point_arr(i))
case 0
before_point_arr(i) = 1;
case 1
before_point_arr(i) = 0;
end
end
for k = 1:integer
before_point_arr(k) = before_point_arr(k) + 1;
if (before_point_arr(k) == 1)
break;
else if (before_point_arr(k) == 2)
before_point_arr(k) = 0;
end
end
end
for j = 1:decimal
switch (after_point_arr(j))
case 0
after_point_arr(j) = 1;
case 1
after_point_arr(j) = 0;
end
end
end
%------------------------------%
for i = 1:integer
sum_before_point = sum_before_point + before_point_arr(i) * 2.^(i - 1);
end
for j = 1:decimal
sum_after_point = sum_after_point + after_point_arr(j) * 2.^(j - decimal - 1);
end
sum = sum_before_point + sum_after_point;
if (flag == 1)
sum = -sum;
end
%------------------------------%
disp(sum);
flag = 0;
sum = 0;
sum_before_point = 0;
sum_after_point = 0;
before_point_arr = 0;
after_point_arr = 0;
end
Explanation: Because the MATLAB modulo function mod
has a limit on the number of digits, the number of binary digits input is also limited.
As shown in the figure above, directly input a string of binary 0 and 1 when inputting, but pay attention to the limit on the number of digits of positive and negative numbers.