C-question

Ian Graham igraham at SMAUG.PHYSICS.MCGILL.CA
Tue Apr 30 08:44:22 AEST 1991


/* 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 <stdio.h>
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 at physics.mcgill.ca
					        Tel: (514) 398-6526
					        Fax: (514) 398-8434



More information about the Comp.sys.sgi mailing list