Avatar of ShaPaulraj
ShaPaulraj
 asked on

checking range in C code

Hi, Would like to check for a value which lies in the range.
For example, say I've got a value +5 in var. Also in another variable a value of 2. These two values can be negative also. like inplace of +5, replace it with -5. And the corresponding second variable -7.
Basically want to check for alram conditons using c code.
incase if I'm targetting for +5, and the value obtained is say +2, then it's an alarm condition.
In the same way if I target -5, and i get -7 then it;s an alarm.
Want to sepcify the range for generating this alarm.
For exmaple , +/- 0.9 onthe sacle.
If the obtained value is betwwen this slab, then no alarm. otherwise it needs to be flaged.
Could anyone suggest a good logic for this?

Thanks
Sha
C

Avatar of undefined
Last Comment
Infinity08

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Infinity08

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ShaPaulraj

ASKER
Wnat to check whether the targetted valus lies in the range of accuracy.
                 -------------------------------------------
                -5.9              -5                                -4.1
 
             ----------------------------------------------
            4.1                 5                              5.9

Any value needs to be computed. ie signed values(both - and +)
Example:

Target = 5
Obtained value = 2
This does not lie in the scale of +/- 0.9 scale of taget. So needs to be flaged.

another example :
target = -5
obtained valu = -7
This also needs to be flaged as it doesn't come under the slab.
hope i'm clear on my query...
Infinity08

>> Wnat to check whether the targetted valus lies in the range of accuracy.

That's what the code I posted does ;)


>> Any value needs to be computed. ie signed values(both - and +)

It works for both ... Try it out :)
ShaPaulraj

ASKER
No ..it's not functioning the way it's expected to.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Infinity08

>> No ..it's not functioning the way it's expected to.

Can you show the code you used to test it ?
ShaPaulraj

ASKER
double target=-5;
double obtained =-7;
doubel prec=0.9;

if(fabs(obtained - target) > prec)
{
  printf("alarm\n");
}
else
{
  printf("no alarm\n");
}
Infinity08

And ? Wasn't the output "alarm\n" ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ShaPaulraj

ASKER
no. basically this needs to look for the range based on the target.
if the obtained value is not in the range of -5.9 to -4.1 for the targetted value of -5, then it's an alarm condition.
Infinity08

>> if the obtained value is not in the range of -5.9 to -4.1 for the targetted value of -5, then it's an alarm condition.

And that's exactly what the code will do. I assume there's something else wrong. Can you show the complete code you're using ?

I noticed a typo in doubel versus double, so I assume the code you posted isn't actually the code you were using.


Note that the code below (just added a main around the code you posted and fixed the typo) works perfectly (just tested it if you want to know) :
#include <stdio.h>
#include <math.h>
 
int main(void) {
  double target=-5;
  double obtained =-7;
  double prec=0.9;
 
  if(fabs(obtained - target) > prec)
  {
    printf("alarm\n");
  }
  else
  {
    printf("no alarm\n");
  }
 
  return 0;
}

Open in new window

ShaPaulraj

ASKER
This code is not functioning consistantly.
I've attached few of the snap shots and the code which i use.
let me know why this is happening.
disc1.JPG
main1.txt
disc.JPG
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Infinity08

Looks fine to me. You're testing right on the edge of the range. Doubles are not as precise as that, so sometimes it will see it as an alarm, sometimes not, depending on whether 14.1 is really 14.10000001 or 13.999999999
ShaPaulraj

ASKER
Will the corner cases be handled if i change the data type to integer?
ShaPaulraj

ASKER
sorry. not as integer. but as aregualr float value instead of double.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Infinity08

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.