Is my compiler defective?

Sep 26, 2009 at 4:26am
So today i got bored so i decided to write this simple code to see what happens:

1
2
3
4
int x = 7;
int y = 22;
int z = 7/22;
cout << z << endl;


Result: 0

Now my question is, since 7 divided by 22 is actually like 3.141592653589796....
so on and so on forever (If you haven't noticed yet, its "pi") why did it give me 0?




Sep 26, 2009 at 4:41am
Because of the data type you are using. Try it with float/double.

Here is the good place to start reading:

http://www.cplusplus.com/doc/tutorial/variables/

Sep 26, 2009 at 4:43am
OH YEAH! I forgot about floats xD (just goes to show how nooby i am T_T) thanks kevin!
Sep 26, 2009 at 6:16am
And your maths is suspect - 7/22 is nowhere near 3
Sep 27, 2009 at 4:03am
He meant 22/7 not 7/22.
Topic archived. No new replies allowed.