-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatingbits.cpp
More file actions
154 lines (121 loc) · 3.13 KB
/
floatingbits.cpp
File metadata and controls
154 lines (121 loc) · 3.13 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
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
/**
* Copyright Johannes Kloimböck 2021 - 2022.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* https://www.boost.org/LICENSE_1_0.txt)
*/
#include <iostream>
#include <cmath>
#include <cinttypes>
#include <string>
inline int fb_abort() {
std::cout << "Bad input!" << std::endl;
std::cout << "Aborting!" << std::endl;
system("PAUSE");
return EXIT_SUCCESS;
}
inline std::size_t precision(std::uint64_t ui) noexcept {
std::size_t c = 0;
for (; c < 64; ++c) {
if ((1ULL << (63 - c)) & ui) {
return (64 - c);
}
}
return 0;
}
inline std::string binaryString(std::uint64_t ui) {
std::size_t s = precision(ui);
if (ui == 0ULL) {
return "0";
}
std::string str{};
for (std::size_t i = 0; i < s; ++i) {
str += ((1ULL << (s - i - 1)) & ui) ? "1" : "0";
}
return str;
}
union float_i64 {
double f;
std::uint64_t i;
};
union float_i32 {
float f;
std::uint32_t i;
};
int main() {
unsigned int choice = 0;
std::cout << "Int to Float.....1" << std::endl;
std::cout << "Float to Int.....2" << std::endl;
std::cin >> choice;
if (choice == 1) {
std::cout << "\t32-bit......1" << std::endl;
std::cout << "\t64-bit......2" << std::endl;
std::cin >> choice;
if (choice == 1) {
int base = 10;
std::cout << "Enter the integer number: ";
std::string s;
std::cin >> s;
std::cout << "\nEnter the radix: " << std::endl;
std::cin >> base;
float_i32 fi;
fi.i = (std::uint32_t)(std::strtoul(s.c_str(), nullptr, base));
std::cout << std::endl;
std::printf("%.7g\n", fi.f);
}
else if (choice == 2) {
int base = 10;
std::cout << "Enter the integer number: ";
std::string s;
std::cin >> s;
std::cout << "\nEnter the radix: ";
std::cin >> base;
float_i64 fi;
fi.i = (std::uint64_t)(std::strtoull(s.c_str(), nullptr, base));
std::cout << "\n" << std::endl;
std::printf("%.16g\n", fi.f);
}
else {
return fb_abort();
}
}
else if (choice == 2) {
std::cout << "\t32-bit......1" << std::endl;
std::cout << "\t64-bit......2" << std::endl;
std::cin >> choice;
if (choice == 1) {
std::string s;
std::cout << "Enter the value of the floating-point number: ";
std::cin >> s;
float f = strtof(s.c_str(), nullptr);
float_i32 fi;
fi.f = f;
std::cout << "\n" << std::endl;
std::printf("Bin: %s\n", binaryString(fi.i).c_str());
std::printf("Oct: %lo\n", fi.i);
std::printf("Dec: %lu\n", fi.i);
std::printf("Hex: %lx\n", fi.i);
}
else if (choice == 2) {
std::string s;
std::cout << "Enter the value of the floating-point number: ";
std::cin >> s;
double f = strtod(s.c_str(), nullptr);
float_i64 fi;
fi.f = f;
std::cout << "\n" << std::endl;
std::printf("Bin: %s\n", binaryString(fi.i).c_str());
std::printf("Oct: %llo\n", fi.i);
std::printf("Dec: %llu\n", fi.i);
std::printf("Hex: %llx\n", fi.i);
}
else {
return fb_abort();
}
}
else {
return fb_abort();
}
system("PAUSE");
return EXIT_SUCCESS;
}