More than and less than

This is a discussion on More than and less than within the C++ Programming forums, part of the General Programming Boards category; Something tells me I'm going to be made aware of this mistake several more times. Yeah, for a total of ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,262
    >> 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,069
    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
    Portugal
    Posts
    7,438
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,262
    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
    21,650
    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!";
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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

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

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