0 / 0 / 0
Регистрация: 24.12.2017
Сообщений: 80
|
|
1
|
Ошибочное значение экспоненты
04.11.2018, 17:07. Показов 703. Ответов 1
Народ, наткнулся на проблему программа нее правильно высчитывает экспоненту, не могу понять почему
C++ | 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
| // l1.cpp: определяет точку входа для консольного приложения.
//long float массив char[3][3]
#include "stdafx.h"
#include <iostream>
using namespace std;
void BinOut(long *buff_ptr, int sizebuff) { //Переводим лонг из 2 в 10
const int size = sizebuff * 8; //Находим размер типа в битах
int *bin = new int[size]; //создаем массив равный количеству бит типа
for (int i = 0; i < size; i++)
{
bin[i] = *buff_ptr & 1;
*buff_ptr /= 2;
}
cout << "Ответ: ";
for (int i = 31; i >= 0; i--) {
cout << bin[i];
}
cout << endl;
}
int arr[8]; //заменить на 8
int mantisa[23];
//0100000000010000000000000000000000000000000000000010011111110001
void BinFloatOut(float *buff_ptr, int sizebuff) {
const int bf_size = 32;
if (*buff_ptr < 0) {
arr[0] = 1;
*buff_ptr = -(*buff_ptr);
}
else arr[0] = 0;
int exponent = 0;
while (*buff_ptr < 1)
{
*buff_ptr *= 10;
cout << *buff_ptr << endl; //Находение экспоненты если число <1
exponent--;
}
while (*buff_ptr >= 10)
{
*buff_ptr /= 10;
cout << *buff_ptr << endl; // Находение экспоненты если число > 10
exponent++;
}
cout << "Экспонента: " << exponent << endl;
int buff[bf_size];
for (int i = 0; i < bf_size; i++) {
buff[i] = 0;
}
exponent += 127;
cout << "Смещенная экспонента: " << exponent << endl;
for (int i = 1; i < 8; i++)
{
buff[i] = exponent & 1;
exponent /= 2; //Нахождение експоненты в двоичном виде
cout << buff[i];
}
cout << endl;
for (int i = 0; i < 7; i++)
{
arr[i + 1] = buff[7 - i];
}
const int m = 24;
int x = *buff_ptr;
while (*buff_ptr - x > 0)
{
*buff_ptr *= 10;
x = *buff_ptr;
}
cout << "Мантиса: " << x << endl;
for (int i = 0; i < 23; i++) {
mantisa[i] = x & 1;
x /= 2;
}
cout << "Ответ: ";
for (int i = 0; i < 8; i++) {
cout << arr[i];
}
for (int i = 22; i >= 0; i--) {
cout << mantisa[i];
}
int res[32];
cout << endl;
}
void BinIn(long *buff_ptr, int sizebuff) {
int step = 0;
int res = 0;
while (*buff_ptr > 0) {
res += (*buff_ptr % 10) * pow(2, step);
step++;
*buff_ptr /= 10;
cout << "Step =" << step << endl;
cout << "buff_ptr =" << *buff_ptr << endl;
}
cout << "Ответ: " << res << endl;
}
void BinFloatIn(float *buff_ptr, int size_buff) {
int exponent = 0;
int step = 0;
for (int i = 7; i > 0; i--) {
exponent += arr[i] * pow(2, step);
step++;
}
exponent -= 127;
float mant = 0;
cout << "Экспонента: " << exponent << endl;
step = 0;
for (int i = 0; i < 23; i++) {
mant += mantisa[i] * pow(2, step);
step++;
}
cout << "Мантиса: " << mant << endl;
while (mant > 10) {
mant /= 10;
}
while (mant < 1) {
mant *= 10;
}
cout << "Нормализованная мантиса: " << mant << endl;
while (exponent > 0) {
mant *= 10;
exponent--;
}
while (exponent < 0) {
mant /= 10;
exponent++;
}
cout << "Ответ: " << mant << endl;
}
int main()
{
setlocale(LC_ALL, "russian");
long y = 8;
cout << "Лонг в двоичный формат " << y << endl;
BinOut(&y, sizeof(y));
cout << endl << endl;
long z = 101;
cout << "Лонг из двоичного формата " << z << endl;
BinIn(&z, sizeof(z));
cout << endl << endl;
float x = 15.625;
cout << "Флоат в двоичный формат " << x << endl;
BinFloatOut(&x, sizeof(x));
cout << endl << endl;
cout << "Флоат из двоичного формата ";
for (int i = 0; i < 12; i++) cout << arr[i];
for (int i = 52; i >= 0; i--) cout << mantisa[i];
cout << endl;
BinFloatIn(&x, sizeof(x));
cout << endl << endl;
system("pause");
return 0;
} |
|
0
|