Pages: [1]   Go Down
 Author Topic: Simple problem with floating program  (Read 155 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 28
 « on: February 13, 2013, 08:02:03 pm » Bigger Smaller Reset

Code:
/*
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.
 Logged

Rome, Italy
Offline
Sr. Member
Karma: 20
Posts: 442
 « Reply #1 on: February 13, 2013, 08:56:39 pm » Bigger Smaller Reset

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.
 Logged

Offline
God Member
Karma: 12
Posts: 898
 « Reply #2 on: February 13, 2013, 09:38:49 pm » Bigger Smaller Reset

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:
Code:
boolean almostEqual(float a, float b) {
const float DELTA = .00001; // max difference to be almost equal
return fabs(a - b) <= DELTA;
}

Pete
 Logged

Offline
Newbie
Karma: 0
Posts: 28
 « Reply #3 on: February 13, 2013, 10:15:46 pm » Bigger Smaller Reset

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.
 Logged

Offline
God Member
Karma: 12
Posts: 898
 « Reply #4 on: February 13, 2013, 10:27:57 pm » Bigger Smaller Reset

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.
Code:
/*
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
 Logged

Offline
Newbie
Karma: 0
Posts: 28
 « Reply #5 on: February 13, 2013, 10:39:35 pm » Bigger Smaller Reset

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.
 Logged

 Pages: [1]   Go Up