how to define a range of type in C++ or C?

This is a discussion on how to define a range of type in C++ or C? within the C++ Programming forums, part of the General Programming Boards category; >> I will try this way..Thanks very much... Erm, that won't do what you want it to do. That only ...

  1. #16
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> I will try this way..Thanks very much...
    Erm, that won't do what you want it to do. That only stores 2001 values, ranging from -1000 to +1000.

    >> it will automatically change to type in these ranges...
    You can use the modulus operator:

    std::cout<< 1253215%1001;

    Make a class that stores two values, a real and an imaginary one. use the modulus operator to make sure their ranges are within the right values and you have it really.

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    This is obviously comming from a Pascal or Delphi background. In those languages you could do exactly what the OP describes.

    This functionality is not built-in C or C++. The closest thing you can do is specify how many bits of a bitfiend a number takes up. But that gives a range that is always a power of two.
    To do this you need to write a class, as others have began to show. I'll see what I can come up with shortly...

    beene: That answer truly belongs on the WorseThanFailure (previously theDailyWTF) website. Go hide your shameful face.

  3. #18
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> beene: That answer truly belongs on the WorseThanFailure (previously theDailyWTF) website. Go hide your shameful face.
    To be honest while it is not good code it's an interpretation of the OP's problem which was rather illucid to begin with, in my opinion, so he wrote the code to fit the problem as he saw it. You ever see that strip with the programmer, customer, and the product and the 6 different interpretations of it? It was very amusing, but I'm damned if I can find it now. I'll edit if I do.

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304

    Cool Here you go...

    Something like this should do what you want:
    Code:
    #include <climits>
    
    template <int MINRANGE = INT_MIN, int MAXRANGE = INT_MAX >
    class rangedInteger {
    	int value;
    
    public:
    	operator int() const {
    		return value;
    	}
    	rangedInteger() : value(MINRANGE) {
    	}
    	rangedInteger(int val) : value(val) {
    		if (value < MINRANGE || value > MAXRANGE) throw std::exception("Out of range");
    	}
    	rangedInteger(const rangedInteger &r) : value(r.value) {
    		if (value < MINRANGE || value > MAXRANGE) throw std::exception("Out of range");
    	}
    	const rangedInteger& operator = (const rangedInteger &r) {
    		value = r.value;
    		if (value < MINRANGE || value > MAXRANGE) throw std::exception("Out of range");
    		return *this;
    	}
    };
    Declare and use the variable like this:
    Code:
    	rangedInteger<-1000, 1000> ri;
    	ri = -397;
    	ri = ri + 2000; // Will throw an exception
    Edit: Fixed defaults
    Last edited by iMalc; 03-23-2007 at 03:38 PM.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Unless you make the int unsigned, -1 is probably a bad choice for the default MAXRANGE. All positive numbers would fail. std::numeric_limits<int>::max() might be better?

    Edit: Apparently std::numeric_limits<int>::max() is not a compile time constant. I guess INT_MAX would have to do it.
    Last edited by Daved; 03-23-2007 at 03:28 PM.

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by twomers
    >> beene: That answer truly belongs on the WorseThanFailure (previously theDailyWTF) website. Go hide your shameful face.
    To be honest while it is not good code it's an interpretation of the OP's problem which was rather illucid to begin with, in my opinion, so he wrote the code to fit the problem as he saw it. You ever see that strip with the programmer, customer, and the product and the 6 different interpretations of it? It was very amusing, but I'm damned if I can find it now. I'll edit if I do.
    I know I was harsh, I'm sure he knows I'm not that serious.
    Do you mean the one about the tree with the swing? - seen it.

    To be honest though, if you've ever used Pascal style languages, it is absolutely crystal clear what he is asking for from his initial description, and I believe the solution I just posted is exactly what he wants.

  7. #22
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    not quite

    >> it will automatically change to type in these ranges...

    so use a % in the constructor I'd say.

    >> if you've ever used Pascal style languages, it is absolutely crystal clear what he is asking for from his initial description
    I'm just uneducated then I've never used Pascal. Well, I did. But It was a long time ago when I hated programming so my memories of it are "as whispers upon yesterday's breeze", as someone once put it.

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    6,299
    Quote Originally Posted by Daved
    Unless you make the int unsigned, -1 is probably a bad choice for the default MAXRANGE. All positive numbers would fail. std::numeric_limits<int>::max() might be better?
    Indeed. I'd make the minimum allowed value default to std::numeric_limits<int>::min() as well.

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I just tried it with VC++ 2003 and it wouldn't allow it. Apparently numeric_limits<int>::max() is not a compile time constant. As I mentioned in an edit, INT_MAX and INT_MIN from <climits> would work.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Daved
    Unless you make the int unsigned, -1 is probably a bad choice for the default MAXRANGE. All positive numbers would fail. std::numeric_limits<int>::max() might be better?

    Edit: Apparently std::numeric_limits<int>::max() is not a compile time constant. I guess INT_MAX would have to do it.
    I actually made the default max less than the default min in the thinking that it forces you to not leave it unspecified. But duh it's a template and you have to specify it to make it useful , so yeah INT_MAX and INT_MIN would be good, or you could just forget the defaults.

    Actually, if one wants to use operators like +=, -=, *=, /=, >>=, <<= then you'd have to add those to the class too.
    Code:
    	const rangedInteger& operator += (const rangedInteger &r) {
    		value += r.value;
    		if (value < MINRANGE || value > MAXRANGE) throw std::exception("Out of range");
    		return *this;
    	}
    I'll leave the rest as an exersize for the reader.
    Last edited by iMalc; 03-23-2007 at 03:36 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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