Thread: how to divide two ints and get a double?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    how to divide two ints and get a double?

    I am having troubler with simple division. I know this might be a stupid question but here is my code:

    unsigned int totalCycles;
    unsigned int cycleCount;

    double IPC = (double) totalCycles / cycleCount;

    I then print out IPC. When IPC comes out, it is nowhere near what the value should be. For instance, if totalCycles is ~99 million and cycleCount is around 10.1 million, IPC should be around 9.8. When I print IPC, it comes out as ~2.1 billion. I tried converting the unsigned int to an int and then to a double by doing this:

    double temp = (int)totalCycles;
    double temp2 = (int)cycleCount;

    and temp was like ~60 million and temp2 gave me a value of 0. So that did not work... any help would be great!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Can you provide an example?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    double IPC = (double) totalCycles / cycleCount;
    This should work. Division between double and int will promote the int to a double. There's no need to do the temp stuff.

    Post smallest compilable code with expected and actual outputs.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you casting them to signed integers when they are unsigned? First start by dividing small numbers:
    Code:
    for( unsigned int tc = 99, unsigned int cc = 101, int x = 0; x < 1000; x++ )
    {
        cout << (double) tc / (double) cc << endl;
        tc *= 10;
        cc *= 10;
    }
    Does that give you the answers you expect? (I don't use C++, so they may have changed namespacing or something of cout and endl, so you might have to std:: or whatever it is now.)


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    here are my variables:
    unsigned int instCount;
    unsigned int cycleCount;

    //lots of code

    double IPC = (double) instCount / cycleCount;

    printf(//instructions);
    printf(//cycles);
    printf(//IPC);

    Here is an example of what is printed:

    INSTRUCTIONS = 99983617
    CYCLES = 10163608
    IPC = 2121261532

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by quzah View Post
    ...(I don't use C++, so they may have changed namespacing or something of cout and endl, so you might have to std:: or whatever it is now.)
    A "using namespace std;" at the beginning of the file or simply prefixing "std::" to cout should do the trick.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    That is not compilable code.

    Just copy and paste those lines into a new file, initialize them with values, print them out, and see the output.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Elysia View Post
    A "using namespace std;" at the beginning of the file or simply prefixing "std::" to cout should do the trick.
    Or perhaps neither and this should be moved to the C board....... (even though this topic isn't language specific.)

    Quote Originally Posted by redhawk87 View Post
    here are my variables:
    unsigned int instCount;
    unsigned int cycleCount;

    //lots of code

    double IPC = (double) instCount / cycleCount;

    printf(//instructions);
    printf(//cycles);
    printf(//IPC);

    Here is an example of what is printed:

    INSTRUCTIONS = 99983617
    CYCLES = 10163608
    IPC = 2121261532
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    I was printing it out as %d instead of %f lol! gotta love stupid mistakes...

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you using C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by redhawk87 View Post
    I was printing it out as %d instead of %f lol! gotta love stupid mistakes...
    Common mistake. I was actually guessing that, but there's no way to tell without seeing the actual code (because you see, you thought the problem wasn't in printf calls, so you omitted them).

    That's why we ask for compilable code. 80% of the time, by just simplifying the code, you can realize the mistake yourself.

    By the way, doubles should really be printed with %lf (long float), even though they are functionally identical. If you get used to using %f for double, and accidentally do it for scanf, too, then something very bad will happen. Better keep them consistent.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you should be more polite.
    EDIT: Reply was in response to redhawk87, whose reply seems to have been deleted.
    Last edited by Elysia; 09-18-2011 at 05:03 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Elysia View Post
    I think you should be more polite.
    ? Did I miss something??
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    I misread a persons comment. Thought they were trying to say I was stupid cause the print statements I put there would not compile... I re-read and realized that was all in my head. I deleted the post

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You still haven't elaborated on whether you are using C or C++.
    There is little point in using printf in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. divide by zero
    By kushal in forum C Programming
    Replies: 3
    Last Post: 06-09-2011, 02:51 PM
  2. divide by zero
    By George2 in forum C# Programming
    Replies: 17
    Last Post: 05-02-2008, 02:37 AM
  3. divide long by double
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 03:00 PM
  4. Divide by 3 in ISR
    By Roaring_Tiger in forum C Programming
    Replies: 40
    Last Post: 08-21-2004, 11:51 AM
  5. Cannot divide
    By /nFallen in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2003, 04:35 PM