Question: 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

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.

Floating Point Number Representation in C programming You only need to modifythe 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

hw3.h

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

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

driver.c

#ifndef HW3_H #define HW3_H typedef unsigned float_bits; float_bits float_i2f(int i); #endif driver.c#include #include #include "hw3.h" typedef union { unsigned u; float f; }

#include #include #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>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

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

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

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

Could u modify hw3.c please? Thank you.

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 Oxabcd130 1 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 don't 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. #define HW3_H 3 typedef unsigned float bits: 4 float.bits float.i2f int) #end i f #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>31) & 1, (f>>23) & 0xff, f&0x7fffff); = 34 else printf(" i XsOxX.8x, answer OxX.8X, your solution Oxx.8x n", (io)?.. _ .. : . (i 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 79else{ 80 testAIl i2f(); 82 83 return 0; #include .. hw3.h" 2 af loat bits float.i2f(int i) t 3return 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!