Yeah, for a total of x times, whereSomething tells me I'm going to be made aware of this mistake several more times.
2 <= x <= ULONG_MAX
Yeah, for a total of x times, whereSomething tells me I'm going to be made aware of this mistake several more times.
2 <= x <= ULONG_MAX
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> 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?
Don't make me check the archives of your posts, twomers...
Sent from my iPadŽ
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.
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.Code:template <class T> bool rangecheck(string tokens, T value, T min, T max) { /* ... */ }
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.
I've been clean for nigh on ages! I promise.Originally Posted by SlyMaelstrom
You could use subtraction to check the ranges.
but that's no better is it?Code:if ( (num-lower > 0) && ( num-upper < 0 ) )
I got the answer that I wanted already (NO), but I can define it like this:
And use it like this:Code:#define Range(var,from,to) var>=from&&var<=to
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
tsk tsk, you should use inline functions instead of a macro. For one thing, your macro has a potential problem.
Consider:
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 between them!"; } else { std::cout << "It is not 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> #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!"; } }
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!"; } }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Then where do I need to put the parenthesis?
"The Internet treats censorship as damage and routes around it." - John Gilmore
Inline functions are just as fast as macros AND they are type safe! Only use macros when there is no other real choice.
You also have the twice-used argument problem. Consider:
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.Code:int i = whatever(); if(Range(++i, 2, 8)) { // What value does i have here? }
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