Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site houligan.UUCP Path: utzoo!watmath!clyde!cbosgd!ihnp4!houxm!whuxl!whuxlm!akgua!akguc!codas!peora!ucf-cs!novavax!houligan!daemon From: daemon@houligan.UUCP Newsgroups: net.unix-wizards Subject: Re: unsigned -> float conversion Message-ID: <385@houligan.UUCP> Date: Fri, 18-Apr-86 17:07:31 EST Article-I.D.: houligan.385 Posted: Fri Apr 18 17:07:31 1986 Date-Received: Mon, 21-Apr-86 01:58:49 EST Sender: daemon@houligan.UUCP Organization: Gould Electronics, Ft. Lauderdale, Florida. Lines: 81 I offer the following bits with the understanding that I do not work for UTX support and have no access to the source, so please don't bombard me with bug reports or fixes. >Try this on your favorite C compiler and see what you get. > >main() >{ > unsigned u; > double d; > float f; > > u = ~0; > d = ((float)(u))+1; > f = ((float)(u))+1; > printf("u = %lu f = %f d = %lf\n", u, f, d); > if (d < 0) printf("d < 0\n"); > if (f < 0) printf("f < 0\n"); >} > >On a VAX-780 running 4.2bsd or a Sun-2 running 2.0 it gives > >u = 4294967295 f = -1.000000 d = -1.000000 >d < 0 >f < 0 > >On a Gould PN6000 running UTX/32 1.2 it gives > > >u = 4294967295 f = -1.000000 d = 4294967295.000000 >f < 0 > I tried the given example on our PN9080 and got the same results that the author of the original article got on the 6000. The float value comes out -1, and the double value comes out to the large positive value which is the value of the unsigned. On the other hand, if you do this: main() { unsigned u; double d; float f; u = ~0; d = (float) u; f = (float) u; printf("u = %lu f = %f d = %lf\n", u, f, d); if (d < 0) printf("d < 0\n"); if (f < 0) printf("f < 0\n"); } you get the same results as the VAX. It could be a problem with the Gould compiler, which probably works by converting the unsigned to an 8-byte quantity (there is a machine instruction that will extend the sign of an integer through 8 bytes), then converting the result to an 8-byte floating- point variable. Apparently, in the first example, it fails for some reason to do the sign extension. But, now for a larger question: I claim that both the VAX and Gould results are incorrect. The Kernighan and Ritchie book doesn't say anything about converting unsigned quantities directly to float, but I think that if I wanted to assign an unsigned quantity to a float or double, I would damn well expect to get a positive number. It doesn't make sense to me to take something that is explicitly declared as unsigned and treat it as if it were signed. (However, I fear that most machines don't have a convert- unsigned-to-float instruction, so I may be stuck with this.) What does everyone out there think? Dave Cornutt Gould Computer Systems Ft. Lauderdale, FL gould!dcornutt (if it works) "The opinions expressed herein are not necessarily those of my employer; in fact, the opinions expressed herein are not necessary."