Link to home
Start Free TrialLog in
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
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ShaPaulraj
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...
>> 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 :)
No ..it's not functioning the way it's expected to.
>> No ..it's not functioning the way it's expected to.

Can you show the code you used to test it ?
double target=-5;
double obtained =-7;
doubel prec=0.9;

if(fabs(obtained - target) > prec)
{
  printf("alarm\n");
}
else
{
  printf("no alarm\n");
}
And ? Wasn't the output "alarm\n" ?
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.
>> 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

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
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
Will the corner cases be handled if i change the data type to integer?
sorry. not as integer. but as aregualr float value instead of double.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial