Arithmetic Question

Hi all,

I know this is kinda of a dumb question but I was hoping someone could help me on this.

I know math and I know C++ evaluates from left to right using associative priority but I can't figure out, even after doing some pretty extensive reading and searching, why a couple of the variables in the following code I'm messing with outputs zeros.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;


int main()
{

int a = 7 * 9 - 3 + 5;
double b = 3 / 4 * 6;
int c = 5 / 1 * 2;
int d = 2 * 2 * 3 / 4;
double e = 2 / 3 * 3 / 3;


cout <<" a: " << a << endl;
cout <<" b: " << b << endl;
cout <<" c: " << c << endl;
cout <<" d: " << d << endl;
cout <<" e " << e << endl;

    return 0;
}




The above code isn't homework, it's just code I'm messing with after reading C++ Primer. B and E are outputting zero's and I'm not sure why.

Aren't doubles supposed to be able to handle fractional values? B should equal to 4.5 and E should be 0.67 (I'm rounding up here) using basic arithmetic. Am I missing something? I've tried experimenting with different variations but it still outputs zeros.

Thanks alot for your help.
I'm think you need decimal points for the values that go into the double.

-Albatross
Last edited on
integer division results in an integer, even if it's assigned to a double.

For example:

double b = 3 / 4;

The compiler first evaluates "3 / 4". Since 3 and 4 are both integers, it evaluates this as 0. Therefore b is assigned to zero.

Even though 'b' is a double, it doesn't matter because b is not involved in the division. And therefore the division is integral.


If you want floating point division than one of more of the values must be a floating point. IE:

double b = 3.0 / 4.0;

This would now work as you expect (b would be 0.75 instead of zero)
Never mind, I figured it out. I didn't use the following format:

1
2
3
4

double b = 3.0 / 4.0 * 6.0;
double e = 2.0 / 3.0 * 3.0 / 3.0;

I left off the .0 at the ends of the numerical values.

I'm bit confused though. Aren't values that have been declared as floating point or double supposed to output fractional values regardless of not having .0 attached to the original value? I thought C++ did that for you automatically.



@ albatross && Disch:

Thank both of you for your help. You replied as I was writing my last post telling everyone I found my problem lol.

As for the floating point (.0), I thought C++ did that automatically for you. I could have sworn I read that somewhere.

Thanks again for your help!
Last edited on
I've been there man... I had my program output tons and tons of garbage values before realizing that C++ does NOT initialize them with automatic decimals... And i could have sworn i had read that C++ does just that.

You know what they say, live and learn :)
lol AngelHoof.

Live and learn is definitely a rule we all have to live by even if learning takes a whole lot'a living lol.
Topic archived. No new replies allowed.