 # How to compare float variable?

Hi,,

I'm trying to compare using float variable but its not working..

I'm usinfg floatr because I need decimal numers..

like..

float myvar; void loop() { myvar = myvar + 0.1; if(myvar ==2.0) { Serial.print("Got it"); rerturn; } }

I can see on Serial monitor the value passing by 2.0 and doesn't work at all. I have tried to use double as well but no sucess..

Does anybody know how to solve it?

Thank you very much

Rodrigo

I can see on Serial monitor the value passing by 2.0 and doesn't work at all.

Try printing more decimal places before the comparison`Serial.println(myvar, 8);`does myval ever equal 2.0 ?

hi..

Well. you are right.

Thats weird. because I'm only add 0.1 and its increasing by some weird numbers..

Is there a way to just add 0.1 to a float variable?

0.10000001 0.20000000 0.30000000 0.40000000 0.50000000 0.60000000 0.70000004 0.80000009 0.90000009 1.00000011 1.10000014 1.20000016 1.30000019 1.40000019

http://c-faq.com/fp/fpequal.html

In this case, an epsilon of .001 should be fine.

You never ever use == with floats as it is very very unlikely to work try comparing 8.0/4.0 and 4.0/2.0

Mark

It is best to count and compare with integers and convert to float.

``````myvar++

if (myvar == 20)
{
serial.printline((float)myvar/10.0)
}
``````

The problem is that .1 is not a nice number in binary, which is used for floating point addition. (Much like 1/3 is not a nice number in decimals.)

The thing people forget is that a float is only an approximation to a number, it is rarely the exact number you want it to be because it is a binary fraction. Just like decimal numbers you can have recurring numbers. For example you can not write down an exact decimal value for one third, there are many decimal values you can't express as a binary fraction or floating point number.

The best way to compair floats fro equality is to subtract the number you want to test from the value you want to compair it with. Take the absoloute value of the result and test if that is greater than a small number.

If you read the page I linked to, Grumpy_Mike, your solution is not the best idea. It is better to scale the "small" number to the relative size of the values being compared.

Grumpy_Mike:
The best way to compair floats fro equality is to subtract the number you want to take from the value you want to compair it with. Take the absoloute value of the result and test if that is greater than a small number.

Hi!!

First of you, thank you and everybody that answered my question!!

Just a question…

What exactly you meant by “The best way to compair floats fro equality is to subtract the number you want to take from the value you want to compair it with.”

How Could I Substract?

ie. I want to compare my 2.0 with a float

Read the page I linked to and all will be explained. You compare your number to 2.0.

How Could I Substract?

ie. I want to compare my 2.0 with a float

``````If( fabs(myValue - 2.0) < 0.00001) // then say it is equal.
``````

Keath, never said it was the best way but it is the way I have always used. I will look at your page soon.

KeithRB: Read the page I linked to and all will be explained. You compare your number to 2.0.

Read it but it is a bit biased against the method I use because it assumes that you use it blindly without thinking about what you are doing. What the "approve" method does is do about half of that thinking for you.

holmes4: You never ever use == with floats as it is very very unlikely to work try comparing 8.0/4.0 and 4.0/2.0

Mark

Those numbers are probably some of the best candidates for equality comparison to work correctly!