1. Engineering
  2. Computer Science
  3. floating point number representation in c programming you only need...

Question: floating point number representation in c programming you only need...

Question details

Floating Point Number Representation in C programming

You only need to modify the file hw3.c. Following the Bit-Level Floating-Point Coding Rules implement the function with the following prototype:

/* Compute (float)i */

float_bits float_i2f(int i);

For argument i, this function computes the bit-level representation of (float) i.

Here is some sample output on a correctly implemented float_i2f function [vagrant@localhost hw3-handout]$ ./driver Testing numbers between 0x00000000 to 0xOFFFFFFF Testing numbers between 0x10000000 to 0x1FFFFFFF Testing numbers between 0x20000000 to 0x2FFFFFFF Testing numbers between 0x30000000 to 0x3FFFFFFF Testing numbers between 0x40000000 to 0x4FFFFFFF Testing numbers between 0x50000000 to 0x5FFFFFFF Testing numbers between 0x60000000 to 0x6FFFFFFF Testing numbers between 0x70000000 to 0x7EFFFFFF Testing numbers between 0x80000000 to Ox8FFFFFFF Testing numbers between 0x90000000 to 0x9FFFFFFF Testing numbers between 0xA0000000 to 0xAFFFFFFF Testing numbers between 0xB0000000 to 0xBFFFFFFF Testing numbers between 0xC0000000 to 0xCEFFFFFF Testing numbers between OxD0000000 to 0xDEFFFFFF Testing numbers between 0xE0000000 to 0xEFFFFFFF Testing numbers between 0xF0000000 to 0xFFFFFFFF Passed all tests for i2f. [vagrant@localhost hw3-handout]$ ./driver 0 i = 0x00000000, answer = 0x00000000, y ur s luti n = 0x00000000 answer u rac0x0 Passed one test for number 0 vagrant@localhost hw3-handout] ./driver 64 i = 0x00000040, answer = 0x42800000, y ur s luti n = 0x42800000 answer Frac 0x0 Passed one test for number 64 vagrant@localhost hw3-handout]ş ./driver Oxabcd1301 0x0AB CD130, answer = Ox4D2BCD13, your solution 0x4D2BCD13 sign = 0, exp = 0x3A, frac = 0x2BCD13 Passed one test for number 180146480 EI

The program driver can be used in two different ways. You can run this program by passing it an argument that is an integer representation (could be decimal, hexadecimal or octal like you write constant in C), and it will only test if your function works on this given integer. You can also run this program without any argument, and it will test your function on all 23 integer values until you either pass all the tests, or fail at least 10 tests. This test will run for quite a while because 23 is a large number. And it will print out some status showing the test is on going for every 22 test we do. When you run the program in this mode and you dont see the status change for a long time, then most likely it means your code has an infinite loop and is stuck. You could use ctrl-c to terminate the stuck program.

hw3.h

#define HW3_H 3 typedef unsigned float bits: 4 float.bits float.i2f int) #end i f

#ifndef HW3_H
#define HW3_H
typedef unsigned float_bits;
float_bits float_i2f(int i);
#endif

driver.c

#include stdio.h> | # include «stdlib.h> L#include 3 .. hw3.h 5 typedef union { unsigned u いuf-union-type; 10 f loat u2f (unsigned u) return a.f 16 unsigned f2u(float f) float f uf union type a a,u = u; 17 uf union.type a: return a.u 20 21 22 曰int test one i 2f(int i, int detail) { Float bits f float.i2fI); float.bits answef2u(f loat)i): 24 25 26 27 28 白if (detail) { printf( і = Zs0x2.8X, answer 0xx.8X, your solution = OxX.8XMn.. , (ієд)?..-..: , (i<0)?-i: i, answer, f); print f ( answer: ₩nsign-xd, exp 0xXX, frac 0xXXMn, ( answer»-31 ) & 1, ( answer»-23) & 0xff, answer&0x7 fffff); 30 if (f !- answer) 31 Eif (detail) 32 printf(f: ₩ns ign = xd, exp = 0xZX, frac 0xZX₩n, (f>>31) & 1, (f>>23) & 0xff, f&0x7fffff); = 34 else printf( i XsOxX.8x, answer 0x1 . 8X, your solution Oxx.8x n, (i«o)?.. _ .. : . (i<0 )7-i: i. answer, f): 36 37 38 39 40 else ( 4 1 42 43 return 0 return 1 45 46 47 48 49 50 51 52 53 Evoid testAlIi2F() int i 0xFFFFFFFF; int count0 : do { // print out status for every 2 28 numbers tested if ((i&0x0FFFFFFF) == 0) { E printf(Testing numbers between 0xX.8X to 0xX.8XNn, і, i+0x0FFFFFFF); if (testOne i2fi, 0)) fcount ++ if (count 10) 56 E 57 58 59 60 printf Failed 10 tests. Aborting test.Wn): break } while (i != 0xffffffff); if (count 0) { 62 63 64 65 printf(Passed all tests for i2t.₩n..); 67曰int main(int argc, char **argv) { 68 69曰if (argc > 1) { 70 long input: //testing a given integer input strtol(argv[1] , NULL,0); 72 Eif(!testOnei2f ((int)input,1)) f 73 74 75 Eelse { 76 printf( Failed one test, n); printf Passed one test for number Xd.Wn, (nt)input); 78 79曰else{ 80 testAIl i2f(); 82 83 return 0;

#include <stdio.h>
#include <stdlib.h>
#include "hw3.h"

typedef union {
unsigned u;
float f;
} uf_union_type;

float u2f(unsigned u) {
uf_union_type a;
a.u = u;
return a.f;
}

unsigned f2u(float f) {
uf_union_type a;
a.f = f;
return a.u;
}

int testOnei2f(int i, int detail) {
float_bits f = float_i2f(i);
float_bits answer = f2u((float)i);
if (detail) {
printf("i = %s0x%.8X, answer = 0x%.8X, your solution = 0x%.8X ", (i<0)?"-":"", (i<0)?-i:i, answer, f);
printf("answer: sign = %d, exp = 0x%X, frac = 0x%X ", (answer>>31) & 1, (answer>>23) & 0xff, answer&0x7fffff);
}

if (f != answer) {
if (detail) {
printf("f: sign = %d, exp = 0x%X, frac = 0x%X ", (f>>31) & 1, (f>>23) & 0xff, f&0x7fffff);
}
else {
printf("i = %s0x%.8X, answer = 0x%.8X, your solution = 0x%.8X ", (i<0)?"-":"", (i<0)?-i:i, answer, f);
}

return 0;
}
else {
return 1;
}
}

void testAlli2f() {
int i=0xFFFFFFFF;
int count = 0;
do {
i = i + 1;
// print out status for every 2^28 numbers tested
if ((i&0x0FFFFFFF) == 0) {
printf("Testing numbers between 0x%.8X to 0x%.8X ", i,i+0x0FFFFFFF);
}
if (!testOnei2f(i, 0)) {
count ++;
if (count >= 10) {
printf("Failed 10 tests. Aborting test. ");
break;
}
}
} while (i != 0xffffffff);
if (count == 0) {
printf("Passed all tests for i2f. ");
}
}

int main(int argc, char **argv) {
long input;
if (argc > 1) {
//testing a given integer
input = strtol(argv[1],NULL,0);
if(!testOnei2f((int)input,1)) {
printf("Failed one test. ");
}
else {
printf("Passed one test for number %d. ", (int)input);
}
}
else {
testAlli2f();
}
return 0;
}

hw3.c

#include .. hw3.h 2 af loat bits float.i2f(int i) t 3return 0:

#include "hw3.h"
float_bits float_i2f(int i) {
return 0;
}

Could u modify hw3.c please? Thank you.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution