Adding two IEEE754 double numbers
-
:confused:Hello, I have an assignment to write a C function to add two IEEE754 double numbers based on an algorithm in my text book. The function adds two positive doubles via two unsigned long parameters and returns the result also in unsigned long format. My professor gave us the main program and an empty subroutine and I need to code the subroutine. All I am trying to do for now is print the values that are entered by the user, not do any calculations, just print them in the main and then once they get into the subroutine and I can't even get that. It's been a year and a half since I have done any c programming, but I did not expect to be stumped this quickly ... Here is the code. I commented the lines that I added, not much. I understand the values I get when I print the HEX, but I get all zeroes when I try to print the decimal values. Thanks for any help. #include unsigned long adddouble(unsigned long x, unsigned long y) { //I added all unsigned long result; result = x; //print values of x and y printf("In subroutine, x = %f and y = %f\n", x ,y); printf("In subroutine, return = %f\n", result); return result; } int main(int argc, char* argv[]) { double d1, d2; double *dp; unsigned long s; unsigned long *longp1, *longp2; //get two input real numbers scanf("%lf %lf", &d1, &d2); longp1 = (unsigned long*)&d1; longp2 = (unsigned long*)&d2; //I added - hex value of what longp1 and longp2 point to printf("In main longp1 = %08x, %08x \n", *longp1, *(longp1+1)); printf("In main longp2 = %08x, %08x \n", *longp2, *(longp2+1)); //I added - decimal value of what longp1 and longp2 point to printf("In main longp1 = %f \n", *longp1); printf("In main longp2 = %f \n", *longp2); s = adddouble(*longp1, *longp2); dp = (double*)&s; printf("%f + %f = %f\n", d1, d2, *dp); return 0; } PC