How to compare two float variable upto 6 precision.

Hi

I am trying to compare two float value(upto 6 precision).Even if 6th precision is different then it should say not equal.

I google and found some solution but none of it worked for me as I am considering upto 6th precision.
Solution found till now is:

 long compareValue= value * 1000000;
    
  
  if (compareValue != oldCompareValue) {

But when i assign value*1000000 to long variable then it round off the last 5th and 6th decimal value.
And make 2 close by float equal.

Thanks.

Check out this page for help:

http://www.arduino.cc/en/Reference/Float

I’m also wondering if there’s a command that can specifiy the amount of decimal places such as nf()
in processing:

IE:

nf(num, digits)
nf(num, left, right)

I’m sure there is something, a quick Google search may help you.

Give up trying to compare floats. It won't work because floats cannot be expressed exactly in binary and multiplying by 1000000 will not solve the problem.

What you can do in most case, however, is to work in integers of a suitable size and divide them down to floating point numbers when displayed. What is the source of your floating point numbers that you want to compare.

A floating point value only has 6 to 7 digits of precision. If you are talking about a value greater than 10, it doesn’t have 6 decimal places of accuracy.

You can easily tell if two floating point values are nearly equal:

   if(abs(float1 - float2) < 0.000001)
   {
       // Nearly equal

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

(PaulS - your solution only works if the numbers are close to 1. If you are comparing numbers on the order of 1e-12 (i.e., picofarad capacitors), it will fail badly. )

(PaulS - your solution only works if the numbers are close to 1. If you are comparing numbers on the order of 1e-12 (i.e., picofarad capacitors), it will fail badly. )

In the absence of any real information, I don't think that that is an unreasonable assumption. But, point taken.

OP's attempt won't work any better, though.

His attempt is not even wrong.

PaulS:
You can easily tell if two floating point values are nearly equal:

   if(abs(float1 - float2) < 0.000001)

{
      // Nearly equal

Nearly, but not quite.

  if (float1 == 0.0 ? float2 == 0.0 :
      abs ((float1 - float2) / float1) < 0.000001)  // normalized error
   {
       // Nearly equal

[/quote]