Subject: Re: Castings...
To: Chris G. Demetriou <cgd@pa.dec.com>
From: Neil A. Carson <neil@causality.com>
List: tech-kern
Date: 10/08/1997 17:48:45
Chris G. Demetriou wrote:
> 
> > Situation:
> >       1 Casting a negative floating point number to a unsigned integer
> > also
> >       2   12 % -5 (both long longs)

> Also, casting a negative floating point number to an unsigned integer
> appears to happen "through" 'int', i.e.:
> 
> 459 [dnaunix] tmp % cat bar.c
> 
> main(){
>         double d;
>         unsigned int x;
>         unsigned long y;
> 
>         d = -10.5;
> 
>         x = d;
>         printf("%x\n", x);
> 
>         y = d;
>         printf("%lx\n", y);
> }
> 460 [dnaunix] tmp % cc bar.c
> 461 [dnaunix] tmp % ./a.out
> fffffff6
> fffffffffffffff6
> 
> The same programs generate similar results on NetBSD/i386, modulo the
> fact that the 'long' printf ends up being identical to the 'int'
> printf.

Are there any defined standards for this stuff? In java, they are
defined, is there a 'C' equivalent? Java says if it's negative you get
zero as the result; should this be the defined behaviour in 'C' too?

A similar case for casting out of range numbers was also encountered.

	Neil

-- 
Neil A. Carson                         Marketing Director
E-mail: neil@causality.com             Causality Limited (London, UK)
Mobile: +44 (0)370 593183              Tel/Fax: +44 (0)181 930 7408
Sent from a 233MHz StrongARM NC!       Web: http://www.causality.com.