unsigned int assigned to double yields negative # ... Why?

James Jones jejones at mcrware.UUCP
Sat Mar 19 02:37:55 AEST 1988


In article <5020 at nsc.nsc.com>, andrew at nsc.nsc.com (Andrew Lue) writes:
> Using various C compilers on VAX and Series 32000 based machines...
> I've gotten negative results instead of positive results [from con-
> versions of unsigned to double when the unsigned value has its MSB
> set].

This one has clobbered me, too, under 4.2 on the VAX.  The code generated
was a VAX instruction that is determined to interpret the integer quantity
being converted as signed...so, I wound up compensating with something
like

	unsigned int x;
	double d;

	if ((d = x) < 0)
		d += /* whatever 2**32 is */;

to get what I wanted.  (The compiler on the Sun 3 I use does the moral
equivalent of this, since the fmove on the 68881 treats integers as signed
when converting them to floating-point.)

I would've added here what ANSI Draft says about the matter, but looking
at section 3.2 doesn't seem to indicate what happens.  Certainly getting
a negative result violates the Law of Least Amazement, if nothing else.

		James



More information about the Comp.lang.c mailing list