Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// FloatCompare // // Description:Accepts two float numbers and compares them bi

ID: 641766 • Letter: #

Question

// 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 FloatCompare(float number1,float number2) { // Write the function to compare and return the corresponding value }

//***********************************************************************************/ //

// printFloatRepresentation // //

Description: Prints the argument passed in floating point representation // Preconditions: // Postconditions: // // Calls: N/A // Called by: main // //***********************************************************************************/

void printFloatRepresentation(float number)

{ // Write a function that prints the floating point representation of a given number }

Explanation / Answer

/* * Write a program that shows the internal binary representation * of given 32-bit signed floating-point number in IEEE 754 format * (the C# type float). Example: -27,25 ? sign = 1, exponent = 10000011, mantissa = 10110100000000000000000. */ using System; class FloatingPointRepresentation { // Returns the binary representation of an decimal integer value static string DecimalWholePartToBinary(int value) { string bin = String.Empty; for (int i = value; i != 0; i /= 2) { bin = i % 2 + bin; } return bin; } // 0.625 = 0.101 binary static string DecimalFractionToBinary(float floatValue) { string bin = String.Empty; // 0.625 -> 1.25 for (floatValue *= 2; floatValue != 0; floatValue *= 2) { bin += (int)floatValue; floatValue -= (int)floatValue; } return bin; } // The leftmost bit designates the sign static int GetSign(float floatValue) { if (floatValue < 0) { return 1; } else { return 0; } } // Exponent is stored in the next 8 bits static string GetExponent(string wholePart, string fraction) { // -127 exponent = 3 if (wholePart.Length != 0) { exponent = wholePart.Length - 1; } // Fraction only else { // 0.125 = 0.001b -> exponent = -3 exponent = -(fraction.IndexOf('1') + 1); } // biased e = e + 127 // 0