Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feed.news.qwest.net!mpls-nntp-03.inet.qwest.net!news-out.readnews.com!transit3.readnews.com!postnews.google.com!news1.google.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:43:50 -0600 Message-ID: <4ED970C8.1F03@mindspring.com> Date: Fri, 02 Dec 2011 19:43:52 -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> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Lines: 54 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.218.218 X-Trace: sv3-MYSOWBdavT42zFtLbwO/iBHNIGqHWRYl3XAAsv1ZA0OzhZUrcREntR/TOYSbN7i+0S+CFdbhU9EEa+z!pKuJ8jkOeBVSW5Kd9H7HdPLDFH+Ce41YxZM+Fc4uoRD7gR/qtfqMNBwXr4Y/zyJdwsxyQYxZr+PJ!boHq1upIH3o= 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: 2944 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:14253 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. Section 6.3.1.4 is 6.3.1.4 Real floating and integer and the quote that you quoted applies to this case: "When a value of integer type is converted to a real floating type, ..." The relevant section is 6.3.1.3 Signed and unsigned integers 1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged. 2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. ... which amounts to exactly what Quentin Pope said. -- pete