AND OR inside IF condition

Hi all!

I have a silly dumb question but it might help others.
I'm using an IF condition to evaluate some values. For example this:

if (voltage >= 4.77 && voltage <= 4.81) {
...
}

The problem comes when i want to add a new condition. For example:

if (voltage >= 4.77 && voltage <= 4.81 && voltage == 4.82) {
...
}

That doesn't work as expected and brokes the inside IF code.

How can i manage several values inside the IF condition so i can evaluate them?.
Should i use OR instead of AND?.

Regards!

These are mutually exclusive

1 Like

sorry what that is mean?

It's impossible for these two conditions to be true simultaneously:

Therefor, the "AND" of the two will always fail.

1 Like

4.82 is bigger than 4.81. You can't have less than 4.81 and equal to 4.82.

Also, you should never attempt to test floating point values for equality.

1 Like

this is impossible condition

kind of a silly example..
but you could OR that part in..

if ( (voltage >= 4.77 && voltage <= 4.81) || ( voltage == 4.82) ){

have fun.. ~q

Ok, to understand it. Why did you use an OR between those two voltage results?.

To make it simply, i need to evaluate some conditions because i have a few voltage results and everyone should post a message into a LCD screen.

Regards!

Ok, why not?.
Should I transform float results into integer numbers?.

Regards!

using logical and all must evaluate true..
the or is used because one of your statements will never be true when the others are..
with or this side true OR this side true, then it happens..
will also happen if both side evaluate true but kind of impossible..

check out.. Logical Truth Tables

good luck.. ~q

1 Like

Because floating point numbers are represented using a finite number of bits but they purport to represent values from the infinite field of real numbers. Thus they are only exact representations for an extremely limited and finite set of numbers. For everything else they are only approximations.

No. The standard technique is to test that the variable's value lies within a small band centered around the point of interest.

1 Like

Ok, i get it.

Using this statement:

((voltage >= 4.82 && voltage <= 4.83) || (voltage == 4.85))

When the value reach 4.85 it doesn't work so i assume that part is false while 4.82 and 4.83 are true.
Kinda weird, something is making it not working propperly.

Don't test floating point numbers for equality.

ok, and how could i test that?

Decide how much error you can tolerate first, and test for a narrow range of values.

See Post #12:

Ok, you mean something like:

((voltage >= 4.82 && voltage <= 4.83) || (voltage >= 4.87 voltage <= 4.89))

The values 4.85 and 4.86 are evaluated for another condition.

Also seen is the use of the absolute value function, viz:

  if (abs(voltage - 375) > 5) {   // if voltage is between 370 and 380


  }

Use whichever is clear to you. Either method will be recognized for what you are doing by most readers.

abs() uses integers. Avoid floating point if you can, but use fabs() in the same pattern if you are talking about floats.

a7

either like already mentioned test for bandwith or use integer math

int i = voltage * 100;
if (i == 482)