473,386 Members | 2,050 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,386 software developers and data experts.

Can you approximate subranges in C?

CBFalconer <cb********@yahoo.com> writes:
It would be fairly easy to graft such a construct with:

enum subrangetype {MINVAL .. MAXVAL};

where MINVAL and MAXVAL are constant integral values.


Suppose you need an integer type "foo" that can represent MINVAL,
MAXVAL, and all values in between. Will the following type do?

typedef enum { foo_lo = MINVAL, foo_hi = MAXVAL } foo;
Nov 13 '05 #1
9 1920
In article <7w***************@sic.twinsun.com>
Paul Eggert <eg****@twinsun.com> writes:
Suppose you need an integer type "foo" that can represent MINVAL,
MAXVAL, and all values in between. Will the following type do?

typedef enum { foo_lo = MINVAL, foo_hi = MAXVAL } foo;


This might be fixed in C99 (and no doubt someone will post about
it if so), but in C89, at least, it is not guaranteed to work.
A hypothetical "evil" implementation might make the enumerated
type "foo" compatible with, e.g., unsigned char and thus give it
a maximum integral range of [0..255], even if MINVAL is -32767
and MAXVAL is 32767.

I doubt there are any compilers that do this, but I would be not
terribly surprised to find 16-bit-"int" compilers that make enum foo
compatible with plain (signed) int even when MINVAL is -2147483647
and MAXVAL is 2147483647, which is guaranteed to fit within plain
mlong.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #2
Chris Torek <no****@elf.eng.bsdi.com> writes:
In article <7w***************@sic.twinsun.com>
Paul Eggert <eg****@twinsun.com> writes:
Suppose you need an integer type "foo" that can represent MINVAL,
MAXVAL, and all values in between. Will the following type do?

typedef enum { foo_lo = MINVAL, foo_hi = MAXVAL } foo;


This might be fixed in C99 (and no doubt someone will post about
it if so)


It has been fixed, in that the type chosen must be capable of
representing all enumerated types; however, the constraint still
exists that all of the constant expressions enumerated must be
representable as an int, which means an implementation could find its
work easiest by simply always using an int, and warning (at least) if
an expression falls outside of that.

In any case (to OP), C isn't Ada; you wouldn't have the advantage of
being able to *constrain* the value to that range (if that's what you
wanted). At any rate, you're at least as well off employing int as
enum.

HTH,
-Micah
Nov 13 '05 #3

"Chris Torek" <no****@elf.eng.bsdi.com> wrote in message news:be**********@elf.eng.bsdi.com...
In article <7w***************@sic.twinsun.com>
Paul Eggert <eg****@twinsun.com> writes:
Suppose you need an integer type "foo" that can represent MINVAL,
MAXVAL, and all values in between. Will the following type do?

typedef enum { foo_lo = MINVAL, foo_hi = MAXVAL } foo;


This might be fixed in C99 (and no doubt someone will post about
it if so), but in C89, at least, it is not guaranteed to work.


Could you elaborate on what difference exactly you refer to here?
As I recall, there is only wording improvement between those
standards, C90+TCs and C99.
--
Jun, Woong (my******@hanmail.net)
Dept. of Physics, Univ. of Seoul

Nov 13 '05 #4

"Micah Cowan" <mi***@cowan.name> wrote in message news:m3************@localhost.localdomain...
"Jun Woong" <my******@hanmail.net> writes:
"Chris Torek" <no****@elf.eng.bsdi.com> wrote in message news:be**********@elf.eng.bsdi.com...
In article <7w***************@sic.twinsun.com>
Paul Eggert <eg****@twinsun.com> writes:
>Suppose you need an integer type "foo" that can represent MINVAL,
>MAXVAL, and all values in between. Will the following type do?
>
>typedef enum { foo_lo = MINVAL, foo_hi = MAXVAL } foo;

This might be fixed in C99 (and no doubt someone will post about
it if so), but in C89, at least, it is not guaranteed to work.


Could you elaborate on what difference exactly you refer to here?
As I recall, there is only wording improvement between those
standards, C90+TCs and C99.


From what I could read, C90 states only that enum be compatible with
an integer type; which specific type is implementation-defined. There
doesn't seem to be a requirement that it choose a type capable of
representing all enumerated values (which would still be a poor QoI,
of course, but still...). This was fixed in C99.


From the TC2 for C90:

In subclause 6.5.2.2, page 61, second paragraph of Semantics,
change:

Each enumerated type shall be compatible with an integer type;
the choice of type is implementation-defined.

to:

Each enumerated type shall be compatible with an integer type.
The choice of type is implementation-defined, but shall be capable
of representing the values of all the members of the enumeration.
--
Jun, Woong (my******@hanmail.net)
Dept. of Physics, Univ. of Seoul

Nov 13 '05 #5
Paul Eggert <eg****@twinsun.com> wrote:
CBFalconer <cb********@yahoo.com> writes:
It would be fairly easy to graft such a construct with:

enum subrangetype {MINVAL .. MAXVAL};

where MINVAL and MAXVAL are constant integral values.


Suppose you need an integer type "foo" that can represent MINVAL,
MAXVAL, and all values in between. Will the following type do?

typedef enum { foo_lo = MINVAL, foo_hi = MAXVAL } foo;


I don't think this makes much sense without Pascal style BUILT IN RANGE
CHECKING. Pascal has (optional) RANGE CHECKING for variables, which IIRC
incvludes range check for SUBRANGES.
Nov 13 '05 #6

"Paul Eggert" <eg****@twinsun.com> wrote in message news:7w************@sic.twinsun.com...
[...]

As an aside, the wording in C99 does not seem to specifically rule out
the following declaration:

enum { foo = INT_MAX, foo_plus_one };

though clearly this violates the intent of the standard, as
'foo_plus_one' is of type 'int'. Certainly no diagnostic is required
for this bogus declaration, though I think one ought to be.


I think that the intent is to still apply the constaint in the
subclause and require a diagnostic:

Each subsequent enumerator with no = defines its enumeration
constant as the value of the constant expression obtained by
~~~~~~~~~~~~~~~~~~~~~~~
adding 1 to the value of the previous enumeration constant.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

But, if you disagree, how about:

6.6p3 (constraints)

Each constant expression shall evaluate to a constant that is in
the range of representable values for its type.

?
--
Jun, Woong (my******@hanmail.net)
Dept. of Physics, Univ. of Seoul

Nov 13 '05 #7
In article <be*********@news.hananet.net>
Jun Woong <my******@hanmail.net> writes:
From the TC2 for C90 ...


Aha. You have an advantage over me: I have neither of the two TCs.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #8
"Jun Woong" <my******@hanmail.net> writes:
enum { foo = INT_MAX, foo_plus_one };
I think that the intent is to still apply the constaint in the
subclause and require a diagnostic:

Each subsequent enumerator with no = defines its enumeration
constant as the value of the constant expression obtained by
~~~~~~~~~~~~~~~~~~~~~~~
adding 1 to the value of the previous enumeration constant.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


OK, I guess I can buy that, though it's a little indirect for my taste.
Nov 13 '05 #9
In article <7w************@sic.twinsun.com>, Paul Eggert
<eg****@twinsun.com> writes
As an aside, the wording in C99 does not seem to specifically rule out
the following declaration:

enum { foo = INT_MAX, foo_plus_one };


The intent was that this wording:

Each subsequent enumerator
with no = defines its enumeration constant as the value of
the constant expression obtained by adding 1 to the value of
the previous enumeration constant.

generates a constant expression (in this case, "foo + 1") to which the
constraint applies. I think there was a C90 DR on the topic.

--
Clive D.W. Feather, writing for himself | Home: <cl***@davros.org>
Tel: +44 20 8371 1138 (work) | Web: <http://www.davros.org>
Fax: +44 870 051 9937 | Work: <cl***@demon.net>
Written on my laptop; please observe the Reply-To address
Nov 13 '05 #10

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

Similar topics

4
by: Travis Stewart | last post by:
Hi, I am not very familiar with C++ so the problems I am having might be simple and I just cannot see them or I am totally unaware of why something would be incorrect. Anyway, I'm using the ANN...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
1
by: boblotz2001 | last post by:
For anybody that implemented fuzzy searches in SQL Server or any other database for that matter. I have implemented edit distance algorithm, specifically q-grams, in SQL Server and wanted to get...
3
by: Hazem | last post by:
Hi, I want to make a calculation & need to appriximate any fraction after the point to be added as genuine like 4.1 = 5 what function or syntax can I use to accomplish that in VB.net ?? ...
130
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.