Go Down

Topic: Simple problem with floating program (Read 977 times) previous topic - next topic

Code: [Select]
/*
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.

spatula

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.

el_supremo

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: [Select]

  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.

el_supremo

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: [Select]

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy