Restricted Integer

This is a discussion on Restricted Integer within the C++ Programming forums, part of the General Programming Boards category; Looking out for a program in C++ that implements a restricted integer....

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    Question Restricted Integer

    Looking out for a program in C++ that implements a restricted integer.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You mean, an integer that can only store a range of values?

    Well, there are lots of ways you could do this. You could simply restrict the range of a number by using modulus or some such every time it was assigned to, for example. You could throw an exception when the integer got out of range. You could wrap it all up into a class to make things easier . . . .

    Why do you want to know? What have you tried? Is this homework? Or are you just curious?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    A restricted integer is one that has a limited domain. E.g. if you define a variable x of type restricted integer, this would have constraints as defined in your class, such as the number can’t exceed 1000.

    Out of curiosity, just looking out for a program

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    I think one way would be a class. Overload appropriate operators to make it act like a normal integer, but assert or throw errors if it exceeds range or limitations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    assert or throw errors if it exceeds range or limitations.
    or wrap around like a native type would =).

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Don't forget to attempt the code first and post what you've got (preferably your own code and not code you find on the internet).

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    I have written such a class which you can find here:
    http://homepages.ihug.co.nz/~aurora7...ul_Classes.htm
    It's called RangedInteger, and throws exceptions when you try and give it a number outside the range. Let me know if you find any bugs.
    Feel free to fix the crappy exception handling. It's not something I've worked on recently. You use it like this:
    Code:
    try {
    rangedInteger<1, 1000> r;
    r = 500;
    r *= 3; // throws
    } catch(...)
    {
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21