Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are running programs on a machine where values of type int have a 32-bit two’

ID: 3839203 • Letter: W

Question

We are running programs on a machine where values of type int have a 32-bit two’s-complement representation. Values of type float use the 32-bit IEEE format, and values of type double use the 64-bit IEEE format. We generate arbitrary integer values x, y, and z, and convert them to values of type double as follows:

/* Create some arbitrary values */

int x = random();

int y = random();

int z = random();

/* Convert to double */

double dx = (double) x;

double dy = (double) y;

double dz = (double) z;

For each of the following C expressions, you are to indicate whether or not the expression always yields 1. If it always yields 1, describe the underlying mathematical principles. Otherwise, give an example of arguments that make it yield 0. Note that you cannot use an IA32 machine running gcc to test your answers, since it would use the 80-bit extended-precision representation for both float and double.

In addition, one or more of these expressions requires the use of –fwrapv. Explain which ones require this assumption and why.

A. (float) x == (float) dx

B. dx – dy == (double) (x – y)

C. (dx + dy) + dz == dx + (dy + dz)

D. (dx * dy) * dz == dx * (dy * dz)

E. dx/dx = dz/dz

Explanation / Answer

A. It yields 1 as float(x) rounds off the value of x and float(dx) also rounds off the value of dx which are equal so the statement is true and returns 1

B.IIt yields 0 as dx-dy==double(x-y) is not a correct statement as if x>y then double(x-y) yields negative value which may result is signed overflow

C.IIt yields 1 as associative law is applicable over addition for these datatypes

D. It is false so yields 0 because there may be a problem of overflow as in the case of precision like if dx=123.4566 and dy=192.1728 and dz=max value of double then the multiplication is not possible

E.Returns 0.since the statement is false when dx=0 and dz=1 because dx/dx yields undefined or garbage value.

-fwrapv is used as a flag for generating traps for signed overflow

Here in the above expression B is used with flag -fwrapv as signed subraction overflow and D is used for signed multiplication overflow

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote