Thread: More than and less than

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Something tells me I'm going to be made aware of this mistake several more times.
    Yeah, for a total of x times, where
    2 <= x <= ULONG_MAX
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> Something tells me I'm going to be made aware of this mistake several more times.

    Would you be offended if I copied it into my signature?

  3. #18
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Don't make me check the archives of your posts, twomers...
    Sent from my iPadŽ

  4. #19
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    There doesn't seem to be a way to establish if a number is within a range by means of a mathematical expression either. Well, at least one that can be used with a single condition. So you have at least 2 options:

    - Use a compiler extension if it offers one.
    - Use a templated function as discussed before

    An interesting variant is to use the templated function to allow any type of comparison.

    Code:
    template <class T>
    bool rangecheck(string tokens, T value, T min, T max) {
       /* ... */
    }
    tokens is a string that allows you to establish comparisons based on interval notation. It is used to establish what comparisons are to be performed. "()", "[]", "[)", "(]" are the possible argument values to be passed.
    Last edited by Mario F.; 10-08-2006 at 08:02 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #20
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by SlyMaelstrom
    Don't make me check the archives of your posts, twomers...
    I've been clean for nigh on ages! I promise.

    You could use subtraction to check the ranges.
    Code:
    if ( (num-lower > 0) && ( num-upper < 0 ) )
    but that's no better is it?

  6. #21
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I got the answer that I wanted already (NO), but I can define it like this:
    Code:
    #define Range(var,from,to) var>=from&&var<=to
    And use it like this:
    Code:
    #include <iostream>
    #define Range(var,from,to) var>=from&&var<=to 
    
    int main(){
    	if(Range(9,3,8)){
    		std::cout<<"It is between them!";
    	}
    	system("pause");
    	return 0;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    tsk tsk, you should use inline functions instead of a macro. For one thing, your macro has a potential problem.

    Consider:
    Code:
    #include <iostream>
    #define Range(var,from,to) var>=from&&var<=to 
    
    int main() {
        if (Range(9,3,8)){
            std::cout << "It is between them!";
        } else {
            std::cout << "It is not between them!";
        }
    }
    Since (9 <= 8) is false, the whole expression is false, hence "It is not between them!" is printed. Now consider:
    Code:
    #include <iostream>
    #define Range(var,from,to) var>=from&&var<=to 
    
    int main() {
        if (!Range(9,3,8)){
            std::cout << "It is not between them!"; // note the change to "not"
        } else {
            std::cout << "It is between them!";
        }
    }
    Since (!9>=3) is (0>=3) which is false, the whole expression is false, hence "It is between them!" is printed. This is incorrect. You can avoid it by wrapping more parentheses around your macro's variables, or you can use a function template, inlined if need be:
    Code:
    #include <iostream>
    
    template <typename T>
    inline bool Range(T var, T from, T to) {
        return var >= from && var <= to;
    }
    
    int main() {
        if (!Range(9,3,8)){
            std::cout << "It is not between them!"; // note the change to "not"
        } else {
            std::cout << "It is between them!";
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Then where do I need to put the parenthesis?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #24
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Inline functions are just as fast as macros AND they are type safe! Only use macros when there is no other real choice.

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You also have the twice-used argument problem. Consider:
    Code:
    int i = whatever();
    if(Range(++i, 2, 8)) {
      // What value does i have here?
    }
    The answer is, 2 more than what whatever() returned. Remember that the preprocessor is just a text replacement engine and think about why this happens.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed