Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!SMAUG.PHYSICS.MCGILL.CA!igraham From: igraham@SMAUG.PHYSICS.MCGILL.CA (Ian Graham) Newsgroups: comp.sys.sgi Subject: C-question Message-ID: <9104292244.AA26782@smaug.physics.mcgill.ca> Date: 29 Apr 91 22:44:22 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 66 /* Hi. One of our users came upon the following short int/int/double evaluation problem. As he discoverd (see the following example), the product of an unsigned integer with an integer (short or long) is ill-defined when the integer is <1 (here -1). Reordering (or bracketing) of his expressions makes things work properly, since default promotion to double solves the `problem'. My question is: 1) What is the heirarchy for default promotions within an arithmetic expression evaluation? Is there an ANSI (or other :-) ) standard for this, or is it different for different compilers/machines? 2) In particular why, when multiplying an unsigned short and an int is the unsigned short not promoted to int (as opposed to, apparently, an unsigned int) before evaluation? */ #include main() { int i = -1; double xbox=10., prod1, prod2, prod3; double prod4, prod5, prod6; double prod7, prod8, prod9; unsigned short aa=29; short aaa=29; unsigned int bb=29; prod1 = aaa*i*xbox; /* This works */ prod2 = aa *i*xbox; /* This doesn't work */ prod3 = aa *(i*xbox); /* This works */ prod4 = (aa *i)*xbox; /* This doesn't work */ prod5 = aa*xbox * i; /* This works */ prod6 = aa*(double)i*xbox; /* This works */ prod7 = bb *i*xbox; /* This doesn't work */ prod8 = bb *(i*xbox); /* This works */ prod9 = (bb *i)*xbox; /* This doesn't work */ printf("short *int *double: prod1=%f\n",prod1); printf("unsigned short * int * double: prod2=%f\n",prod2); printf("unsigned short * (int * double): prod3=%f\n",prod3); printf("(unsigned short * int) * double: prod4=%f\n",prod4); printf("unsigned short * double * int: prod5=%f\n",prod5); printf("unsigned short *(double)int * double: prod6=%f\n",prod6); printf("unsigned int * int * double: prod7=%f\n",prod7); printf("unsigned int * (int * double): prod8=%f\n",prod8); printf("(unsigned int * int) * double: prod9=%f\n",prod9); } /* Program Output : Iris 3D, Unix 3.3.1. /or/ Sun-3 SunOS 3.5 * * short *int *double: prod1=-290.000000 * unsigned short * int * double: prod2=42949672670.000000 * unsigned short * (int * double): prod3=-290.000000 * (unsigned short * int) * double: prod4=42949672670.000000 * unsigned short * double * int: prod5=-290.000000 * unsigned short *(double)int * double: prod6=-290.000000 * unsigned int * int * double: prod7=42949672670.000000 * unsigned int * (int * double): prod8=-290.000000 * (unsigned int * int) * double: prod9=42949672670.000000 * */ ___________________________________________ Ian Graham ______________ igraham@physics.mcgill.ca Tel: (514) 398-6526 Fax: (514) 398-8434