float

Discussion in 'C++' started by ramtin, Oct 17, 2011.

  1. ramtin

    ramtin Guest

    hi all ,

    #include "stdafx.h"
    #include <stdio.h>
    using namespace System;

    int main(array<System::String ^> ^args)
    {
    float d = 4 / 3 ;
    printf("%f",d);
    return 0;
    }

    when you run this ,it will be 1 in d ,but it should be 1.3333 in it !!!
     
    ramtin, Oct 17, 2011
    #1
    1. Advertising

  2. ramtin

    Dombo Guest

    Op 17-Oct-11 21:40, ramtin schreef:
    > hi all ,
    >
    > #include "stdafx.h"
    > #include<stdio.h>
    > using namespace System;
    >
    > int main(array<System::String ^> ^args)
    > {
    > float d = 4 / 3 ;
    > printf("%f",d);
    > return 0;
    > }
    >
    > when you run this ,it will be 1 in d ,but it should be 1.3333 in it !!!


    That is because 4 and 3 are integers, so the result of the division will
    be an integer as well, after that the integer result is converted to a
    float. If you use 4.0/3.0 you should get something close to 1.3333.
     
    Dombo, Oct 17, 2011
    #2
    1. Advertising

  3. ramtin

    Thomas Boell Guest

    On Mon, 17 Oct 2011 12:40:40 -0700 (PDT)
    ramtin <> wrote:

    > hi all ,
    >
    > #include "stdafx.h"
    > #include <stdio.h>
    > using namespace System;
    >
    > int main(array<System::String ^> ^args)
    > {
    > float d = 4 / 3 ;
    > printf("%f",d);
    > return 0;
    > }
    >
    > when you run this ,it will be 1 in d ,but it should be 1.3333 in it !!!


    What language is it?
     
    Thomas Boell, Oct 17, 2011
    #3
  4. On Oct 17, 10:40 pm, ramtin <> wrote:
    > hi all ,
    >
    > #include "stdafx.h"
    > #include <stdio.h>
    > using namespace System;
    >
    > int main(array<System::String ^> ^args)
    > {
    >         float d = 4 / 3 ;
    >         printf("%f",d);
    >     return 0;
    >
    > }
    >
    > when you run this ,it will be 1 in d ,but it should be 1.3333 in it !!!


    Hi Ramtin
    Besides what Dombo wrote you can use static_cast:
    float d = static_cast<float>(4)/3;

    Regards,
    -- Saeed Amrollahi
     
    Saeed Amrollahi, Oct 17, 2011
    #4
  5. ramtin

    Ian Collins Guest

    On 10/18/11 08:40 AM, ramtin wrote:
    > hi all ,
    >
    > #include "stdafx.h"
    > #include<stdio.h>
    > using namespace System;
    >
    > int main(array<System::String ^> ^args)


    This looks like a language almost, but not entirely, unlike C++.

    --
    Ian Collins
     
    Ian Collins, Oct 17, 2011
    #5
  6. ramtin

    Ian Collins Guest

    On 10/18/11 09:05 AM, Saeed Amrollahi wrote:
    > On Oct 17, 10:40 pm, ramtin<> wrote:
    >> hi all ,
    >>
    >> #include "stdafx.h"
    >> #include<stdio.h>
    >> using namespace System;
    >>
    >> int main(array<System::String ^> ^args)
    >> {
    >> float d = 4 / 3 ;
    >> printf("%f",d);
    >> return 0;
    >>
    >> }
    >>
    >> when you run this ,it will be 1 in d ,but it should be 1.3333 in it !!!

    >
    > Hi Ramtin
    > Besides what Dombo wrote you can use static_cast:
    > float d = static_cast<float>(4)/3;


    If the OP were using C++, wouldn't it be easier just to write

    float d = float(4) / 3;

    ?

    Either way, the rules of whatever language the OP is using may be different.

    --
    Ian Collins
     
    Ian Collins, Oct 17, 2011
    #6
  7. ramtin

    Ian Collins Guest

    On 10/18/11 09:31 AM, Leigh Johnston wrote:
    > On 17/10/2011 21:25, Leigh Johnston wrote:
    >> On 17/10/2011 21:23, Ian Collins wrote:
    >>> On 10/18/11 09:05 AM, Saeed Amrollahi wrote:
    >>>> On Oct 17, 10:40 pm, ramtin<> wrote:
    >>>>> hi all ,
    >>>>>
    >>>>> #include "stdafx.h"
    >>>>> #include<stdio.h>
    >>>>> using namespace System;
    >>>>>
    >>>>> int main(array<System::String ^> ^args)
    >>>>> {
    >>>>> float d = 4 / 3 ;
    >>>>> printf("%f",d);
    >>>>> return 0;
    >>>>>
    >>>>> }
    >>>>>
    >>>>> when you run this ,it will be 1 in d ,but it should be 1.3333 in it !!!
    >>>>
    >>>> Hi Ramtin
    >>>> Besides what Dombo wrote you can use static_cast:
    >>>> float d = static_cast<float>(4)/3;
    >>>
    >>> If the OP were using C++, wouldn't it be easier just to write
    >>>
    >>> float d = float(4) / 3;

    >>
    >> No it would be easier to just write
    >>
    >> float d = 4.0f / 3.0f;

    >
    > Or even:
    >
    > float d = 4.0 / 3.0;
    >
    > as the compiler will probably optimize the division into a constant
    > assignment.


    That was already posted (the "what Dombo wrote" referenced above).

    My point was there is no need to use static_cast, a temporary will do.
    If the '4' was an integer variable for example.

    --
    Ian Collins
     
    Ian Collins, Oct 17, 2011
    #7
  8. ramtin

    ramtin Guest

    On Oct 17, 1:35 pm, Ian Collins <> wrote:
    > On 10/18/11 09:31 AM, Leigh Johnston wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On 17/10/2011 21:25, Leigh Johnston wrote:
    > >> On 17/10/2011 21:23, Ian Collins wrote:
    > >>> On 10/18/11 09:05 AM, Saeed Amrollahi wrote:
    > >>>> On Oct 17, 10:40 pm, ramtin<>  wrote:
    > >>>>> hi all ,

    >
    > >>>>> #include "stdafx.h"
    > >>>>> #include<stdio.h>
    > >>>>> using namespace System;

    >
    > >>>>> int main(array<System::String ^>  ^args)
    > >>>>> {
    > >>>>> float d = 4 / 3 ;
    > >>>>> printf("%f",d);
    > >>>>> return 0;

    >
    > >>>>> }

    >
    > >>>>> when you run this ,it will be 1 in d ,but it should be 1.3333 in it!!!

    >
    > >>>> Hi Ramtin
    > >>>> Besides what Dombo wrote you can use static_cast:
    > >>>> float d = static_cast<float>(4)/3;

    >
    > >>> If the OP were using C++, wouldn't it be easier just to write

    >
    > >>> float d = float(4) / 3;

    >
    > >> No it would be easier to just write

    >
    > >> float d = 4.0f / 3.0f;

    >
    > > Or even:

    >
    > > float d = 4.0 / 3.0;

    >
    > > as the compiler will probably optimize the division into a constant
    > > assignment.

    >
    > That was already posted (the "what Dombo wrote" referenced above).
    >
    > My point was there is no need to use static_cast, a temporary will do.
    > If the '4' was an integer variable for example.
    >
    > --
    > Ian Collins


    thanks of all . . .
     
    ramtin, Oct 17, 2011
    #8
  9. ramtin

    SG Guest

    On 18 Okt., 01:55, Paul wrote:
    > Ian Collins wrote in message
    > > [...] ramtin wrote:

    >
    > >> int main(array<System::String ^>  ^args)

    >
    > > This looks like a language almost, but not entirely, unlike C++.

    >
    > That is the syntax use din MSVC++ when you create a windows form
    > projec(C++), probably a .net thing.


    I would guess that a large fraction of the competent regulars
    (including Ian) already know exactly what it is: C++/CLI. And yes,
    that's what you get if you ask Herb Sutter to make .NET easily
    accessible from C++ by use of language extensions.

    Off-topic: I makes me chuckle a bit to see almost the same syntax
    being used for C++/CX where the CX language extensions are used to
    deal with WinRT's ref-counting object system. I guess Microsoft lost a
    bit of interest in .NET and C++/CLI since this seems to make the CLI
    and CX extensions mutually exclusive. Btw, I can recommend Herb
    Sutter's "modern C++" talk. It almost sounds like he's trying to
    convert .NET developers. "Look, how easy it is to write modern and
    safe C++ code!".

    Cheers!
    SG
     
    SG, Oct 18, 2011
    #9
  10. ramtin

    Sjouke Burry Guest

    Thomas Boell <> wrote in
    news:20111017215005.66a8d93d@arcadia:

    > On Mon, 17 Oct 2011 12:40:40 -0700 (PDT)
    > ramtin <> wrote:
    >
    >> hi all ,
    >>
    >> #include "stdafx.h"
    >> #include <stdio.h>
    >> using namespace System;
    >>
    >> int main(array<System::String ^> ^args)
    >> {
    >> float d = 4 / 3 ;
    >> printf("%f",d);
    >> return 0;
    >> }
    >>
    >> when you run this ,it will be 1 in d ,but it should be 1.3333 in it

    cut

    (int(4/(int)3 equals 1.
    so whats new today??
     
    Sjouke Burry, Oct 27, 2011
    #10
  11. ramtin

    Christopher Guest

    On Oct 18, 7:34 am, SG <> wrote:
    > On 18 Okt., 01:55, Paul wrote:
    >
    > > Ian Collins wrote in message
    > > > [...] ramtin wrote:

    >
    > > >> int main(array<System::String ^>  ^args)

    >
    > > > This looks like a language almost, but not entirely, unlike C++.

    >
    > > That is the syntax use din MSVC++ when you create a windows form
    > > projec(C++), probably a .net thing.

    >
    > I would guess that a large fraction of the competent regulars
    > (including Ian) already know exactly what it is: C++/CLI. And yes,
    > that's what you get if you ask Herb Sutter to make .NET easily
    > accessible from C++ by use of language extensions.
    >
    > Off-topic: I makes me chuckle a bit to see almost the same syntax
    > being used for C++/CX where the CX language extensions are used to
    > deal with WinRT's ref-counting object system. I guess Microsoft lost a
    > bit of interest in .NET and C++/CLI since this seems to make the CLI
    > and CX extensions mutually exclusive. Btw, I can recommend Herb
    > Sutter's "modern C++" talk. It almost sounds like he's trying to
    > convert .NET developers. "Look, how easy it is to write modern and
    > safe C++ code!".
    >
    > Cheers!
    > SG


    I never really understood what advantage there would be in using C++/
    CLI. If I want CLI, why wouldn't I just use C# instead? Maybe so I can
    have pointers? OT anyway I suppose.
     
    Christopher, Oct 27, 2011
    #11
  12. ramtin

    Dombo Guest

    Op 27-Oct-11 23:29, Christopher schreef:
    > On Oct 18, 7:34 am, SG<> wrote:
    >> On 18 Okt., 01:55, Paul wrote:
    >>
    >>> Ian Collins wrote in message
    >>>> [...] ramtin wrote:

    >>
    >>>>> int main(array<System::String ^> ^args)

    >>
    >>>> This looks like a language almost, but not entirely, unlike C++.

    >>
    >>> That is the syntax use din MSVC++ when you create a windows form
    >>> projec(C++), probably a .net thing.

    >>
    >> I would guess that a large fraction of the competent regulars
    >> (including Ian) already know exactly what it is: C++/CLI. And yes,
    >> that's what you get if you ask Herb Sutter to make .NET easily
    >> accessible from C++ by use of language extensions.
    >>
    >> Off-topic: I makes me chuckle a bit to see almost the same syntax
    >> being used for C++/CX where the CX language extensions are used to
    >> deal with WinRT's ref-counting object system. I guess Microsoft lost a
    >> bit of interest in .NET and C++/CLI since this seems to make the CLI
    >> and CX extensions mutually exclusive. Btw, I can recommend Herb
    >> Sutter's "modern C++" talk. It almost sounds like he's trying to
    >> convert .NET developers. "Look, how easy it is to write modern and
    >> safe C++ code!".
    >>
    >> Cheers!
    >> SG

    >
    > I never really understood what advantage there would be in using C++/
    > CLI. If I want CLI, why wouldn't I just use C# instead?


    +1
     
    Dombo, Oct 28, 2011
    #12
  13. float d1= 4/3 ; /* promote to inetger only in the right hand side result */

    float d2= 4.0/3 ; /* promote to float */
    float d3= 4/3.0 ;
    float d4= 4.0/3.0;
     
    88888 Dihedral, Oct 29, 2011
    #13
  14. ramtin

    red floyd Guest

    On 10/28/2011 9:06 PM, 88888 Dihedral wrote:
    > float d1= 4/3 ; /* promote to inetger only in the right hand side result */

    No, that's integer arithmetic, result promoted to float.

    >
    > float d2= 4.0/3 ; /* promote to float */
    > float d3= 4/3.0 ;
    > float d4= 4.0/3.0;


    Technically, the first two are promote to double, truncate to float.
    The third is already double, truncate to float.
     
    red floyd, Oct 29, 2011
    #14
  15. red floydæ–¼ 2011å¹´10月29日星期六UTC+8下åˆ1時14分07秒寫é“:
    > On 10/28/2011 9:06 PM, 88888 Dihedral wrote:
    > > float d1= 4/3 ; /* promote to inetger only in the right hand side result */

    > No, that's integer arithmetic, result promoted to float.
    >
    > >
    > > float d2= 4.0/3 ; /* promote to float */
    > > float d3= 4/3.0 ;
    > > float d4= 4.0/3.0;

    >
    > Technically, the first two are promote to double, truncate to float.
    > The third is already double, truncate to float.

    ------------------------------------------------------
    int x=3;
    int y=4;

    float d = x/y ;
    float d= ((float)x)/y ;

    /* whether the compiler knows x,y are constants up to now ? */
     
    88888 Dihedral, Oct 29, 2011
    #15
  16. ramtin

    red floyd Guest

    On 10/28/2011 11:27 PM, 88888 Dihedral wrote:
    >
    >
    > red floydæ–¼ 2011å¹´10月29日星期六UTC+8下åˆ1時14分07秒寫é“:
    >> On 10/28/2011 9:06 PM, 88888 Dihedral wrote:
    >>> float d1= 4/3 ; /* promote to inetger only in the right hand side result */

    >> No, that's integer arithmetic, result promoted to float.
    >>
    >>>
    >>> float d2= 4.0/3 ; /* promote to float */
    >>> float d3= 4/3.0 ;
    >>> float d4= 4.0/3.0;

    >>
    >> Technically, the first two are promote to double, truncate to float.
    >> The third is already double, truncate to float.

    > ------------------------------------------------------
    > int x=3;
    > int y=4;
    >
    > float d = x/y ;
    > float d= ((float)x)/y ;
    >
    > /* whether the compiler knows x,y are constants up to now ? */
    >


    What's your point? 4.0 and 3.0 are *DOUBLE* constants.
     
    red floyd, Oct 29, 2011
    #16
  17. ramtin

    ptyxs Guest

    Is there any compelling reason to use float instead of double ? If
    not, use double.
     
    ptyxs, Oct 30, 2011
    #17
  18. ramtin

    Ian Collins Guest

    On 10/30/11 09:40 PM, ptyxs wrote:
    > Is there any compelling reason to use float instead of double ? If
    > not, use double.


    Is there any compelling reason not to include any context in your post?

    --
    Ian Collins
     
    Ian Collins, Oct 30, 2011
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,319
    Roedy Green
    May 10, 2004
  2. Jim West
    Replies:
    4
    Views:
    1,491
    Jeff Schwab
    Jan 16, 2004
  3. Jukka Lehtonen

    operator== (float, float)

    Jukka Lehtonen, Aug 4, 2004, in forum: C++
    Replies:
    5
    Views:
    2,452
    Jukka Lehtonen
    Aug 5, 2004
  4. bd
    Replies:
    0
    Views:
    660
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,628
    James Kanze
    Oct 8, 2009
Loading...

Share This Page