checking the number (int/double) range

This is a discussion on checking the number (int/double) range within the C++ Programming forums, part of the General Programming Boards category; Hello Is there any simple std or boost function/algorithm that will check some value's range and return true (if the ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    checking the number (int/double) range

    Hello

    Is there any simple std or boost function/algorithm that will check some value's range and return true (if the number is in the range) or false if its not.

    For instance:

    Code:
    int i = 5;
    if (i > 2 && i < 10) ..
    This code looks really ugly, especially if you need to duplicate it many times in your program.

    Thanks for help!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Max and minimum ranges of number can be attained from:
    boost::integer_traits<T>::const_max/const_min
    Or
    std::numeric_limits<T>::max()/min().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't think Elysia is answering the right question.

    Using templates, you should be able to do something like this:
    Code:
    template<typename T> 
    bool inrange(T x, T minval, T maxval)
    {
        return (x >= minval) && (x <= maxval);
    }
    I haven't tried the above code, but it should work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    It doesn't say "in range" to what so I guessed it was "in range" of the type's maximum / minimum numeric range (limit?).
    Of course, even so, it wasn't a complete code example... Perhaps I missed the thing that was the real problem? I don't know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by l2u View Post
    This code looks really ugly, especially if you need to duplicate it many times in your program.
    How do you need to duplicate it in your program? You could assign it to a variable and use that:
    Code:
    bool inrange = i > 2 && i < 10;
    
    if (inrange) docode();

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this, by any chance, related to your options parsing? If so, I would suggest that you have a couple of fields for each option, something like this:
    Code:
    struct OptionEntry
    {
        std:string optName;
        int defaultValue;
        int maxVal, minVal;
    } entry = { "Something", 10, 1, 100 };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Code:
    boost::integer_traits<T>::const_max/const_min
    As far as I have understood traits, they should only be used internally and not by the user of a library? I thought they are used to parametrize return types, zero-types, etc.. But then again, I very well might be wrong on this one..

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Well, traits contains all kinds of useful information about certain types. That's why they're called traits, I believe.
    They're not just for private use - they're there to make programming easier for you!
    You can use them to parameterize your own types, zero-types, etc, for your own functions
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Random number range
    By tzakieta in forum C Programming
    Replies: 9
    Last Post: 09-02-2008, 01:24 PM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  5. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM

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