I have some questions about the conditions after "if"

 ``1234567`` ``````so when I typed: float num1,num2,num3; num1=1.3; num2=3.5; num3=-0.7; if (num1==1.3 && num2==3.5) { cout<<"True.\n"; } else { cout<<"False.\n"; } in the main function, it gives me "false" and when I changed the "if" conditions to (num1=1.3 && num2=3.5), the error message says "non-lvalue in assignment" then I changed the conditions to ((num1=1.3) && (num2=3.5)),it finally gives the right answer, "true."``````

What are the differences between the three conditions I have tried?

Thank you!
Well, the first one will return false because floating-point digits tend to be unreliable when it comes to using the comparison operator (==). Basically, it doesn't treat it like 1.3, but 1.30000000000000000000000001 or something along those lines. That is why the first case returns false. The second has an error because the and operator (&&) takes precedence over the assignment operator (=). Essentially, it is comparing the wrong values, and just not functioning. Hence the error. The third case works because you're not checking anything. You assign the value 1.3 to num1, assign the value of 3.5 to num2, then check whether both are a non-zero value, which is true.

What you should do here is not use the comparison operator with floating-point numbers. Rather, use greater-than and less-than to see whether the value is within a certain range of the value you're getting at.
I tried your program and something seems a bit peculiar with num1, which when given a fractional number would always evaluate to false, but if given/assigned a whole number would evaluate to true. (N.B. : In conditions, you must use "==" instead of '=')

Notice these programs:

 ``12345678910111213141516`` ``````#include using namespace std; int main () { float num1,num2,num3; num1=1.3; num2=3.5; num3=-0.7; if (num1==1.3) cout<<"True"; else cout<<"False"; cin.get(); return 0; }``````

This program would output:
 `False`

But this program:
 ``12345678910111213141516`` ``````#include using namespace std; int main () { float num1,num2,num3; num1=1; num2=3.5; num3=-0.7; if (num1==1) cout<<"True"; else cout<<"False"; cin.get(); return 0; }``````

would output:
 `True`

Look at these 2 programs:
 ``12345678910111213141516`` ``````#include using namespace std; int main () { float num1,num2,num3; num1=1; num2=3.5; num3=-0.7; if (num1==1 && num2==3.5) cout<<"True"; else cout<<"False"; cin.get(); return 0; }``````

This would output:
 `True`

But, this program:
 ``12345678910111213141516`` ``````#include using namespace std; int main () { float num1,num2,num3; num1=1.3; num2=3.5; num3=-0.7; if (num1==1.3 && num2==3.5) cout<<"True"; else cout<<"False"; cin.get(); return 0; }``````

would output:
 `False`

I think you should change float into double. It might help, since double has a wider range of values.
Last edited on
Doesn't matter if it's a floatin' or a doublin'.
http://learningcppisfun.blogspot.com/2010/04/comparing-floating-point-numbers.html

Basically, you need to create an "epsilon" or an "uncertainty (dy)" that gives a range of values that the program should accept.
Like if you only care about the first five digits after the decimal point or the calculations dictate that only five of the digits after the decimal point are significant, you can have a range of:
[n-0.0001,n+0.0001]
that your resultant number must always fall within to be correct.

Directly comparing two floating point numbers won't work for 99% of the time.

Edit:
Going to go ahead and give Ispil some credit as he/she basically said what I said in his/her last paragraph.
Last edited on
Topic archived. No new replies allowed.