DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    JV Guest

    Integer Division


    How do I divide 2 integers in Visual C++ and have the division return a quotient
    and a decimal instead of just an integer? For example, in the division of
    22 by 7, I want a return of 3.14285... instead of just 3 remainder 1. Also,
    what data type would hold the return value? I'm trying to make an application
    that computes the first thousand numbers of pi.

    Any comments are greatly appreciated!

    //cheesy example

    int main()
    {
    double ans;
    ans = 22/7; //it returns 3 instead of a decimal
    return 0;
    }



  2. #2
    required field Guest

    Re: Integer Division


    you can cast it as a float or double

    ans = (float) 22/7





    "JV" <gjvelez@earthlink.net> wrote:
    >
    >How do I divide 2 integers in Visual C++ and have the division return a

    quotient
    >and a decimal instead of just an integer? For example, in the division of
    >22 by 7, I want a return of 3.14285... instead of just 3 remainder 1. Also,
    >what data type would hold the return value? I'm trying to make an application
    >that computes the first thousand numbers of pi.
    >
    > Any comments are greatly appreciated!
    >
    >//cheesy example
    >
    >int main()
    >{
    > double ans;
    > ans = 22/7; //it returns 3 instead of a decimal
    > return 0;
    >}
    >
    >



  3. #3
    Danny Kalev Guest

    Re: Integer Division



    required field wrote:
    >
    > you can cast it as a float or double
    >
    > ans = (float) 22/7


    actually, this won't do. The cast is applied to the result, which
    already truncated.

    instead, use the following form:
    ans = 22.0/7.0;

    Danny Kalev

    "The ANSI/ISO C++ Professional Programmer's Handbook"
    http://www.amazon.com/exec/obidos/ASIN/0789720221

    Please reply to the newsgroup.



    >
    > "JV" <gjvelez@earthlink.net> wrote:
    > >
    > >How do I divide 2 integers in Visual C++ and have the division return a

    > quotient
    > >and a decimal instead of just an integer? For example, in the division of
    > >22 by 7, I want a return of 3.14285... instead of just 3 remainder 1. Also,
    > >what data type would hold the return value? I'm trying to make an application
    > >that computes the first thousand numbers of pi.
    > >
    > > Any comments are greatly appreciated!
    > >
    > >//cheesy example
    > >
    > >int main()
    > >{
    > > double ans;
    > > ans = 22/7; //it returns 3 instead of a decimal
    > > return 0;
    > >}
    > >
    > >


  4. #4
    required field Guest

    Re: Integer Division


    ans = (float) 22/7 ;

    This works just fine in vc++ 6.0 std.
    The (float) typecast has a higher precendence than '/'
    so it really makes the 22 a float then does the division.

    If it had been:
    ans = (float) (22/7);
    Then it would cast only the result.

    Maybe its different in Borland??

    Danny Kalev <dannykk@inter.net.il> wrote:
    >
    >
    >required field wrote:
    >>
    >> you can cast it as a float or double
    >>
    >> ans = (float) 22/7

    >
    >actually, this won't do. The cast is applied to the result, which
    >already truncated.
    >
    >instead, use the following form:
    > ans = 22.0/7.0;
    >
    >Danny Kalev
    >
    >"The ANSI/ISO C++ Professional Programmer's Handbook"
    >http://www.amazon.com/exec/obidos/ASIN/0789720221
    >
    >Please reply to the newsgroup.
    >
    >
    >
    >>
    >> "JV" <gjvelez@earthlink.net> wrote:
    >> >
    >> >How do I divide 2 integers in Visual C++ and have the division return

    a
    >> quotient
    >> >and a decimal instead of just an integer? For example, in the division

    of
    >> >22 by 7, I want a return of 3.14285... instead of just 3 remainder 1.

    Also,
    >> >what data type would hold the return value? I'm trying to make an application
    >> >that computes the first thousand numbers of pi.
    >> >
    >> > Any comments are greatly appreciated!
    >> >
    >> >//cheesy example
    >> >
    >> >int main()
    >> >{
    >> > double ans;
    >> > ans = 22/7; //it returns 3 instead of a decimal
    >> > return 0;
    >> >}
    >> >
    >> >



  5. #5
    Danny Kalev Guest

    Re: Integer Division



    required field wrote:
    >
    > ans = (float) 22/7 ;
    >
    > This works just fine in vc++ 6.0 std.
    > The (float) typecast has a higher precendence than '/'
    > so it really makes the 22 a float then does the division.
    >
    > If it had been:
    > ans = (float) (22/7);
    > Then it would cast only the result.
    >
    > Maybe its different in Borland??


    Thanks for the info. Actually, I used a different compiler, which
    probably has a bug regarding precedence (not Borland). So your code is
    correct. However, I'd rather avoid casts in general (and C-style cast in
    particular).

    Danny Kalev

    "The ANSI/ISO C++ Professional Programmer's Handbook"
    http://www.amazon.com/exec/obidos/ASIN/0789720221

    Please reply to the newsgroup.




    >
    > Danny Kalev <dannykk@inter.net.il> wrote:
    > >
    > >
    > >required field wrote:
    > >>
    > >> you can cast it as a float or double
    > >>
    > >> ans = (float) 22/7

    > >
    > >actually, this won't do. The cast is applied to the result, which
    > >already truncated.
    > >
    > >instead, use the following form:
    > > ans = 22.0/7.0;
    > >
    > >Danny Kalev
    > >
    > >"The ANSI/ISO C++ Professional Programmer's Handbook"
    > >http://www.amazon.com/exec/obidos/ASIN/0789720221
    > >
    > >Please reply to the newsgroup.
    > >
    > >
    > >
    > >>
    > >> "JV" <gjvelez@earthlink.net> wrote:
    > >> >
    > >> >How do I divide 2 integers in Visual C++ and have the division return

    > a
    > >> quotient
    > >> >and a decimal instead of just an integer? For example, in the division

    > of
    > >> >22 by 7, I want a return of 3.14285... instead of just 3 remainder 1.

    > Also,
    > >> >what data type would hold the return value? I'm trying to make an application
    > >> >that computes the first thousand numbers of pi.
    > >> >
    > >> > Any comments are greatly appreciated!
    > >> >
    > >> >//cheesy example
    > >> >
    > >> >int main()
    > >> >{
    > >> > double ans;
    > >> > ans = 22/7; //it returns 3 instead of a decimal
    > >> > return 0;
    > >> >}
    > >> >
    > >> >


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links