elseif (D=0)
= is an assignment operator.
== is a comparison operator. You want to use this one.
Also the code works fine anyways. Im not sure if the calculations are right since Im not gonna check that, but if you input a = 2, b = 15, c = 2, it enters the first if statement.
Be careful with equality with doubles: elseif (D== 0.0) will almost certainly be false, because of the way doubles are stored.
To check for equality to zero, see if the absolute value is less than some arbitrary precision value:
1 2 3 4 5 6 7
constdouble PRECISION = 0.001;
constdouble a = 1.0 - 10.0 * 0.1 ; // not equal to zero, maybe +/-3e-16
if (std::abs(a) < PRECISION ) { // near enough to zero for us
// do stuff
}
I always put the decimal point and at least 1 figure after the point, so it is obviously a double.
Be careful with equality with doubles: elseif (D== 0.0) will almost certainly be false, because of the way doubles are stored.
Be careful when comparing double types. The computer will store very precise values. (eg 0.000012 != 0.0 even though in some cases it would be near enough to 0 that you'd prefer it to be considered 0).
To check for equality to zero, see if the absolute value is less than some arbitrary precision value:
One way to do this is set a constant value that is the closest value you don't want considered to be zero. (refer the example TheIdeasMan provided)
I always put the decimal point and at least 1 figure after the point, so it is obviously a double.
To make reading your source code easier later on down the road, or when someone else might need to read your code, it's a good practice to always add a decimal point, even when it's not explicitly required, to double and float types.
Just to be pedantic, I put figures before and after the decimal as well as in 1.0 not 1. or 0.3 not .3
The error in floating point types (float or double) is usually in the last decimal place which that type can handle, so 3e-16 for double say, and 2e-6 say for float .