Path: csiph.com!x330-a1.tempe.blueboxinc.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!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 30 May 2011 14:27:08 -0500 From: Pete Becker Organization: Roundhouse Consulting, Ltd. Newsgroups: comp.lang.c++ Date: Mon, 30 May 2011 09:27:08 -1000 Message-ID: <2011053009270823394-pete@versatilecodingcom> References: <2011053009075475971-pete@versatilecodingcom> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: implicit promotion User-Agent: Unison/2.1.4 Lines: 63 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-YK2qNq4FV4LNFn12pj98exEqwaJmv5RQCZ1hTzQ3/KkAzm75syIcJ4CIBX1hQuk5j1Rkm30rP0ftfg/!ihw8jnpXpKshrJJ/5qDoFwg3th+C6OaUnFs+ItRKUcrA2KcqcD9CYFFz2bNrujycCWZ1XZ4= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html 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: 3665 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:5845 On 2011-05-30 09:13:08 -1000, Öö Tiib said: > On May 30, 10:07 pm, Pete Becker wrote: >> On 2011-05-30 09:01:48 -1000, Öö Tiib said: >> >> >> >> >> >>> On May 30, 9:58 pm, Pete Becker wrote: >>>> On 2011-05-30 02:49:08 -1000, Öö Tiib said: >> >>>>> On May 30, 2:45 pm, Juha Nieminen wrote: >>>>>> Paavo Helde wrote: >>>>>>>> If you write >> >>>>>>>> float tot = (float)sum/tally->size(); >> >>>>>>>> you then have a float divided by an int, 1.5. >> >>>>>>> Yes, for getting floating-point division one needs to cast only one >>>>>>> operand to a floating-point type, the other would be cast to the type of >>>>>>> the first automatically. In the original line from OP both operands were >>>>>>> cast, possibly because the original author liked symmetry or clarity. >> >>>>>>   Actually, if a float is divided by an int, aren't both of them cast >>>>>> to double first? (Not that it probably makes any difference in practice, >>>>>> but just pointing out.) >> >>>>> Most things about floating point arithmetics is implementation defined >>>>> so what really happens depends on platform and compiler. >> >>>> Not here. Read about "usual arithmetic conversions". If one argument is >>>> float and the other is int, the int is converted to float. >> >>> Or into something bigger and more precise if implementation wants. Or >>> that is how i read the 5/10 in "usual arithmetic conversions". >> >> No, the type of the result is float. There is general license to do >> floating-point math at higher precision, but that doesn't affect the >> type of the result. >> >> void f(float); >> void f(double); >> >> f(1.0f/3);      // calls f(float) > > Certainly. That is what Juha asked as i understood above: > Both the 1.0f and 3 may be casted to double and result may be casted > back to float for passing to f(float). There are no casts involved. There may be conversions, but that's all internal; there is no requirement that conversions be limited to the same representation as float, double, or long double. But unless you're a serious number cruncher, this is just a distraction. 1.0f/3 has type float. How it gets there is irrelevant. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book)