Quote:
Originally Posted by ivanraso
I dont why i cannot compare two numbers. I can do it in Java,C,etc..
Maybe if i create the x value in float.?¿??¿ I am confused.
|
Yes, you can compare two numbers as either doubles or floats. But you probably won't get the result you want. Floats and doubles are approximations. The "==" comparison checks that the two numbers are exactly the same. The problem is they won't be exactly the same, unless they were set from the same number originally, such as:
Code:
float x = 34.5;
float y = 34.5;
if(x == y) {...}
But this may not work:
Code:
float x = 34.5;
if(x == 34.5) {...}
That is because in the "if" statement, the x is promoted to double before comparing with the (double) constant 34.5. The double constant 34.5 retains all the precision of a double, but some of that precision may have been sacrificed when the assignment to x was made. So x is just an approximation of 34.5. Then when x is promoted to double, it may not be exactly the same as the double constant 34.5.
When floats and doubles represent real measurements in the physical world (as they do in the case of the accelerometer) then comparisons for equality are usually inappropriate for the application because they will hardly ever be true.
Robert Scott
Ypsilanti, Michigan