problem with using double and drand48

Thad P Floryan thad at cup.portal.com
Sun Oct 7 07:16:02 AEST 1990


lath at geocub.greco-prog.fr (Laurent Lathieyre) in <279 at geocub.greco-prog.fr>
writes:

	here a piece of C code which provides some strange
	results :
	double d;
	printf("%f\n",drand(48));
	d=drand48();
	printf("%f\n",d);

	output:
	0.899854
	1071863078.000000

	according to the manual section 3, drand48() is supposed to return a
	double type value in [0.0,1.0]...

Since you have the manual (or at least section 3), look at ``INTRO(3)'' which
mentions you should ``#include <math.h>'' in which is contained a declaration
for "drand48()" as its proper double type (at least on most systems; on some,
the assertion for drand48() is missing from math.h).

Barring that, using an ANSI-compliant C compiler with forced prototypes will
cause drand48() to be properly declared.

Also, the lint library /usr/lib/llib-lc has "double drand48(){return (0.0);}"

Barring the use or presence of any of the above on your system, if you change
your program to:

	double d, drand48();
	printf("%f\n", drand48());
	d=drand48();
	printf("%f\n", d);

you'll find the expected results displayed.  BTW, there is NO 'drand(48)' as
in your posted example.

As a general note, when you have the manual(s) and you see a function's
synopsis per:

	``datatype functionname()''

that means that you must explicitly declare the functionname as "datatype" if
you choose not to use the appropriate *.h file or if the function is not so
defined in the *.h file.

Thad Floryan [ thad at cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad ]



More information about the Comp.sys.att mailing list