我是靠谱客的博主 幸福小蝴蝶,这篇文章主要介绍十进制转二进制定点小数MATLAB代码 十进制数和有符号二进制定点小数转换MATLAB代码 ,现在分享给大家,希望可以做个参考。

十进制数和有符号二进制定点小数转换MATLAB代码

我是在做一个FPGA的比赛时写的该MATLAB代码,原因是在FPGA中遇到了很多定点小数的运算,手动变换格式过于繁琐,因此通过代码解决。

一、十进制转二进制:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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

说明:输出的二进制定点小数最高位(最左边一位)为符号位。

如上图所示,直接输入十进制数即可,输出结果为一串连续的0和1,圆圈处为最高位符号位,因为我定的是四位整数位,所以终端方框处为整数位,所以总二进制位数为1+4+14=19位。

二、二进制转十进制

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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

说明:因为MATLAB取模函数mod有位数限制,所以输入的二进制位数也有限制,

如上图所示,输入时直接输入一串二进制0和1,但是注意输入正数和负数的位数限制。

最后

以上就是幸福小蝴蝶最近收集整理的关于十进制转二进制定点小数MATLAB代码 十进制数和有符号二进制定点小数转换MATLAB代码 的全部内容,更多相关十进制转二进制定点小数MATLAB代码 内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部