Question

Question 1: Write down an function named bitwisedFloatCompare(float number1, float number2) that tests whether a floating...

Question 1:

Write down an function named bitwisedFloatCompare(float number1, float number2) that tests whether a floating point number number1 is 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 differing bit 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 note the solution is constrained to be implemented using bitwise comparison of the two numbers.

Question 2:

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).significand For example if the number passed an argument is 71 your program 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 for the two functions are given in the attached C course. Complete the two functions mentioned in the question.

  • Use the main function provided to test your functions.

/*
 * 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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The required code is given below in case of any doubts you can ask me in comments and please do upvote as it is necessary for my career.

Main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int FloatCompare(float number1,float number2)
{
int binaryBits;
int *bits = &binaryBits;
uint32_t *num1, *num2;
int resultComapre = 0;
num1 = (uint32_t *)(void *)&number1;
num2 = (uint32_t *)(void *)&number2;
for (*bits = 31; *bits>= 0; (*bits)--) {
if ((*num1 & (UINT32_C(1) << *bits))
&& !(*num2 & (UINT32_C(1) << *bits))) {
resultComapre = 1;
break;
}
else if (!(*num1 & (UINT32_C(1) << *bits))
&& (*num2 & (UINT32_C(1) << *bits))) {
resultComapre = -1;
break;
}
}
if (*bits == 31)
resultComapre = -resultComapre;
return resultComapre;
}

void binaryRepsent(int numb, int bits)
{
int bit;
for (bits--; bits >= 0; bits--)
{
bit = numb >> bits;
if (bit & 1)
printf("1");
else
printf("0");
}
}

void printFloatRepresentation(float number)
{
typedef union
{
float fpNum;
struct
{
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} field;
} myfloat;

myfloat floatingpoint;
floatingpoint.fpNum=number;
printf("%d ",floatingpoint.field.sign);
binaryRepsent(floatingpoint.field.exponent, 8);
printf(" ");
binaryRepsent(floatingpoint.field.mantissa, 23);
printf("\n");
}
int main()
{
float number1;
float number2;
int comparison;
number1=56;
number2=12;
comparison=FloatCompare(number1,number2) ;
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");
printFloatRepresentation(number1);
printFloatRepresentation(number2);
//numbers 71 and -71
printf("\nRepresentation of 71 and -71\n");
printFloatRepresentation(71);
printFloatRepresentation(-71);
return 0;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Question 1: Write down an function named bitwisedFloatCompare(float number1, float number2) that tests whether a floating...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using C , comments will be appreciated. Question1 Write down an function named bitwisedFloatCompare(float number1, float...

    using C , comments will be appreciated. Question1 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...

  • In the following code, it gets hung up at    cout << "Number1 * Number2 =...

    In the following code, it gets hung up at    cout << "Number1 * Number2 = " << number1 * number2 << endl; giving an error of "no math for operator<<" what am i doing wrong? Thank you #include <iostream> #include <cctype> #include <cstdlib> using namespace std; class Rational //class for rational numbers (1/2, 5/9, ect..) {    public:        Rational(int numerator, int denominator);        Rational(int numberator);        Rational(); //default        friend istream& operator >>(istream& ins,...

  • MATLAB QUESTION function Huge_Add = huge_add(number1,number2) sum= number1 + number2; sprintf(' %+5.2d',sum) end 3. Write function...

    MATLAB QUESTION function Huge_Add = huge_add(number1,number2) sum= number1 + number2; sprintf(' %+5.2d',sum) end 3. Write function called huge add that adds together two positive integers of any length specified as strings using decimal notation. The single output argument is the result and it is a string as well. The inputs and output must contain digits only; no commas, spaces or any other characters are allowed. If any of these assumptions are violated by the input, the function returns the number-1.

  • i have no idea how to do floating point comparison this is c programming CHALLENGE 3.16.1:...

    i have no idea how to do floating point comparison this is c programming CHALLENGE 3.16.1: Floating-point comparison: Print Equal or Not equal Write an expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targetValue. Otherwise, print "Not equal". Ex: If targetValue is 0.3333 and sensorReading is (1.0/3.0), output is Equal 1 #include <stdio.h 2 #include <math.h> 4 int main(void)f 6 8 scanf("%lf", &targetValue); 1 test passed double targetValue; double...

  • 61. The following program is accepted by the compiler:         int sum( int x, int y...

    61. The following program is accepted by the compiler:         int sum( int x, int y )         {             int result;             result = x + y;            }                            T__   F__ 62. The following implementation is accepted by the compiler:         void product()         {             int a; int b; int c; int result;             cout << "Enter three integers: ";             cin >> a >> b >> c;             result = a * b * c;            ...

  • there is a function to create two random numbers between 1 and 25 and a function...

    there is a function to create two random numbers between 1 and 25 and a function to add them together. add the prototypes correctly, call them from main correctly and output the results. The three functions should be subtractNumbers, multiplyNumbers, and divideNumbers. Remember that division won't always yield an integer so be sure to take care of that issue within the function (don't change the variable declarations). Don't change the code that I've given you. Follow the same pattern. #include...

  • I need help with this assignment. My answers are in bold but I am not getting...

    I need help with this assignment. My answers are in bold but I am not getting the correct output which is also below. Can you please take a look at it. #include   <stdlib.h> #include   <stdio.h> #include   <float.h> #include   <math.h> // PURPOSE: To define a nickname for type 'unsigned int'. typedef   unsigned int           uInt; //--          Sign related constants           --// // PURPOSE: To tell how many bits to shift the sign field from the //   least...

  • I must call multiply, divide and remainder functions inside of SUBTRACT function.. I am stuck at...

    I must call multiply, divide and remainder functions inside of SUBTRACT function.. I am stuck at this point. Thank you #include<stdio.h> int getDifference(int a, int b); int main() { int a, b, result, multiply, divide, rem; printf("Enter the two integer numbers : "); scanf("%d %d", &a, &b); //Call Function With Two Parameters result = getDifference(a, b);    printf("Difference of two numbers is : %d\n" , result);    return (0); } int getDifference(int a, int b) { int c, multiply; float...

  • 2. Write a program with three functions that will be called from the main function The functions will calculate the...

    2. Write a program with three functions that will be called from the main function The functions will calculate the volume, area and perimeter of a cuboid. You should able to pass three parameters such as length, width and height, and a function will return area, another will return volume and final one will return perimeter. Define all your variables in floating point numbers 3. Modify the problem#2 to calculate the volume, area and perimeter in a sing le function...

  • Please complete the implementation of the four functions IN C. //************************************************************************************/ // // countNumberofOnes // //...

    Please complete the implementation of the four functions IN C. //************************************************************************************/ // // countNumberofOnes // // Description: Counts the number of 1s in an integer passed as argument // Preconditions:input argument is passed as a pointer // Postconditions:the number of 1s returned // // Calls: N/A // Called by: main // //***********************************************************************************/ int countNumberofOnes(uint32_t *intData) { // Write a function that counts number of 1s in an integer passed } //***********************************************************************************/ //* //* setBit //* //* Description: The function sets...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT