Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intermediate Accounting

Authors: Donald E. Kieso, Jerry J. Weygandt, And Terry D. Warfield

13th Edition

9780470374948, 470423684, 470374942, 978-0470423684

More Books

Students also viewed these Accounting questions

Question

=+a. Calculate the payback period for each project.

Answered: 1 week ago

Question

=+d. Derive the IRR of each project.

Answered: 1 week ago