Go Down

Topic: variable equal one but don't equal one (Read 680 times) previous topic - next topic

TTU.ABD


Also in your case, you are not comparing a floating point number to a floating point number, you are comparing a floating point number with the integer 1. You should use:
cursf == 1.0

I tried it [ if(cursf==1.0) ], but the results are the same.

GoForSmoke

Floating point is not exact. The more math you perform with it, the less exact it gets.

And btw, Arduino float and double are the same. 32-bit IEEE float.

As useless as it has been before I will mention to use fixed-point or choose smaller units and use integers. If I work in micro-meters (the unit, not the measuring tool) to achieve results in fractional meters then I will have 6 places accuracy and only need to add the decimal point for final output. A 32-bit signed integer (long) is good for 9 places.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

TTU.ABD


Floating point is not exact. The more math you perform with it, the less exact it gets.

I tried [ if(round(cursf)==1) ] , the result was right.
THANX.

JarkkoL

floats don't have infinite precision, thus when you write float x=0.01f; it's not exactly 0.01 but more like 0.0099999998f. It's generally not good idea to compare equality of floats (there are exception, but not in this case).

holmes4

instead of if(cursf==1) use if( (int)(cursf+0.5)==1)

Welcome to the world of floats ints and casts. Here's how it should work cursf is a float so the value will not be equal to 1 ever!. You should never ask if a float is equal to any thing ever. Greater or less than but never equal to.

Convert (casting) a float to an int all ways rounds DOWN never up or off so adding the 0.5 and then casting to an int will give you an int of 1 if cursf is between 0.5 and 1.4999.

Mark  

Go Up