# More than and less than

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 10-08-2006
laserlight
Quote:

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
:D
• 10-08-2006
twomers
>> 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?
• 10-08-2006
SlyMaelstrom
Don't make me check the archives of your posts, twomers...
• 10-08-2006
Mario F.
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.
• 10-08-2006
twomers
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?
• 10-08-2006
maxorator
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; }```
• 10-08-2006
laserlight
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!";     } }```
• 10-08-2006
maxorator
Then where do I need to put the parenthesis?
• 10-08-2006
Wraithan
Inline functions are just as fast as macros AND they are type safe! Only use macros when there is no other real choice.
• 10-08-2006
CornedBee
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.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12