[unix-pc.bugs] problem with using double and drand48

thad@cup.portal.com (Thad P Floryan) (10/07/90)

lath@geocub.greco-prog.fr (Laurent Lathieyre) in <279@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@cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad ]

ted@eslvcr.wimsey.bc.ca (Ted Powell) (10/07/90)

In article <279@geocub.greco-prog.fr> lath@geocub.greco-prog.fr (Laurent Lathieyre) 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]...

This is exactly why you get absurd results if you let the type default
to int. The function returns a double, probably in a floating point
register, and your program's generated code picks up garbage from
wherever integer results are normally returned.

To tell the compiler that drand48 returns a double value, you need a line
like:
    double drand48();
otherwise the C compiler will helpfully (hah!) default the type to int.
Some library functions will have an associated header file that contains
a declaration of the function. Since this function doesn't, one needs
to determine the necessary declaration from the SYNOPSIS section of the
man page.

drand(48) above should be drand48() -- when posting code, it's generally 
best to extract it from the file you actually compiled and ran. 

The returned range is [0.0, 1.0), not [0.0, 1.0] -- i.e. you are guaranteed
not to get the value 1.0 returned.
-- 
ted@eslvcr.wimsey.bc.ca   ...!ubc-cs!van-bc!eslvcr!ted    (Ted Powell)