开发者

How do I fix this formula error?

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: " 开发者_JAVA技巧<< 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?


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.


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.


Your constant 2 is an int, make it 2.0.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜