Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 02 Dec 2011 18:49:02 -0600 Message-ID: <4ED971FE.7646@mindspring.com> Date: Fri, 02 Dec 2011 19:49:02 -0500 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: casting constant value from float to unsigned short - compiler bugs? References: <4ed8950a$0$18699$91cee783@newsreader03.highway.telekom.at> <4ED970C8.1F03@mindspring.com> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Lines: 47 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.218.218 X-Trace: sv3-tO5vKU4beCuiktHYLfd+e9wTzegaQSKlaQscXbrWli7ebQT97aP7rX4YgOcQfQ+/S4xbqQkubdsFcRv!UZOCQp6BJr07Cv2IUZQED9k581J9B1b9oXz/Z4kGll1RgyxBJfHZYGmqLB0xacv4Zh4MRgkkXlpo!bwzYkCwq7eU= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2767 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:14254 pete wrote: > > jacob navia wrote: > > > > Le 02/12/11 23:36, Quentin Pope a écrit : > > > On Fri, 02 Dec 2011 10:34:22 +0100, jacob navia wrote: > > >> Yes. The first one is that the range for unsigned short is zero to > > >> USHORT_MAX. Your program has undefined behavior > > >> since a negative value > > >> will not go into an unsigned short, it is OUTSIDE its domain. > > > > > > Sorry, but this is nonsense. > > > Negative numbers stored in unsigned integer > > > types "wrap around" - there is NO overflow for unsigned types, > > > but all arithmetic is done modulo USHORT_MAX+1. > > > It is amazing that you are not > > > aware of this as a compiler writer! > > > > The C standard specifies that when the > > > > (C Standard 6.3.1.4) > > If the value being converted is outside the range of values that can > > be represented, the behavior is undefined. > > > > > > A negative value can't be represented in an UNsigned integer. > > > > By definition. > > You got that all wrong. Sorry about that. I got that all wrong. I hadn't read back far enough into thread to realise what you were talking about. 6.3.1.4 Real floating and integer 1 When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.) -- pete