Pages: [1]   Go Down
Author Topic: Simple problem with floating program  (Read 628 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Edison Member
*
Karma: 35
Posts: 1429
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Edison Member
*
Karma: 35
Posts: 1429
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Jump to: