Subj : Re: Math error ??? doen't make sense! To : borland.public.cpp.borlandcpp From : Stan DeGroff Date : Fri Jul 02 2004 09:31 pm Lets change the program to find out more... ------------------------------ #include #include main() { int x,y; long int z; x=7000; y=12000; z=x*y; printf("x = %d, y = %d\n",x,y); printf("sizes of x, y, z = %d, %d, %d\n",sizeof(x),sizeof(y),sizeof(z)); printf("x * y == (unsigned)%lu\n",z); printf("x * y == (signed) %ld\n",z); printf("x = 0x%08X\n",x); printf("y = 0x%08X\n",y); printf("z = 0x%08X\n",z); return 0; } ------------------------------- Do the math on a hex calculator: (1) if you set up the ide for a 16 bit task, the above will report sizeof for x=2, y=2, z=4 bytes (2) if you set format for Unsigned (even though z is signed it will return 4924950144. (3) if you set format as signed, it will return -17152 thats only changing the printf format. NOW! lets use a hex calculator. x = 7000 = 0x1B58 y = 12000 = 0x2EE0 z = x * y = 0x0501 BD00 = 84,000,000 decimal fact, always true: a 16 bit x 16 bit will always fit in 32 bits. z size is 32 bits x & y are initialized as 16 bit integers. z is set up as a 32 bit integer. There ain't no overflow. as shown in the z result above. ---------------------------------- If we set up the task to be a 32 bit task. Sizeof x, y, & z are all 32 bits (4 byte) correct answer is produced. OR if we set int x,y to long int x,y correct answer is produced. conclusion : Multiply in Borland product must have both inputs to a multiply and the output all the same size variables. interesting, since most assembly instructions do things like 16 x 16 = 32 and same for most PLC's (Programmable Logic COntrollers). Is this a deviation from the ANSI C spec? ie: the "Sams Learning C in 24 hrs" book uses 16 x 16 for a 32 bit result as I first brought it to the table. (See my first post, as it came from the book.) Thanks for your input. Stan .