473,383 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

generic data range monitor

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;
}

Nov 14 '05 #1
6 1614
Lo*****@hotmail.com wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Lo*****@hotmail.com wrote:
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
Nov 14 '05 #3
Lo*****@hotmail.com wrote:

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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #4
Je***********@physik.fu-berlin.de wrote:
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
Nov 14 '05 #5
Je***********@physik.fu-berlin.de <Je***********@physik.fu-berlin.de> wrote:
Lo*****@hotmail.com wrote:
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;


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.

--
With sufficient thrust, pigs fly just fine. However, this is
not necessarily a good idea. It is hard to be sure where they
are going to land, and it could be dangerous sitting under them
as they fly overhead. -- RFC 1925
Nov 14 '05 #6
Jesse Meyer <me**************@ideaone.net> wrote:
Je***********@physik.fu-berlin.de <Je***********@physik.fu-berlin.de> wrote:
Lo*****@hotmail.com wrote:
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;

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.


Obviously it was me that needed more coffee when I wrote that;-)

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
19
by: Nafai | last post by:
Hi I want to write a function which erases al the repeated elements in a range. How should be the prototype? template <class Iterator> void eraseRepeated(Iterator begin, Iterator end); ...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
14
by: James Wong | last post by:
Hi everybody, I'm facing a serious trouble relating to GDI+ generic error. The error message is "A Generic error occured in GDI+" and the following information is stored in Excepton object:...
1
by: Anthony Paul | last post by:
Hello everyone! Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about...
15
by: Anthony Paul | last post by:
Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about creating the following...
1
by: Noah Roberts | last post by:
A while back I posted a question either here or to the boost user list about how to iterate through a vector of strings and perform a lexical cast on them into elements of a tuple. I was working...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.