473,408 Members | 1,976 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,408 software developers and data experts.

Putting logical limitations on types

Hi,
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?

For example: a bound_int which can only be in a certain range, so
don't i don't have to have a bound checking function like this one:

int in_range(int i, int min, int max){
----if(i<min)
--------return min;
----if(i>max)
--------return max;
}

Or a prime_number type, and so on.

Thanks,
Ido.
Jan 25 '08 #1
11 1237
Ido Yehieli wrote:
Hi,
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?

For example: a bound_int which can only be in a certain range, so
don't i don't have to have a bound checking function like this one:

int in_range(int i, int min, int max){
----if(i<min)
--------return min;
----if(i>max)
--------return max;
}

Or a prime_number type, and so on.
A variable of type T can store any value in T's range,
without restriction. There is no way you can limit the
values to a subrange or subset:

int hour; /* 0 through 23, please */
hour = 42; /* nothing prevents this */

int odd; /* odd numbers only, please */
odd = 42; /* nothing prevents this */

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jan 25 '08 #2
In article <92**********************************@e23g2000prf. googlegroups.com>,
Ido Yehieli <Id*********@gmail.comwrote:
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?
>For example: a bound_int which can only be in a certain range, so
don't i don't have to have a bound checking function
No, not in C.

Note: C enumerations ("enum") are suggestive of what you are looking
for, but they do NOT prevent other values from being stored.
Effectively they just give names for integral constants, rather
than controlling the data type.
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)
Jan 25 '08 #3
Ido Yehieli <Id*********@gmail.comwrites:
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?
Not in C.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Jan 25 '08 #4
On Jan 25, 11:12 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
int hour; /* 0 through 23, please */
hour = 42; /* nothing prevents this */
That's unfortunate. Thanks anyway.
Jan 25 '08 #5
On Jan 25, 11:55 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
If you want to discus the best alternatively language for your
requirements comp.programming *might* be an appropriate place.
Alternatively, if you want to explain more about what you want to
achieve it might be that someone can suggest a completely different
approach using C to solve your problem.
Never mind, I'll just use a function to check the bounds when needed.
I thought there might be a more appropriate way of doing it.

-Ido,
Jan 25 '08 #6
Ido Yehieli <Id*********@gmail.comwrites:
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?

For example: a bound_int which can only be in a certain range
<snip>
Or a prime_number type, and so on.
You've had the C answer: no, but it is worth pointing out that a
language that the full version of what you are hinting at with a "prime
number" type would either have to abandon compile time types or the
compiler would need a complex theorem prover in it (and the compiler
might end up not guaranteed to terminate). That is far cry from C.

--
Ben.
Jan 26 '08 #7
On Jan 26, 2:02 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
a
language that the full version of what you are hinting at with a "prime
number" type would either have to abandon compile time types or the
compiler would need a complex theorem prover in it
I guess a lazy-evaluating language could provide such a type.
But then a prime_number type is just syntactic sugar around a function
that generates prime numbers.

-Ido.

Jan 26 '08 #8
Ido Yehieli wrote:
On Jan 26, 2:02 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>a
language that the full version of what you are hinting at with a
"prime number" type would either have to abandon compile time types
or the compiler would need a complex theorem prover in it

I guess a lazy-evaluating language could provide such a type.
But then a prime_number type is just syntactic sugar around a function
that generates prime numbers.
What is a prime number /type/ supposed to do? Signal an error when a
non-prime number is written to it?
Jan 26 '08 #9
On Jan 26, 10:59 am, "Malcolm McLean" <regniz...@btinternet.com>
wrote:
This isn't particularly complicated, but will
approximately halve the speed of the program.
How did you figure it will be 1/2?
I'm not saying it wouldn't, just interested to see where this estimate
came from.

-Ido.
Jan 26 '08 #10
On Jan 25, 1:56*pm, Ido Yehieli <Ido.Yehi...@gmail.comwrote:
Hi,
* * can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?
Don't you think you need a better reference manual, if it cannot
answer simple questions about what the language can or cannot do?
Jan 26 '08 #11
On Jan 26, 3:40 pm, Kaz Kylheku <kkylh...@gmail.comwrote:
Don't you think you need a better reference manual, if it cannot
answer simple questions about what the language can or cannot do?
I knew the exact thing I asked is probably not possible, but hoped
someone know an elegant solution I didn't think about.

-Ido.
Jan 26 '08 #12

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

Similar topics

6
by: Hari Om | last post by:
Here are the details of my error log files: I execute the command and get following message at console: ---------------------------------------------------------------------- ../sqlldr...
13
by: Jeager | last post by:
Why is it, Microsoft manage to write operating systems and office applications with every bell and whistle facility known to man. Yet, even after years and years of development they still cannot...
0
by: chris6995 | last post by:
I'm looking for a way to programatically query for the association between a physical device (i.e. removable USB storage) and it's logical drive letter. I've gone through Win32_LogicalDisk,...
19
by: Steve Jorgensen | last post by:
I've run across this issue several times of late, and I've never come up with a satisfactory answer to the best way to handle this schema issue. You have a large section of schema in which a...
80
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one...
1
by: Shawn | last post by:
i am trying to write a test that would cause a method that writes a registry value name to fail. can anyone tell me if there are any limitations to a registry value name? length? types of...
23
by: Cor | last post by:
Hi group, A question to the group, would it not be for a lot of the users better when there was beside "Option strict on" an "Option logical Strict on". I was automaticaly writting...
4
by: Doug Laidlaw | last post by:
Thank you everybody for your help to date, and please be tolerant of a CSS beginner! I have successfully converted my site at http://www.douglaidlaw.net/boykett/ to use a free 3-column template...
11
by: Dominic Vella | last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical type fields. I start with Dim dbsTmp As Object ' I think it's DAO.Database Set dbsTmp =...
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
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.