CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Division in C++

  1. #1
    Guest

    Division in C++

    In C++ how may I divide and get a floating point result (ie 7/4 = 1.75). Wound you have to do this, or rather is this the only way?

    int a, b;
    float answer, answer2;

    a=7;
    b=4;
    answer = a/b;
    answer2 = a%b;
    answer += answer2;

    Is this the only way to do it? Is this the best way if I must compare two division results and I have to be able to compare all of the decimal places also. Thanks!


  2. #2
    Join Date
    May 1999
    Posts
    128

    Re: Division in C++

    you can cast the int's before division, i.e. use...


    int a,b;
    float answer;

    answer = (float)a / (float)b;






  3. #3
    Join Date
    Jul 1999
    Location
    Russia
    Posts
    51

    Re: Division in C++

    ...or use something like that:
    Code:
    int a, b;
    float answer=(float)a*exp(-log(b));

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured