In: Computer Science
using C , comments will be appreciated. I already posted this before and I am looking for different answer.
please answer both part of the question.
A) Write down an function named bitwisedFloatCompare(float number1,
float number2)
that tests whether a floating point number number1is less than, equal to or greater than another floating point number number2, by simply comparing their floating point representations bitwise from left to right, stopping as soon as the first differingbit is encountered. The fact that this can be done easily is the main motivation for biased exponent notation. The function should return 1 if number1 > number2, return -1 if number2 > number1 and should return 0 if the two numbers are equal. Please notethe solutionis constrainedto be implemented using bitwise comparison of the two numbers.
B) Write a function named printFloatRepresentation(float number) that will print the floating point representation of a number using the format given below. (Sign bit) exponent in binary (assumed bit).
significandFor example if the number passed an argument is 71 yourprogram should print
(0) 10000101 (1).00011100000000000000000
Similarly if the number passed to the function as argument is -71 the program should print
(1) 10000101 (1).00011100000000000000000
The main function and function skeletons forthe twofunctions are given in the attached C course. Complete the two functions mentioned in the question.
/*
* FloatingPointRepresentationFunctions.c
*/
//************************************************************************************/
//
//
FloatCompare
//
//
Description:Accepts two float numbers and compares them bitwise
based
//
on floating point
representations. This function will have
//
to convert the given
numbers into IEEE floating
//
representation and then
do bitwise comparison
//
Preconditions:two input arguments are passed
//
Postconditions:Returns 1,-1 or 0 based on the comparison
//
1 if number1>number2
//
-1 if number2>number1
//
0 if equal
//
Calls: N/A
// Called
by: main
//
//***********************************************************************************/
int bitwisedFloatCompare(float number1,float number2)
{
// Write the function to compare and return the
corresponding value
}
/*
* FloatingPointRepresentation.c
Please do not return this file or the main function with your
homework
*/
#include <stdio.h>
int main()
{
float number1;
float number2;
int comparison;
number1=56;
number2=12;
comparison=bitwisedFloatCompare(number1,number2) ;
// Compare two floating point numbers
if (comparison==1)
printf(“%f
is greater than %f\n”,number1,number2);
else if
(comparison==-1)
printf(“%f is greater than %f\n”,number2,number1);
else if
(comparison==0)
printf(“Number are equal\n”);
else
printf(“Error\n);
return 0;
}
A]
int bitwisedFloatCompare(float number1,float
number2){
int arr1[65];
int arr2[65];
for(int i=0;i<65;i++)arr1[i]=0;
for(int i=0;i<65;i++)arr2[i]=0;
int a = number1;
int b = number2;
double remA = number1-a;
double remB = number2-b;
//THE ARRAY STORES IN THE FIRST 32 BITS THE
BINARY REPRESENTATION OF THE INTEGRAL PART
//AND THE NEXT POSITION IS BLANK REPRESENTING
THE DECIMAL POINT
// AND THE NEXT 32 BITS REPRESENT THE FRACTIONAL
PART
//BINARY REPRESENTATION OF NUMBER1 STORED IN
ARR1
int j = 31;
while(a>0 && j>=0){
arr1[j] = a%2;
a = a/2;
j--;
}
j = 33;
int x;
while(j<65){
x = remA;
arr1[j] = x;
printf("%f\n",remA);
if(x==1){
remA = remA-1;
}
remA = 2*remA;
j++;
}
//BINARY REPRESENTATION OF NUMBER2 STORED IN
ARR2
j = 31;
while(a>0 && j>=0){
arr2[j] = b%2;
b = b/2;
j--;
}
j = 33;
while(j<65){
x = remB;
arr2[j] = x;
if(x==1){
remB = remB-1;
}
remB = 2*remB;
j++;
}
for(int i=0;i<65;i++){
if(i==32)
else {
if(arr1[i]==arr2[i]){
continue;
}else{
if(arr2[i]==1 && arr1[i]==0){
return -1;
}else{
return 1;
}
}
}
}
return 0;
}
B]
void printBinary(int n, int i)
{
// Prints the binary representation
// of a number n up to i-bits.
int k;
for (k = i - 1; k >= 0; k--) {
if ((n >> k) &
1)
printf("1");
else
printf("0");
}
}
typedef union {
float f;
struct
{
// Order is
important.
// Here the members of
the union data structure
// use the same memory
(32 bits).
// The ordering is
taken
// from the LSB to the
MSB.
unsigned int mantissa :
23;
unsigned int exponent :
8;
unsigned int sign :
1;
} raw;
} myfloat;
// Function to convert real value
// to IEEE foating point representation
void printFloatRepresentation(myfloat var)
{
// Prints the IEEE 754 representation
// of a float value (32 bits)
printf("(%d)", var.raw.sign);
printBinary(var.raw.exponent, 8);
printf("(1).");
printBinary(var.raw.mantissa, 23);
printf("\n");
}
/***** USAGE ********/
// myfloat var;
// // Get the real value
// var.f = -2.25;
// // Get the IEEE floating point representation
// printf("Float representation of %f is : \n",
// var.f);
// printFloatRepresentation(var);
/***** END ******/