IEEE 754

So I'm currently working on a project where I'm supposed to convert numbers into IEEE 754 single precision. So far, I'm stuck. I'm trying to figure out how to obtain the exponent in base-10 and binary format. This is what I have so far...


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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

//Function Prototypes
void signbit(double number, string &x); // checks sign of number; positive (0) or negative (1)
void zoro(double number, int &whole, double & fraction); // separates the integer value from the decimal value (ex. -10.25)
string wholeconverter(int whole); // converts the integer value to binary (ex. 10 = 1010)
string fractionconverter(double number); // converts the decimal value to binary (ex. 0.25 = 0100 0000 0000 0000 0000 0000)
string numberconverter(string wholebinary, string fracbin); // converts number entered into binary (ex. 10.25 = 00001010.0100000000000....)
string mantissaconverter(string wholebinary, string fracbin); //displays mantissa of converted number (ex. 01001000 00000000 .....)
string exponentconverter(string fullbinary); //converts integer of entered number into binary (ex. 130 = 10000010)
void fullconverter(string fracbin); //displays entered number in IEEE 754 single precision binary (ex. -10.25 = 01000001 00100100 00000000 00000000)
void hexconverter(double number); //displays entered number into IEEE 754 single precision HEX (ex. -10.25 = C1240000)

//C++ entry point
int main()
{
	//Declaration
	string x; // Sign
	string wholebinary, fracbin, fullbinary, mantissa;
	int whole, exponent;
	double number, fraction, binary;
	char choice, hex;

	//Initialization
	fraction = 0.00;
	number = 0.00;
	exponent = 0;
	whole = 0;
	binary = 0.00;
	hex = 0;
	choice = 0;
	x = "";
	fracbin = "";
	wholebinary = "";
	fullbinary = "";
	mantissa = "";

	signbit(number, x);
	zoro(number, whole, fraction);
	wholebinary = wholeconverter(whole);
	fracbin = fractionconverter(fraction);
	fullbinary = numberconverter(wholebinary, fracbin);
	mantissa = mantissaconverter(wholebinary, fracbin);



	// Menu Display
	cout << "PLEASE ENTER A NUMBER TO DISPLAY THE IEEE 754 FLOATING POINT OPTIONS." << endl;
	cin >> number;
	cout << "                                                                                          " << endl;

	cout << "PLEASE CHOOSE ONE OF THE FOLLOWING OPERATIONS: " << endl;
	cout << "1. DISPLAY THE SIGN BIT VALUE" << endl;
	cout << "2. DISPLAY THE INTEGER PART IN BOTH BASE-10 AND BINARY FORMATS" << endl;
	cout << "3. DISPLAY THE DECIMAL PART IN BOTH BASE-10 AND BINARY FORMATS" << endl;
	cout << "4. DISPLAY THE NUMBER ENTERED IN BOTH BASE-10 AND BINARY FORMATS" << endl;
	cout << "5. DISPLAY THE MANTISSA IN BINARY FORMAT" << endl;
	cout << "6. DISPLAY THE EXPONENT IN BOTH BASE-10 AND BINARY FORMAT" << endl;
	cout << "7. DISPLAY THE IEEE 754 SINGLE PRECISION BINARY FORMAT" << endl;
	cout << "8. DISPLAY THE IEEE 754 SINGLE PRECISION HEX FORMAT" << endl;
	cout << "                                                                                          " << endl;

	//Operation Selection
	cin >> choice;


	switch (choice)
	{
	case '1':
		signbit(number, x); // Function Call
		if (number >= 0)
		{
			cout << "SIGN BIT IS (" << x << ") SINCE THE NUMBER ENTERED IS POSITIVE." << endl;
		}
		
		else
		{
			cout << "SIGN BIT IS (" << x << ") SINCE THE NUMBER ENTERED IS NEGATIVE." << endl;
		}
		break;

	case '2':
		zoro(number, whole, fraction); // Function Call
		wholebinary = wholeconverter(whole); // Function Call
		cout << "NUMBER (" << abs(whole) << ") IN BASE-10 IS EQUAL TO ("<< wholebinary << ") IN BINARY" << endl;
		break;

	case '3':
		zoro(number, whole, fraction); // Function Call
		wholebinary = wholeconverter(whole); //
		fracbin = fractionconverter(fraction); // Function Call
		cout << "NUMBER (" << abs(fraction) << ") IN BASE-10 IS EQUAL TO (" << fracbin << ") IN BINARY" << endl;
		break;

	case '4':
		zoro(number, whole, fraction); //Function Call
		wholebinary = wholeconverter(whole); //
		fracbin = fractionconverter(fraction); // Function Call
		fullbinary = numberconverter(wholebinary, fracbin);
		cout << "NUMBER(" << abs(number) << ") IN BASE-10 IS EQUAL TO (" << fullbinary << ") IN BINARY" << endl;
		break;

	case '5':
		zoro(number, whole, fraction); //Function Call
		wholebinary = wholeconverter(whole); //
		fracbin = fractionconverter(fraction); // Function Call
		fullbinary = numberconverter(wholebinary, fracbin);
		mantissa = mantissaconverter(wholebinary, fracbin); // Function Call
		cout << "MANTISSA IS (" << mantissa << ") IN BINARY." << endl;
		break;

	case '6':
		exponentconverter(fullbinary); // Function Call
		cout << "NUMBER (exponent ex.) IN BASE-10 IS EQUAL TO (exponenttobinary(exponent,binary) ex.) IN BINARY." << endl;
		break;

	case '7':
		fullconverter(fracbin); // Function Call
		
		break;

	case '8':
		hexconverter(number); // Function Call
		break;

	default:
		cout << "INVALID INPUT" << endl;
		break;
	}

	return 0;
}


/////////////////////////////////////////////////
// signbit function
// checks the sign of the number, positive or negative
// void type
// 2 parameters; one pass by value (number) and one pass by reference (&x)
// number, &x
void signbit(double number, string &x)
	{
	if (number >= 0.00)
	{
		x = '0';
	}
	else
	{
		x = '1';
	}
	}

////////////////////////////////////////////////////
// zoro function
// void function
// seperates the integer value from the decimal value 
// 3 parameters; one pass by value and two pass by reference
// number, whole, fraction
void zoro(double number, int &whole, double &fraction)
{
	// declartion
	//int whole;
	//double fraction;
	// initialization
	fraction = 0.0;
	whole = 0;

	whole = (int)number;
	fraction = number - whole;
	// no return statment
	//return a, b, c, d; only the one before the ; will be reported back
}

///////////////////////////////////////////////////
// wholeconverter function
// string function
// Converts the integer value to binary
// 1 parameter; pass by value
// whole
string  wholeconverter(int whole)
{
	//declaration
	string wholeb;
	//initialization
	wholeb = "";
	if (whole < 0)
		whole = whole * -1;
	whole = abs(whole);

	while (whole > 0)
	{
		if (whole % 2 == 0)
		{
			wholeb = "0" + wholeb;
		}
		else
		{
			wholeb = "1" + wholeb;
		}
		whole = whole / 2;
	}

	return wholeb;
}

////////////////////////////////////////
// fractionconverter function
// string function
// converts the decimal value to binary
// 1 parameter, pass by value
// fraction
string fractionconverter(double num)
{
	// result
	string result = "";
	int flag = 0;

	//if (num <0) num = num * -1;  //big O
	num = num;
	while (flag <= 30)  // number of position mantissa precision
	{
		flag++;  // increment for precision
		num = num * 2;
		if (num >= 1)
		{
			result = result + '1';
			num = num - 1;
			// num = num - (int)num;
		}
		else
			result = result + '0';
	}
	return result;
}

////////////////////////////////////////
// numberconverter function
// string type
// displays the number entered into binary format
// 2 parameters, both pass by value
// number, fracbin
string numberconverter(string wholebinary, string fracbin)
{
	string result = "";
	result = wholebinary + "." + fracbin;
	return result;
}


////////////////////////////////////////
// mantissaconverter function
// string type
// displays the mantissa of the converted number in binary format
// 1 parameter, pass by value
// mantissa
string mantissaconverter(string wholebinary, string fracbin)
{
	string result = "";
	result = wholebinary + fracbin;
	string mantissa(result, 1, 24);
	
	if (mantissa.substr(1, 23) == "1")
	{
		mantissa = mantissa.substr(0, 22) + "1";
	}
	return mantissa;
}

////////////////////////////////////////
// exponentconverter function
// string type
// converts the exponent of the entered number into binary format
// 1 parameter, pass by value
// exponent
string exponentconverter(string fullbinary)
{
	string exponent = "";
	exponent = fullbinary.length() - 1;
	

}


Any advice would be appreciated.
Last edited on
I'm supposed to convert numbers into IEEE 754 single precision

What does that mean?
What does that mean?


Convert a decimal number to 32 bit binary.
Convert the input into an arbitrary precision* rational. Multiply the rational by 2 until it's greater than or equal to 1, then divide by 2 until it's less than 1. The number of times you've divided is the exponent. The mantissa is the binary expansion of the rational trimmed to the 23 most significant digits.
You will need to tweak things for denormals, but this algorithm should get you most of the way there.


* Note: It doesn't need to be strictly arbitrary precision, but you will need at least 128 bits of precision.
Topic archived. No new replies allowed.