Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!news-xxxfer.readnews.com!panix!not-for-mail From: Grant Edwards Newsgroups: comp.os.linux.development.apps Subject: Re: cast double to short problem Date: Wed, 7 Mar 2012 20:13:53 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 28 Message-ID: References: NNTP-Posting-Host: dsl.comtrol.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: reader1.panix.com 1331151233 16919 64.122.56.22 (7 Mar 2012 20:13:53 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Wed, 7 Mar 2012 20:13:53 +0000 (UTC) User-Agent: slrn/pre0.9.9-102 (Linux) Xref: csiph.com comp.os.linux.development.apps:451 On 2012-03-07, Ersek, Laszlo wrote: > >> Ah, there's your problem: expecting to get 3. You should expect to >> get either 2 or 3. And you do. :) > > He should get the same result both times. Yes. He should. My point is that he shouldn't expect to. His code should not be written to depend on either (int)(0.3 * 10.0) == 2 or on (int)(0.3 * 10.0) == 3. > - The floating point environment is the same. > - dval starts out the same (as in "object representation"). > - 10 (int) is converted to double the same way (with exact value). > - Both double multiplications happen with the same operands (as in > "object representation"). > - Initialization of (~ simple assignment to) a double does not change the > double product (as in "object representation"). > - Conversion to short is the same (truncated toward zero). I agree: I think it should produce the same result in both cases. -- Grant Edwards grant.b.edwards Yow! I'm using my X-RAY at VISION to obtain a rare gmail.com glimpse of the INNER WORKINGS of this POTATO!!