# how to divide two ints and get a double?

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-18-2011
redhawk87
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!
• 09-18-2011
Elysia
Can you provide an example?
• 09-18-2011
cyberfish
Quote:

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.
• 09-18-2011
quzah
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.
• 09-18-2011
redhawk87
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
• 09-18-2011
Elysia
Quote:

Originally Posted by quzah
...(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.
• 09-18-2011
cyberfish
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.
• 09-18-2011
AndrewHunter
Quote:

Originally Posted by Elysia
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
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

• 09-18-2011
redhawk87
I was printing it out as %d instead of %f lol! gotta love stupid mistakes...
• 09-18-2011
Elysia
Are you using C or C++?
• 09-18-2011
cyberfish
Quote:

Originally Posted by redhawk87
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.
• 09-18-2011
Elysia
I think you should be more polite.
EDIT: Reply was in response to redhawk87, whose reply seems to have been deleted.
• 09-18-2011
AndrewHunter
Quote:

Originally Posted by Elysia
I think you should be more polite.

? Did I miss something?? :confused:
• 09-18-2011
redhawk87
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
• 09-18-2011
Elysia
You still haven't elaborated on whether you are using C or C++.
There is little point in using printf in C++.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last