Question

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 signficant position to where the exponent bit field belongs.
const   uInt   SIGN_SHIFT       = 31;  


// PURPOSE: To be the mask to only keep the sign bit field.
#define       SIGN_MASK       (uInt)(0x1 << SIGN_SHIFT)


//--          Exponent related constants           --//

// PURPOSE: To tell how many bits to shift the exponent bit field from the
//   least signficant position to where the exponent bit field belongs.
const   uInt   EXPONENT_SHIFT       = 23;  


// PURPOSE: To be the mask to only keep the exponent bit field.
#define       EXPONENT_MASK       (uInt)(0xFF << EXPONENT_SHIFT)


// PURPOSE: To tell the exponent bit pattern for denormalized numbers.
const   uInt   EXPONENT_DENORMALIZED_BIT_PATTERN
                   = 0x00;

// PURPOSE: To tell the exponent value (*not* the bit pattern value) that
//   signifies a denormalized number.
const   int   DENORMALIZE_EXPONENT   = -127;


// PURPOSE: To tell the 'bias' of the exponents:
//   exponent = (bit field number) - 'bias'.
const   uInt   EXPONENT_BIAS       = 0x7F;


//--          Mantissa related constants           --//

// PURPOSE: To tell the mask to only keep the mantissa bit field.
const   uInt   MANTISSA_MASK       = 0x007FFFFF;  


// PURPOSE: To tell give the hidden bit in its proper position.
const   uInt   MANTISSA_HIDDEN_BIT   = 0x00800000;  


// 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'

unsigned int* iPtr =(unsigned int*) &number;
unsigned int i =*iPtr;

if ((i & SIGN_MASK))
return (1);

return (0);

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

unsigned int* iPtr =(unsigned int*) &number;
unsigned int i =*iPtr;

if ((i & EXPONENT_MASK)>>EXPONENT_SHIFT)-EXPONENT_BIAS;
return (i);

}


// 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(number) != DENORMALIZE_EXPONENT) then turn the MANTISSA_HIDDEN_BIT on

unsigned int* iPtr =(unsigned int*) &number;
unsigned int i =*iPtr;
if ((i & EXPONENT_MASK)==0){
i =i & MANTISSA_MASK;
}
else
{
i = i & MANTISSA_MASK;
i = i|MANTISSA_HIDDEN_BIT;
}
return i;

}


// 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.

unsigned int* iPtr =(unsigned int*) &number;
unsigned int i =*iPtr;

if ((i|0x10000000)==0x10000000)
{

return 1;
}
else return 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 < rhsExp)
return( isLhsPos ? 1 : 0 );

// II.D. Compare mantissas:
int   lhsMant       = obtainMantissa(lhs);
int   rhsMant       = obtainMantissa(rhs);

if (lhsMant > rhsMant)
return( isLhsPos ? 0 : 1 );

if (lhsMant < rhsMant)
return( isLhsPos ? 1 : 0 );

// III. Finished, if get here they are equal:
return(0);
}


// PURPOSE: To compare several floating point numbers with 'isLessThan()',
//   and to tell how accurate the function is. Ignores arguments. Returns
//   'EXIT_SUCCESS' to OS.
int       main       ()
{
// I. Application validity check:

// II. Do comparisons:
// II.A. Define numbers to compare:
const float   NUMBER_ARRAY[]   = {-INFINITY,
               -FLT_MAX,
               -1e+10,
               -1.0,
               -1e-10,
               -1.17549e-38,
               -5.87747e-39,
               -2.93874e-39,
               -1.4013e-45,
               -0.0,
               +0.0,
               +1.4013e-45,
               +2.93874e-39,
               +5.87747e-39,
               +1.17549e-38,
               +1e-10,
               +1.0,
               +1e+10,
               +FLT_MAX,
               +INFINITY
               };
const int   NUM_NUMS   = sizeof(NUMBER_ARRAY) / sizeof(float);

// II.B. Decompose all numbers into sign, exponent and mantissa:
int       index;

for (index = 0; index < NUM_NUMS; index++)
{
float   number   = NUMBER_ARRAY[index];
int       isPos   = isPositive(number);
int       exponent= obtainExponent(number);
int       mantissa= obtainMantissa(number);

float   reconstitute
           = (exponent == DENORMALIZE_EXPONENT)
           ? pow(2.0,exponent+1) *
           ((double)(mantissa))/MANTISSA_HIDDEN_BIT
           : pow(2.0,exponent) *
           ((double)(mantissa))/MANTISSA_HIDDEN_BIT;

if (!isPos)
reconstitute   = -reconstitute;

printf("%12g: %c\t%4d\t0x%06X\t%g\n",
   number,
   isPositive(number) ? '+' : '-',
   exponent,
   mantissa,
   reconstitute
   );
}
  
// II.C. Do comparisons:
int       indexOut;
int       indexIn;

for (indexOut = 0; indexOut < NUM_NUMS; indexOut++)
{
float   lhs   = NUMBER_ARRAY[indexOut];

for (indexIn = 0; indexIn < NUM_NUMS; indexIn++)
{
float   rhs   = NUMBER_ARRAY[indexIn];
int   result = isLessThan(lhs,rhs);

printf("isLessThan(%12g,%12g) == %5s (%s)\n",lhs,rhs,
   (result ? "true" : "false"),
   ( (result == (lhs < rhs)) ? "correct" : "WRONG")
   );
}
}

// III. Finished:
return(EXIT_SUCCESS);
}

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

I think your obtainmantissa or iszero function is wrong according to instructions written in questions.

try functions which are written by me.keep other code unchanged.I have written functions according to instructions.

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(number) != DENORMALIZE_EXPONENT) then turn the MANTISSA_HIDDEN_BIT on

unsigned int* iPtr =(unsigned int*) &number;
unsigned int i =*iPtr;

i=i & MANTISSA_MASK;

//MANTISSA_SHIFT =0 so no need to shift..

if(obtainExponent(number)!= DENORMALIZED_EXPONENT)

{

i=i|MANTISSA_HIDDEN_BIT;

}

return i;
}

int       isZero       (float       number
               )
{
// If (obtainExponent(number) == DENORMALIZE_EXPONENT) and
// (obtainMantissa(number) == 0x00) then return 1, otherwise 0.

unsigned int* iPtr =(unsigned int*) &number;
unsigned int i =*iPtr;

if (obtainMantissa(number)==0x00 && obtainExponent(number)==DENORMALIZE_EXPONENT)
{

return 1;
}
else return 0;
}

}

PLEASE DON'T FORGET TO GIVE FEEDBACK.IT HELPS US TO IMPROVE OUR ANSWERS.

Add a comment
Know the answer?
Add Answer to:
I need help with this assignment. My answers are in bold but I am not getting...
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
  • I need help modifying this program. How would I make sure that the methods is being...

    I need help modifying this program. How would I make sure that the methods is being called and checked in my main method? Here is what the program needs to run as: GDVEGTA GVCEKST The LCS has length 4 The LCS is GVET This is the error that I'm getting: The LCS has length 4 // I got this right The LCS is   //the backtrace is not being called for some reason c++ code: the cpp class: /** * calculate...

  • ***The following is to be written in C:**** ****The following is the sizeof.c program that needs...

    ***The following is to be written in C:**** ****The following is the sizeof.c program that needs to be modified:**** ****Section B11 the question asks to refer to:**** Modify the sizeof.c program (in the sizeof folder on github) and show the data range (min and max) in addition to the size of the data types in a nice table (print one line for each data type). Refer to section B11 in your textbook (page 257) for getting min and max values...

  • C++ Thinking recursively - No loops , i solved the first one i need help with...

    C++ Thinking recursively - No loops , i solved the first one i need help with the others int factR(int n) {    // Find the factorial of a number    if (n == 0) return 1;    return n * factR(n - 1);    return 0; } 2) //   Linear Search int searchR(const int data[], int first, int last, int lookFor) {    return 0; } 3) int binarySearchR(const int data[], int first, int last, int lookFor) {     return...

  • Getting the following errors and not sure why. My classes for Van are defined just like...

    Getting the following errors and not sure why. My classes for Van are defined just like for everything else. Also not sure why I am getting the error "undeclared identifier" . Below are the errors when I try and build: ______________________________________________________________________________________ ./vehicles.h:1:2: error: unterminated conditional directive #ifndef VEHICLES_H ^ vehicles.cpp:33:44: error: member initializer 'verbose_' does not name a non-static data member or base class : serialNumber_(0), passengerCapacity_(0), verbose_(false)                                            ^~~~~~~~~~~~~~~ vehicles.cpp:36:10: error: out-of-line definition of 'Vehicle' does not match any...

  • This is c++ programming and here is my code. I am getting an exception thrown on...

    This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception? #include <iostream> #pragma warning(disable:4996) class PermanentWorker { private:    char *name;    int salary; public:    PermanentWorker(const char* nam, int money) : salary(money)    {        name = new char[strlen(nam) + 1];        strcpy(name, nam);    }    int getPay() const    {        return salary;   ...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • I need help implementing the following code without using filehandling and only getchar(); this is the...

    I need help implementing the following code without using filehandling and only getchar(); this is the prompt "The input for your program will be a text file containing a large amount of English. Typically, an English sentence ends with a period (aka, dot). Many years ago, when people used mechanical typewriters, the proper form was to place one space between words in a sentence, but two spaces after the period at the end of the sentence. This rule is no...

  • Hello everyone! I am working on my assignment for C++ and I'm having a bit of...

    Hello everyone! I am working on my assignment for C++ and I'm having a bit of trouble on the value producing functions, I am creating a program wherein I have to input a value and I need to create a function called square value where in it will square the value I have input. I cannot seem to figure out this part. Here is the code that I have so far, and thank you for any help. I greatly appreciate...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

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