|
|
|
|
vone = (vtotal*(((double)ptotal/(double)100)-((double)ptwo/(double)100)))/(((double)pone/(double)100)-((double)ptwo/(double)100));
This: 1 2 int pone = 9; double answer = pone/100; does integer division. answer will be zero in this case. Actually, answer will be zero if pone is in the range [0,99]. For integer division, imagine you're doing long division but you don't continue to solve for the decimal values. You will get an answer and a remainder and integer division returns the answer. For example, 6/3 would give 2 with remainder 0, 5/3 would give 1 with remainder 2, and 1/3 would give 0 with remainder 1. The modulus operator, %, would give you the remainder of the integer division between the operands. Thus, if pone and ptwo are less than 100, you are indeed dividing by zero. You need to cast the operands to double (or float or whatever you want) to tell C++ to treat the division as a non-integer division. e.g. vone = (vtotal*(((double)ptotal/(double)100)-((double)ptwo/(double)100)))/(((double)pone/(double)100)-((double)ptwo/(double)100)); |
int
, unless it has more information to go by.
|
|
int
. OK, '5'...Um I don't know what type you are so I'm just going to assume you're an int
."
|
|
float
. OK, '(float)5'...OK then, you want me to make '5' a float
, huh. Gotcha".int
s, then this is bad news if you want decimal values! Basically, it gets rid of everything after the decimal point.