# Simple problem with floating program

**URL:** <https://forum.arduino.cc/t/simple-problem-with-floating-program/145332>\
**Category:** Programming\
**Created:** [February 14, 2013, 1:02am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332 "2013-02-14T01:02:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 14, 2013, 1:02am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/1 "2013-02-14T01:02:03Z")

</div>

```cpp
/*
floating-point example
*/

float value = 1.1;

void setup() {
  Serial.begin(9600);
}

void loop() {
  value = value - 0.1;
  if (value == 0) {
    Serial.println("the value is exactly zero");
  }
  else if (almostEqual(value, 0)) {           <<<<<<--------------------- almostEqual is not declared in this scope.
    Serial.print("the value");                                                    I'm confused as to what I need to do.
    Serial.print(value,7); // print to 7 decimal places
    Serial.println(" is almost to equal");
  }
  else {
    Serial.println (value);
  }
  delay(100);
  //returns true if the difference between a and b is small
  // set value of DELTA to the maximum difference considered to be equal
  boolean almostEqual(float a, float b) {
    const float DELTA = .00001; // max difference to be almost equal
    if (a == 0) return fabs(b) <= DELTA;
    if (b == 0) return fabs(a) <= DELTA;
    return fabs((a - b) / max(fabs(a), fabs(b))) <= DELTA;
  }
}

```

Thank you for your time.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 14, 2013, 1:56am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/2 "2013-02-14T01:56:39Z")

</div>

You defined almostEqual() within loop(); you need to move the definition outside loop(). Just move one of the closing curly braces a few lines up and it should compile.

---

<div class="post-metadata">

**Author:** ![el\_supremo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/el_supremo/32/214787_2.png) [@el\_supremo](https://forum.arduino.cc/u/el_supremo)\
**Post date:** [February 14, 2013, 2:38am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/3 "2013-02-14T02:38:49Z")

</div>

Your almostEqual function doesn't seem to work if neither a nor b is zero.  
For example if a=1000000 and b=1000001 then their difference is greater than DELTA.  
But your function calculates (a-b), which is one, divided by the greater of a and b. The result is 0.000000999 which is less than DELTA.  
Is DELTA an absolute difference or a percentage/relative difference?  
If it is absolute, the entire function can be this:

```cpp
  boolean almostEqual(float a, float b) {
    const float DELTA = .00001; // max difference to be almost equal
    return fabs(a - b) <= DELTA;
  }

```

Pete

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 14, 2013, 3:15am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/4 "2013-02-14T03:15:46Z")

</div>

I tried both of them, yet I'm still getting the same error indicating the variable almostEqual is not declared in my void loop scope. However, when I do declare as a variable by

void loop() {  
boolean almostEqual;  
cont...

before the if statement begins, it says that I cannot use it within that function.

---

<div class="post-metadata">

**Author:** ![el\_supremo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/el_supremo/32/214787_2.png) [@el\_supremo](https://forum.arduino.cc/u/el_supremo)\
**Post date:** [February 14, 2013, 3:27am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/5 "2013-02-14T03:27:57Z")

</div>

almostEqual is a function and in C/C++ you cannot declare/define a function inside another one. You must move the entire almostEqual function outside the loop function.

```cpp
/*
floating-point example
*/

float value = 1.1;

void setup() {
  Serial.begin(9600);
}

void loop() {
  value = value - 0.1;
  if (value == 0) {
    Serial.println("the value is exactly zero");
  }
  else if (almostEqual(value, 0)) {   
    Serial.print("the value");     
    Serial.print(value,7); // print to 7 decimal places
    Serial.println(" is almost to equal");
  }
  else {
    Serial.println (value);
  }
  delay(100);
}
//returns true if the difference between a and b is small
// set value of DELTA to the maximum difference considered to be equal
boolean almostEqual(float a, float b) {
  const float DELTA = .00001; // max difference to be almost equal
  if (a == 0) return fabs(b) <= DELTA;
  if (b == 0) return fabs(a) <= DELTA;
  return fabs((a - b) / max(fabs(a), fabs(b))) <= DELTA;
}

```

Pete

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 14, 2013, 3:39am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/6 "2013-02-14T03:39:35Z")

</div>

Oh, I understand. I didnt realized that was the issue. I'll remember that you cannot declare/define a function within another.

Thank you very much Pete.

---

<div class="post-metadata">

**Author:** ![Dtink](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@Dtink](https://forum.arduino.cc/u/Dtink)\
**Post date:** [October 26, 2017, 1:09am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/7 "2017-10-26T01:09:45Z")

</div>

Hi bro! I still don't understand this following code:  
if (a == 0) return fabs(b) \<= DELTA;  
if (b == 0) return fabs(a) \<= DELTA;  
return fabs((a - b) / max(fabs(a), fabs(b))) \<= DELTA;  
can u tell me how does this code works? Place "\<= DELTA"????

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [October 26, 2017, 2:10am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/8 "2017-10-26T02:10:07Z")

</div>

> can u tell me how does this code works?
> 
> ```arduino
> return fabs((a - b) / max(fabs(a), fabs(b))) <= DELTA
> 
> ```

If two numbers and and b are exactly equal, then (a-b) will be zero.  
If they are only "almost equal", than (a-b) will be a small value (either positive or negative), where "small" means "small compared to a and b: if a=1.0000000e12 and b=1.000001e12, you probably want them to be considered "almost equal" even though the difference is about a million.  
fabs(a-b) is the magnitude of the difference, regardless of whether a or b is bigger.  
dividing by max(...) gives you the magnitude compared to the values...  
the comparison expressions like "fabs(a-b)/max(...) \> DELTA" have boolean results, and they can be returned directly instead of using code like

```arduino
   if (x) return true;
      else return false

```

Grr. "EPSILON" would have been more traditional than DELTA, I think.  
(And it sure seems like there should be a way to do this with far fewer actual floating point operations, given internal knowledge of the floating point formats. But that would be even less comprehensible.)

---

<div class="post-metadata">

**Author:** ![Dtink](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@Dtink](https://forum.arduino.cc/u/Dtink)\
**Post date:** [October 26, 2017, 7:06am UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/9 "2017-10-26T07:06:46Z")

</div>

Thank you so much! nice to meet u!

---

<div class="post-metadata">

**Author:** ![khaledsankar](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@khaledsankar](https://forum.arduino.cc/u/khaledsankar)\
**Post date:** [October 28, 2018, 6:23pm UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/10 "2018-10-28T18:23:58Z")

</div>

> westfw:  
> If two numbers and and b are exactly equal, then (a-b) will be zero.  
> If they are only "almost equal", than (a-b) will be a small value (either positive or negative), where "small" means "small compared to a and b: if a=1.0000000e12 and b=1.000001e12, you probably want them to be considered "almost equal" even though the difference is about a million.  
> fabs(a-b) is the magnitude of the difference, regardless of whether a or b is bigger.  
> dividing by max(...) gives you the magnitude compared to the values...  
> the comparison expressions like "fabs(a-b)/max(...) \> DELTA" have boolean results, and they can be returned directly instead of using code like

The division part and max value i still dont get it any more detailed info  
like where is te max(...) considered from

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [October 28, 2018, 9:02pm UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/11 "2018-10-28T21:02:42Z")

</div>

Maybe that example would be better-written as...

```cpp
fabs(a-b)/max(a,b) > DELTA

```

Or...

```cpp
fabs(a-b)/max(fabs(a),fabs(b)) > DELTA

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:05pm UTC](https://forum.arduino.cc/t/simple-problem-with-floating-program/145332/12 "2021-05-05T20:05:05Z")

</div>

Closed on May 5, 2021, 8:05pm UTC


