tags:

views:

92

answers:

3

I have this code in my program: (I included the cout statements for debugging purposes)

cout << "b: " << b << "\na: " << a;
constant[0] = (-b / (2 * a));
cout << "\nconstant: " << constant[0] << endl;

The output I get is:

b: -4
a: 3
constant: 0

Whereas I'm trying to make constant[0] equal to -(-4)/(2 * 3), or 0.6666... What am I doing wrong with the formula I put there?

+1  A: 

Your constant 2 is an int, make it 2.0.

Make sure your variables a and b are doubles or floats too?

djna
+2  A: 

Is constant an integer? Are a and b integers?

cout << "b: " << b << "\na: " << a;
constant[0] = (-b / (2.0 * a));
cout << "\nconstant: " << constant[0] << endl;

Integer division and/or variable types are the issue.

Xorlev
+9  A: 

Undoubtably you have a and b defined as integers, causing your whole formula to be done in integer math. Either define them as floating point numbers or do something like this:

constant[0] = (-b / (2.0 * a));

which forces the math to be done in floating point.

Gabe
thanks! actually, constant[0] was the variable that was an integer, but your answer helped me solve the problem :)
wrongusername
Oh, I see. The value was already being calculated as .66667, but was being truncated to an integer (0) on assignment.
Gabe