[comp.sys.atari.st] problem with mwc atoi

leo@philmds.UUCP (Leo de Wit) (09/16/88)

In article <12562@ncoast.UUCP> btb@ncoast.UUCP (Brad Banko) writes:
|
|What is wrong with the following code?
    [some lines deleted]...
|#include <stdio.h>
|/* #include <math.h> */
|
|#define	MAXVAL	32767
|
|main(argc,argv)
|	int argc;  char *argv[];
|{
|	int i, x;
|	extern int atoi();
|
|	for (i=1; i+1<=argc; ++i) {
|		x = rand();
|		printf("%d %d %s %d %d %d\n",
|			x, i, argv[i], (double) x / MAXVAL, atoi(argv[i]),
|			(double) x / MAXVAL * atoi(argv[i]) + 1);
|	}
|
|	putchar('\n');
|}
|
|The code compiles, but produces bad (atoi()?) results... why?  Why are 
|the atoi(argv[i]) values bad (negative)?

The arguments supplied to printf() are not conforming to the format:
(double) x / MAXVAL is of type double (probably 8 bytes on the stack),
while you try to print it as an integer (%d) (2 bytes in mwc ?). So the
first three arguments print correctly, but the last three are taken
wrongly from the stack, and besides are interpreted wrong (as integer,
but they are double,int, double. Using a cast (int)(double expr.) for the
double expressions should solve your problem.

          Leo.

jms@antares.UUCP (joe smith) (09/17/88)

In article <12562@ncoast.UUCP> btb@ncoast.UUCP (Brad Banko) writes:
>		printf("%d %d %s %d %d %d\n",
>			x, i, argv[i], (double) x / MAXVAL, atoi(argv[i]),
>			(double) x / MAXVAL * atoi(argv[i]) + 1);

The way printf picks up arguments is as follows:
%d = Pick up 2 bytes of x (which is an int) = OK
%d = Pick up 2 bytes of i (which is an int) = OK
%s = Pick up a pointer to a string argv[i] = OK
%d = Pick up 2 bytes of the 8 bytes that correspond to the value of the
     expressiion "(double) x /MAXVAL".
%d = Pick up the next 2 bytes from the previous double-precision value
%d = Pick up the 4th and 5th bytes of the double-precision value
\n = Start a new line (which ignores the 7th and 8th bytes of the first
     double precision result, and ignore the 2 bytes of the atoi value,
     and ignore the 8 bytes of the 2nd double-precision floating point value.

The problem is in the use of %d with double.  You should be using %f
for double, however you don't need double in this program; use ints and
longs instead.
-- 
+----------------------------------------------------------------------------+
| TYMNET:JMS@F29  CA:"POPJ P,"  UUCP:{ames|pyramid}oliveb!tymix!antares!jms  |
| INTERNET:JMS%F29.Tymnet@Office-1.ARPA   PHONE:Joe Smith @ (408)922-6220    |
+----------------------------------------------------------------------------+

Gribnif@UMass.BITNET (Dan Wilga) (09/30/88)

I believe the problem with the following:

>       printf("%d %d %s %d %d %d\n",
>           x, i, argv[i], (double) x / MAXVAL, atoi(argv[i]),
>           (double) x / MAXVAL * atoi(argv[i]) + 1);

is not in the atoi() function, but in an incorrect printf() format spec.
Since the two calculations involving "x" are typed to "(double)", the
format spec must be "%f", "%e", or "%g" for these two numbers. Otherwise
what happens is that printf() reads an incorrect value off the stack.

In order to use floating-point printf() routines under MWC, you must
also compile with the -f (or -VFLOAT) option.


Dan Wilga

Gribnif@UMASS.Bitnet