Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PROGRAMMING IN C Define the following constants: SIGN_SHIFT how many bits to shift the sign field from the least signficant position to where the exponent
PROGRAMMING IN C
Define the following constants:
SIGN_SHIFT | how many bits to shift the sign field from the least signficant position to where the exponent bit field belongs. |
EXPONENT_SHIFT | how many bits to shift the exponent bit field from the least signficant position to where the exponent bit field belongs. |
MANTISSA_MASK | the mask to only keep the mantissa bit field. |
MANTISSA_HIDDEN_BIT | the hidden bit in its proper position |
MANTISSA_SHIFT | How many bits to shift the mantissa bit field from the least signficant position to where the mantissa bit field belongs. |
Use those constants, and the others given, to finish the following functions:
int isPositive(float number) | Returns '1' if 'number' is positive, or '0' otherwise. |
int obtainExponent(float number) | Returns the power-of-2 exponent of 'number'. |
int obtainMantissa(float number) | Returns the mantissa of 'number'. |
int isZero(float number) | Returns return '1' if 'number' is +0.0 or -0.0, or false otherwise. |
Copy and paste the following:
/*-------------------------------------------------------------------------* *--- ---* *--- floatCompare.c ---* *--- ---* *--- This file defines a program that supports the function ---* *--- isLessThan(float lhs,float rhs). It compares 'lhs' and 'rhs' ---* *--- and implements the floating point ' #include#include #include // 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 signficant position to where the exponent bit field belongs. const uInt SIGN_SHIFT = 0; // CHANGE THAT 0! // PURPOSE: To be the mask to only keep the sign bit field. #define SIGN_MASK (uInt)(0x1 // CHANGE THAT 0! // PURPOSE: To be the mask to only keep the exponent bit field. #define EXPONENT_MASK (uInt)(0xFF // CHANGE THAT 0! // PURPOSE: To tell give the hidden bit in its proper position. const uInt MANTISSA_HIDDEN_BIT = 0; // CHANGE THAT 0! // PURPOSE: To tell how many bits to shift the mantissa bit field from the // least signficant position to where the mantissa bit field belongs. const uInt MANTISSA_SHIFT = 0; // PERHAPS CHANGE THAT 0? // PURPOSE: To tell how many mantissa bits there are (including hidden bit) const uInt NUM_MANTISSA_BITS = 24; //-- Miscellaneous related constants --// // PURPOSE: To give the maximum length of C-strings. const uInt TEXT_LEN = 64; //-- Functions: --// // PURPOSE: To return '1' if 'number' is positive, or '0' otherwise. int isPositive (float number ) { // Make an integer with the same bit pattern as number // If that integer has SIGN_MASK on then return '0', otherwise '1' } // PURPOSE: To return the power-of-2 exponent of 'number'. int obtainExponent (float number ) { // Make an integer with the same bit pattern as number // Use EXPONENT_MASK to only keep only the exponent bits. // Shift the result over by EXPONENT_SHIFT // Subtract EXPONENT_BIAS from the shifted result, return() that value } // PURPOSE: To return the mantissa of 'number'. int obtainMantissa (float number ) { // Make an integer with the same bit pattern as number // Use MANTISSA_MASK to only keep only the exponent bits. // Shift the result over by MANTISSA_SHIFT // Only if (obtainExponent(bitPattern) != DENORMALIZE_EXPONENT) then turn the MANTISSA_HIDDEN_BIT on } // PURPOSE: To return '1' if 'number' is +0.0 or -0.0, or false otherwise. int isZero (float number ) { // If (obtainExponent(number) == DENORMALIZE_EXPONENT) and // (obtainMantissa(number) == 0x00) then return 1, otherwise 0. } // PURPOSE: To return '1' if 'lhs' is computed as being less than 'rhs' or // '0' otherwise. int isLessThan (float lhs, float rhs ) { // I. Application validity check: // II. Compare numbers: // II.A. Handle zero as a special case: if ( isZero(lhs) && isZero(rhs) ) return(0); // II.B. Compare signs: int isLhsPos = isPositive(lhs); int isRhsPos = isPositive(rhs); if (isLhsPos && !isRhsPos) return(0); if (!isLhsPos && isRhsPos) return(1); // II.C. Compare exponents: int lhsExp = obtainExponent(lhs); int rhsExp = obtainExponent(rhs); if (lhsExp > rhsExp) return( isLhsPos ? 0 : 1 ); if (lhsExp rhsMant) return( isLhsPos ? 0 : 1 ); if (lhsMant Expected Output:
Proper ./floatCompare isLessThan ( sLessThan ( isLessThan ( isLessThan ( islessThan ( islessThan ( islessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( -inffalse (correct) -inf,-3.40282e+38)true (correct) -1e+10)true (correct) -1e-10) -true (correct) -inf,-1.17549e-38)true (correct) -inf,-5.87747e-39) true (correct) -inf,-2.93874e-39)-true (correct) -inf, -1.4013e-45)true (correct) -0) true (correct) 0)true (correct) inf, 1.4013e-45) true (correct) inf, 2.93874e-39) - true (correct) in, 5.87747e-39) true (correct) inf, 1.17549e-38) true (correct) 0) true (correct) 1) true (correct) 1et10) --true (correct) inf, 3.40282e+38)true (correct) inf) true (correct) inf) false (correct) isLessThan (-3.40282e+38,-3.40282e+38)false (correct) -le+10) -true (correct) -le-10) -true (correct) isLessThan (-3.40282e+38,-1.17549e-38)true (correct) isLessThan (-3.40282e+38,-5.87747e-39)true (correct) isLessThan (-3.40282e+38,-2.93874e-39)true (correct) isLessThan (-3.40282e+38, -1.4013e-45) true (correct) -0) == true (correct) 0)true (correct) isLessThan (-3.40282e+38, 1.4013e-45)true (correct) isLessThan (-3.40282e+38, 2.93874e-39)true (correct) isLessThan (-3.40282e+38, 5.87747e-39) true (correct) isLessThan (-3.40282e+38, 1.17549e-38) true (correct) 1e-10) == true (correct) 1) true (correct) 1e+10) true (correct) isLessThan (-3.40282e+38, 3.40282e+38) true (correct) inf)true (correct) -inffalse (correct) -1e+10,-3.40282e+38)false (correct) -1e+10)false (correct) -1e-10)true (correct) -1e+10,-1.17549e-38) true (correct) -1e+10,-5.87747e-39)true (correct) -Ie+10,-2.93874e-39) -= true (correct) -le+10, -1.4013e-45)-true (correct) -0)-true (correct) 0)true (correct) -let10,1.4013e-45) true (correct) -le+10, 2.93874e-39) true (correct) -le+10, 5.87747e-39)true (correct) -le+10, 1.17549e-38)true (correct) le-10)-true (correct) 1) -- true (correct) le+10) -true (correct) -le+10, 3.40282e+38) true (correct) f) --true (correct) nf) -- false (correct) 1e-10,-3.40282e+38)false (correct) 1e+10) -false (correct) sThan sLessThan ( isLessThan isLessThan isLessThan ( isLessThan ( isLessThan ( isLessThan (-3.40282e+38, e- isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan ( isLessThan ( isLessThan ( sLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( sLessThan isLessThan( isLessThan ( isLessThan ( Proper ./floatCompare isLessThan ( sLessThan ( isLessThan ( isLessThan ( islessThan ( islessThan ( islessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( -inffalse (correct) -inf,-3.40282e+38)true (correct) -1e+10)true (correct) -1e-10) -true (correct) -inf,-1.17549e-38)true (correct) -inf,-5.87747e-39) true (correct) -inf,-2.93874e-39)-true (correct) -inf, -1.4013e-45)true (correct) -0) true (correct) 0)true (correct) inf, 1.4013e-45) true (correct) inf, 2.93874e-39) - true (correct) in, 5.87747e-39) true (correct) inf, 1.17549e-38) true (correct) 0) true (correct) 1) true (correct) 1et10) --true (correct) inf, 3.40282e+38)true (correct) inf) true (correct) inf) false (correct) isLessThan (-3.40282e+38,-3.40282e+38)false (correct) -le+10) -true (correct) -le-10) -true (correct) isLessThan (-3.40282e+38,-1.17549e-38)true (correct) isLessThan (-3.40282e+38,-5.87747e-39)true (correct) isLessThan (-3.40282e+38,-2.93874e-39)true (correct) isLessThan (-3.40282e+38, -1.4013e-45) true (correct) -0) == true (correct) 0)true (correct) isLessThan (-3.40282e+38, 1.4013e-45)true (correct) isLessThan (-3.40282e+38, 2.93874e-39)true (correct) isLessThan (-3.40282e+38, 5.87747e-39) true (correct) isLessThan (-3.40282e+38, 1.17549e-38) true (correct) 1e-10) == true (correct) 1) true (correct) 1e+10) true (correct) isLessThan (-3.40282e+38, 3.40282e+38) true (correct) inf)true (correct) -inffalse (correct) -1e+10,-3.40282e+38)false (correct) -1e+10)false (correct) -1e-10)true (correct) -1e+10,-1.17549e-38) true (correct) -1e+10,-5.87747e-39)true (correct) -Ie+10,-2.93874e-39) -= true (correct) -le+10, -1.4013e-45)-true (correct) -0)-true (correct) 0)true (correct) -let10,1.4013e-45) true (correct) -le+10, 2.93874e-39) true (correct) -le+10, 5.87747e-39)true (correct) -le+10, 1.17549e-38)true (correct) le-10)-true (correct) 1) -- true (correct) le+10) -true (correct) -le+10, 3.40282e+38) true (correct) f) --true (correct) nf) -- false (correct) 1e-10,-3.40282e+38)false (correct) 1e+10) -false (correct) sThan sLessThan ( isLessThan isLessThan isLessThan ( isLessThan ( isLessThan ( isLessThan (-3.40282e+38, e- isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan (-3.40282e+38, isLessThan ( isLessThan ( isLessThan ( sLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( isLessThan ( sLessThan isLessThan( isLessThan ( isLessThan (
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started