Question
Write a C program to show that, in floating-point arithmetic,the result of (A+B)-B may not always be equal to A. That is, afterthe following operations,
Write a C program to show that, in floating-point arithmetic,the result of (A+B)-B may not always be equal to A. That is, afterthe following operations, D may not be equal to A.
C A+B D C-B
1. First, run your code and show that D is equal to A.
2. Then, run your code with different A and B values and show thatD is not equal to A. The numbers you select for A and B must becorrectly represented in 32-bit binary form.
For example: (0.6875)10 can be exactly represented in 32-bitfloating format since = 1 * 2-1 + 1 * 2-3 + 1 * 2-4 = 0.5 + 0.125 +0.0625 = 0.6875 Hence, its 32-bit floating point representationwould be 00111111 00110000 00000000 00000000
However, (1.36)10 cannot be exactly represented in 32-bitfloating format since the fractional part 0.36 cannot be exactlyobtained by the sum of the weights 2-1 , 2-2 , 2-3 , etc. Hence,its 32-bit floating point representation would be 00111111 1010111000010100 01111011
Therefore, you must select numbers for A and B such that theirfractional parts can be exactly represented in 32- bit floatingformat, as in the number (0.6875)10.
The following code may help you to find the desired numbers:
#include
#include
#include
int main(){
float a;
for(;;){
printf("ENTER the value of a:");
scanf("%f", &a);
union ieee754_float *p_a;
unsigned int a_exp;
unsigned int a_negative;
unsigned int a_mantissa;
p_a = (union ieee754_float*)&a;
a_exp = p_a->ieee.exponent;
a_negative = p_a->ieee.negative;
a_mantissa = p_a->ieee.mantissa;
printf("exponent of a: %x", a_exp);
printf("negative of a: %x", a_negative);
printf("mantissa of a: %x", a_mantissa);
}
return 0;
}
You can use online c compiler for compiling your code:https://www.onlinegdb.com/online_c_compiler
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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