unsigned -> float conversion

daemon at houligan.UUCP daemon at houligan.UUCP
Sat Apr 19 08:07:31 AEST 1986


	<EAT THIS LINE>

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."



More information about the Comp.unix.wizards mailing list