write a program that requires the number of bits for the exponent and significan
ID: 3549062 • Letter: W
Question
write a program that requires the number of bits for the exponent and significant of a floating point representation, then requires two decimal real numbers and an operation,sum,subtraction or multiplication. The program must determine if the two numbers produce overflow or underflow, if not , perform the selected operation and display the result in binary and decimal format.
In C language.
write a program that requires the number of bits for the exponent and significant of a floating point representation, then requires two decimal real numbers and an operation,sum,subtraction or multiplication. The program must determine if the two numbers produce overflow or underflow, if not , perform the selected operation and display the result in binary and decimal format.Explanation / Answer
int main() { /* Smallest possible positive value for a single precision float */ float min = pow(2.0, -126) * pow(2.0, -23); /* The following will not change the float value due to accuracy */ /* min = min / 1.95F; */ /* Smallest change to the variable that generates a positive underflow */ /* min = min / 2.0F; */ /* Lets say we want to divide this tiny value by an array of numbers */ float val[] = { 1.0, 0.2, 0.5, -2.3, 0.08 }; for(int k = 0; k < ( sizeof(val) / sizeof(val[0]) ); ++k) { /* Testing for underflow */ if( val[k] >= 2.0 || val[k] <= -2.0 ) { cout << ( val[k] < 0 ? "Negative" : "Positive" ) << " overflow alert! Division not possible." << endl; } else { /* we may divide the number */ val[k] = min / val[k]; cout << "val[" << k << "] = " << val[k] << endl; } } return 0; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.