 # Simple problem with floating program

``````/*
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;
}
}
``````

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.

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:

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

Pete

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.

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.

``````/*
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

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.

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”???

can u tell me how does this code works?

`````` 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

``````   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.)

Thank you so much! nice to meet u!

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

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

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

Or...

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