[net.lang.c] Bug converting unsigned to double in BSD 4.23

ballou@brahms (Kenneth R. Ballou) (11/08/86)

I recently had occasion to perform this experiment:

% cat bug.c
main ()
{
  printf ("%u\n", ~ ((unsigned) 0));
  printf ("%lf\n", (double) (~ ((unsigned) 0)));
  printf ("%lf\n", 4294967295.0); /* surely double is large enough for this? */
}
% bug
4294967295
-1.000000
4294967295.000000

The results were the same on BSD 4.2 and BSD 4.3.  Of course, the question
is, did I miss something?  (I don't believe so.)

--------
Kenneth R. Ballou		...!ucbvax!cartan!brahms!ballou
Dept. of Mathematics
University of California
Berkeley, California  94720

donn@utah-cs.UUCP (Donn Seeley) (11/09/86)

A fair bit of code was added to make conversions from unsigned to
floating types work right in the 4.3 BSD VAX C compiler, but
unfortunately conversions of unsigned constants were never tested.
That's the way the cookie bounces.

The fix is very simple.  Change the following two lines in makety() in
the compiler source file lib/mip/trees.c:

------------------------------------------------------------------------
RCS file: RCS/trees.c,v
retrieving revision 1.17
diff -c -r1.17 trees.c
*** /tmp/,RCSt1007207	Sat Nov  8 21:02:34 1986
--- trees.c	Sat Nov  8 20:52:03 1986
***************
*** 1210,1216 ****
  		if (t == DOUBLE) {
  			p->in.op = DCON;
  			if (ISUNSIGNED(p->in.type))
! 				p->dpn.dval = /* (unsigned CONSZ) */ p->tn.lval;
  			else
  				p->dpn.dval = p->tn.lval;
  			p->in.type = p->fn.csiz = t;
--- 1210,1216 ----
  		if (t == DOUBLE) {
  			p->in.op = DCON;
  			if (ISUNSIGNED(p->in.type))
! 				p->dpn.dval = (unsigned CONSZ) p->tn.lval;
  			else
  				p->dpn.dval = p->tn.lval;
  			p->in.type = p->fn.csiz = t;
***************
*** 1219,1225 ****
  		if (t == FLOAT) {
  			p->in.op = FCON;
  			if( ISUNSIGNED(p->in.type) ){
! 				p->fpn.fval = /* (unsigned CONSZ) */ p->tn.lval;
  				}
  			else {
  				p->fpn.fval = p->tn.lval;
--- 1219,1225 ----
  		if (t == FLOAT) {
  			p->in.op = FCON;
  			if( ISUNSIGNED(p->in.type) ){
! 				p->fpn.fval = (unsigned CONSZ) p->tn.lval;
  				}
  			else {
  				p->fpn.fval = p->tn.lval;
------------------------------------------------------------------------

Don't ask me why these casts were commented out (argh!),

Donn Seeley    University of Utah CS Dept    donn@utah-cs.arpa
40 46' 6"N 111 50' 34"W    (801) 581-5668    decvax!utah-cs!donn