generic data range monitor

L

LordHog

Hello all,

My lead wants to implement a data range monitor for a project that we
are coding. Basically it performs a boundry checking that will take
three parameters. I am/was trying to implement a generic data range
monitor, but it doesn't quite work. I am trying to create one method
that will accept any type of parameter and use those values whether
they be int, unsigned int, float, double etc.... I am not sure if this
can be done within the c language, but I was hoping someone might point
me in the correct direction. Thanks.

Mark



int drm( void*, void*, void*);

main( ) {
int data = 2,
lower = 5,
upper = 15;

unsigned int data2 = 1;

float data1 = 2.7f;

if ( drm( (void*)data, (void*)lower, (void*)upper ) ) {
printf( "Within bounds\n" );
} else {
printf( "Out of bounds\n" );
}
return 0;
}

bool drm( void* data, void* lowerLimit, void* upperLimit) {
if( data < lowerLimit || data > upperLimit )
return 0;
else
return 1;
}
 
J

Jens.Toerring

My lead wants to implement a data range monitor for a project that we
are coding. Basically it performs a boundry checking that will take
three parameters. I am/was trying to implement a generic data range
monitor, but it doesn't quite work. I am trying to create one method
that will accept any type of parameter and use those values whether
they be int, unsigned int, float, double etc.... I am not sure if this
can be done within the c language, but I was hoping someone might point
me in the correct direction. Thanks.

What about

int drm ( double data, double ll, double ul ) {
return data < ul || data > ul;
}

I guess that's as far as "type-agnostic" you can get, since data must
have a type to be compared (using void isn't an option since you then
don't know what type the data have). As long as 'double' is the type
with the largest range (unless your system has 'long double', then you
should use that) it could be used - on invocation of the function
(given that the declaration of the function is in scope) the arguments
all get converted to double. If your limits are always ints, you could
of course also use

int drm ( double data, int ll, int ul ) {
return data < ul || data > ul;
}
Regards, Jens
 
D

David Resnick

Hello all,

My lead wants to implement a data range monitor for a project that we
are coding. Basically it performs a boundry checking that will take
three parameters. I am/was trying to implement a generic data range
monitor, but it doesn't quite work. I am trying to create one method
that will accept any type of parameter and use those values whether
they be int, unsigned int, float, double etc.... I am not sure if this
can be done within the c language, but I was hoping someone might point
me in the correct direction. Thanks.

Mark



int drm( void*, void*, void*);

main( ) {
int data = 2,
lower = 5,
upper = 15;

unsigned int data2 = 1;

float data1 = 2.7f;

if ( drm( (void*)data, (void*)lower, (void*)upper ) ) {
printf( "Within bounds\n" );
} else {
printf( "Out of bounds\n" );
}
return 0;
}

bool drm( void* data, void* lowerLimit, void* upperLimit) {
if( data < lowerLimit || data > upperLimit )
return 0;
else
return 1;
}

A void* is a generic pointer, not a generic holder of arbitrary data.
Forgetting other issues (trap representations, etc, that I know little
about), a void* may well not have enough bits to hold all types, for
example long double or long long int. IMHO as soon as you are adding
casts to code you have a clue that you've made a bad choice (yes, there
are exceptions...).

You could have the arguments be 3 unions of all types you want supported
and a 4th argument that is an enumeration that says what the type is.
Or you could take 3 void* arguments which are pointers to the things
to be compared and a 4th argument that is again an enum explaining
the type.

Or you could use a macro along the lines of:
#define DRM(value, lower, upper) (((value) < lower) ? 0 : ((value) >
(upper)) ? 0 : 1)
With the usual caveat that DRM(value++, lower, upper) is a problem.

Or you could head over to C++ land which handles this with templates.

BTW, int main(void) is better (required in C99), and the bool return of
drm is C99 specific.

-David
 
C

CBFalconer

My lead wants to implement a data range monitor for a project that
we are coding. Basically it performs a boundry checking that will
take three parameters. I am/was trying to implement a generic data
range monitor, but it doesn't quite work. I am trying to create
one method that will accept any type of parameter and use those
values whether they be int, unsigned int, float, double etc.... I
am not sure if this can be done within the c language, but I was
hoping someone might point me in the correct direction. Thanks.

C doesn't have methods, it has functions. At any rate ...

You will have to write separate routines to deal with floating
point and integral arguments, and probably another for unsigned
arguments. Use the largest range available to you to pass the
arguments, and let the compiler generate the conversions on the
calls. i.e. never cast anything.

int drmi(long value, long min, long max)
{
return (value <= max) && (value >= min);
}

You can probably improve the efficiency by declaring it as static
inline. In any case the usage can be:

if (!drmi(v, bottom, top)) takecorrectiveaction(whatever);
else {
/* known suitable values */
}

For example, you could check for hex lower case chars with:

if (!drmi(ch, 'a', 'f')) ....

You can't use void*, since to dereference anything you have to know
its type.
 
C

Christian Kandeler

As long as 'double' is the type
with the largest range (unless your system has 'long double', then you
should use that)

If he is using a conforming implementation, there _must_ be a long double
data type.


Christian
 
J

Jesse Meyer

What about

int drm ( double data, double ll, double ul ) {
return data < ul || data > ul;

Perhaps its the high amount of blood in my caffiene stream today, but
why are you checking that data != ul?

ITYM
return data < ul && data > ll;

Or perhaps I need coffee.
 
L

LordHog

Thanks to everyone that has responded to my question. It has helped
greatly and allowed me to take a good direction that will appease our
goals and the certification process.

Mark....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top