Hey everyone,

I was wondering if someone could explain to me the following bit of code. I understand != in logic but i dont know how to read it like this:

``````float y = 0.01, x = 0.005, z;

z = 2 * x + (y != 0.001);``````

Thanks

4
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by Aia

Boolean operators evaluate to 0 (false) or 1 (true).

So depending on the outcome of that, you have
z = 2 * x + (0);
z = 2 * x + (1);

Actually, in C 0 always stands for false, but any other number stands for true (not only 1). I'm not sure about the other way, it is possible that for this direction C automatically returns 1 (instead of any number different from 0).

However, knowing C it might be that the expression (y != 0.001) returns 0 when false, but 0.001 when true?

You can just test the code by simply outputting what (y != 0.001) returns and work from there.

Black Box

Oo got ya. Thanks

Oo got ya. Thanks

Careful, checking for inequality using float is not an exact science due to the way a real number is stored in memory.
Most like it ( y != 0.01 ) will return a 1.000000 even when it should have return a 0.000000 if you use a float type and not a double.