Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5845

Re: implicit promotion

From Pete Becker <pete@versatilecoding.com>
Organization Roundhouse Consulting, Ltd.
Newsgroups comp.lang.c++
Date 2011-05-30 09:27 -1000
Message-ID <2011053009270823394-pete@versatilecodingcom> (permalink)
References <irusp0$6pi$1@reader1.panix.com> <2011053009075475971-pete@versatilecodingcom> <e36f470e-70c3-441f-97b5-63407ec361fb@p6g2000vbn.googlegroups.com>
Subject Re: implicit promotion

Show all headers | View raw


On 2011-05-30 09:13:08 -1000, Öö Tiib said:

> On May 30, 10:07 pm, Pete Becker <p...@versatilecoding.com> wrote:
>> On 2011-05-30 09:01:48 -1000, Öö Tiib said:
>> 
>> 
>> 
>> 
>> 
>>> On May 30, 9:58 pm, Pete Becker <p...@versatilecoding.com> wrote:
>>>> On 2011-05-30 02:49:08 -1000, Öö Tiib said:
>> 
>>>>> On May 30, 2:45 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>>>>>> Paavo Helde <myfirstn...@osa.pri.ee> 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)

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

implicit promotion Ruben Safir <ruben@mrbrklyn.com> - 2011-05-30 01:44 +0000
  Re: implicit promotion red floyd <no.spam.here@its.invalid> - 2011-05-29 20:35 -0700
    Re: implicit promotion ruben safir <ruben@mrbrklyn.com> - 2011-05-30 00:01 -0400
      Re: implicit promotion Ian Collins <ian-news@hotmail.com> - 2011-05-30 16:08 +1200
        Re: implicit promotion Paavo Helde <myfirstname@osa.pri.ee> - 2011-05-30 01:18 -0500
          Re: implicit promotion Juha Nieminen <nospam@thanks.invalid> - 2011-05-30 11:45 +0000
            Re: implicit promotion Öö Tiib <ootiib@hot.ee> - 2011-05-30 05:49 -0700
              Re: implicit promotion Pete Becker <pete@versatilecoding.com> - 2011-05-30 08:58 -1000
                Re: implicit promotion Öö Tiib <ootiib@hot.ee> - 2011-05-30 12:01 -0700
                Re: implicit promotion Öö Tiib <ootiib@hot.ee> - 2011-05-30 12:07 -0700
                Re: implicit promotion Pete Becker <pete@versatilecoding.com> - 2011-05-30 09:07 -1000
                Re: implicit promotion Öö Tiib <ootiib@hot.ee> - 2011-05-30 12:13 -0700
                Re: implicit promotion Pete Becker <pete@versatilecoding.com> - 2011-05-30 09:27 -1000
            Re: implicit promotion Paavo Helde <myfirstname@osa.pri.ee> - 2011-05-30 10:17 -0500
              Re: implicit promotion Juha Nieminen <nospam@thanks.invalid> - 2011-05-30 18:20 +0000
                Re: implicit promotion Paavo Helde <myfirstname@osa.pri.ee> - 2011-05-30 14:06 -0500
                Re: implicit promotion Pete Becker <pete@versatilecoding.com> - 2011-05-30 09:23 -1000
                Re: implicit promotion Pete Becker <pete@versatilecoding.com> - 2011-05-30 09:29 -1000
                Re: implicit promotion Ruben Safir <ruben@mrbrklyn.com> - 2011-05-31 01:24 +0000
                Re: implicit promotion Ian Collins <ian-news@hotmail.com> - 2011-05-31 13:30 +1200
                Re: implicit promotion ruben safir <ruben@mrbrklyn.com> - 2011-05-31 00:52 -0400
                Re: implicit promotion Pete Becker <pete@versatilecoding.com> - 2011-05-30 17:36 -1000
              Re: implicit promotion Öö Tiib <ootiib@hot.ee> - 2011-05-30 11:39 -0700
                Re: implicit promotion Pete Becker <pete@versatilecoding.com> - 2011-05-30 09:02 -1000

csiph-web