Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!solaris.cc.vt.edu!news.vt.edu!newsfeed-00.mathworks.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 15:23:39 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 29 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 1331133819 26480 64.122.56.22 (7 Mar 2012 15:23:39 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Wed, 7 Mar 2012 15:23:39 +0000 (UTC) User-Agent: slrn/pre0.9.9-102 (Linux) Xref: csiph.com comp.os.linux.development.apps:449 On 2012-03-07, Noob wrote: > Grant Edwards wrote: > >> $ man round >> #include >> i = (short) round(MyDoubleValue * 100.0); > > Depending on so-called rounding modes, some CPUs do not round as > mathematicians expect. IMO, depending on 1.5 or 2.5 being rounded one way or the other is a bug, but I realize that in some statistical applications it matters. However, the CPU type should have nothing to do with it -- the behavior of the libm round() function is well defined to "round halfway cases away from zero". If you want half-way values to round to nearest even then the round() man page says that you should call rint(). > See, for example, the "nearest even" mode where both 1.5 and 2.5 > (which can be represented exactly in binary FP) are rounded to 2. > > http://www.ocf.berkeley.edu/~horie/rounding.html -- Grant Edwards grant.b.edwards Yow! PARDON me, am I at speaking ENGLISH? gmail.com