Repository navigation
Expand file tree
/
Copy pathrepresentation.cpp
More file actions
81 lines (63 loc) · 2.82 KB
/
Copy pathrepresentation.cpp
File metadata and controls
81 lines (63 loc) · 2.82 KB
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
#include <iostream>
#include <bitset>
#include <limits>
#include <cmath>
#include <iomanip>
using namespace std;
void printBinary(int n) {
// Выводим 8 младших бит для наглядности
cout << bitset<8>(n) << " (binary) = " << n << " (dec)" << endl;
}
int main() {
cout << "=== Приложение: Представление данных ===\n\n";
// ============================================================
// 1. ЦЕЛЫЕ ЧИСЛА (ДОПОЛНИТЕЛЬНЫЙ КОД)
// ============================================================
cout << "1. Целые числа (8 бит)\n";
cout << "----------------------\n";
int pos = 5;
cout << "Положительное 5: ";
printBinary(pos);
// Как получить -5? Инверсия + 1
int neg = ~pos + 1;
cout << "Отрицательное -5: ";
printBinary(neg);
cout << "\n(Обратите внимание: старший бит = 1 означает минус)\n\n";
// ============================================================
// 2. ПЕРЕПОЛНЕНИЕ
// ============================================================
cout << "2. Переполнение\n";
cout << "---------------\n";
int maxInt = numeric_limits<int>::max();
cout << "INT_MAX = " << maxInt << endl;
cout << "INT_MAX + 1 = " << maxInt + 1 << " (Undefined Behavior!)\n";
unsigned int maxUint = numeric_limits<unsigned int>::max();
cout << "\nUINT_MAX = " << maxUint << endl;
cout << "UINT_MAX + 1 = " << maxUint + 1 << " (заворачивается в 0)\n\n";
// ============================================================
// 3. ЧИСЛА С ПЛАВАЮЩЕЙ ТОЧКОЙ
// ============================================================
cout << "3. Плавающая точка (IEEE 754)\n";
cout << "-----------------------------\n";
double a = 0.1;
double b = 0.2;
double c = a + b;
cout << fixed << setprecision(20);
cout << "0.1 = " << a << endl;
cout << "0.2 = " << b << endl;
cout << "0.1 + 0.2 = " << c << endl;
cout << "0.3 = " << 0.3 << endl;
cout << "\n0.1 + 0.2 == 0.3? " << (c == 0.3 ? "true" : "false") << endl;
cout << "Разница: " << fabs(c - 0.3) << "\n\n";
// ============================================================
// 4. СПЕЦИАЛЬНЫЕ ЗНАЧЕНИЯ
// ============================================================
cout << "4. Специальные значения\n";
cout << "-----------------------\n";
double inf = 1.0 / 0.0;
double nan = 0.0 / 0.0;
cout << "1.0 / 0.0 = " << inf << endl;
cout << "0.0 / 0.0 = " << nan << endl;
cout << "nan == nan? " << (nan == nan ? "true" : "false") << "\n";
return 0;
}