Minimum value of floating point types.

Discussion in 'C++' started by Fred Zwarts, Oct 8, 2009.

  1. Fred Zwarts

    Fred Zwarts Guest

    In a template function I need the minimum and maximum value of the valid range of values of a type.
    For integer types I can use std::numeric_limits<Type>::min () and std::numeric_limits<Type>::max () for this purpose.
    But this template function is also used for floating point types.
    For floating point types std::numeric_limits<Type>::min () does not return what it suggests, the minimum value,
    but it returns the smallest value larger than 0. (I wonder why this confusion was introduced.)
    My question is how to find the real minimum value in such cases.
    Is -std::numeric_limits<Type>::max () a good choice?
    I know that for signed integer types the assumption that
    -std::numeric_limits<Type>::max () is the most negative valid value for that type
    is often wrong. How is that for floating point types?

    Would the following code in a template function result in the best value?

    Type RealMinimun = std::numeric_limits<Type>::min () > 0 ? // Test for floating point type
    -std::numeric_limits<Type>::max () // Floating point minimum
    : std::numeric_limits<Type>::min (); // Integer minimum

    This results in a compiler warning when the template is instantiated for unsigned integer types,
    which should not be negated.
    I can ignore this warning, because the negation is not used in this case.
     
    Fred Zwarts, Oct 8, 2009
    #1
    1. Advertising

  2. Fred Zwarts wrote:
    > In a template function I need the minimum and maximum value of the valid range of values of a type.
    > For integer types I can use std::numeric_limits<Type>::min () and std::numeric_limits<Type>::max () for this purpose.
    > But this template function is also used for floating point types.
    > For floating point types std::numeric_limits<Type>::min () does not return what it suggests, the minimum value,
    > but it returns the smallest value larger than 0. (I wonder why this confusion was introduced.)
    > My question is how to find the real minimum value in such cases.
    > Is -std::numeric_limits<Type>::max () a good choice?


    Yes. The difference between FP reps and integral reps is that the FP
    ones are symmetrical AFA the sign goes. Integrals aren't necessarily
    such. For example, in 2's complement INT_MIN != -INT_MAX.

    > I know that for signed integer types the assumption that
    > -std::numeric_limits<Type>::max () is the most negative valid value for that type
    > is often wrong. How is that for floating point types?


    It is that for floating point types.

    > Would the following code in a template function result in the best value?
    >
    > Type RealMinimun = std::numeric_limits<Type>::min () > 0 ? // Test for floating point type
    > -std::numeric_limits<Type>::max () // Floating point minimum
    > : std::numeric_limits<Type>::min (); // Integer minimum
    >
    > This results in a compiler warning when the template is instantiated for unsigned integer types,
    > which should not be negated.
    > I can ignore this warning, because the negation is not used in this case.


    Why don't you write a function per type?

    template<class T> T getRealMinimum();

    template<> double getRealMinimum<double>() {
    return -std::numeric_limits<double>::max();
    }

    template<> int getRealMinimum<int>() {
    return std::numeric_limits<int>::min();
    }

    ... and so on

    Then you can simply pass the 'Type' onto that:

    Type RealMinimum = getRealMinimum<Type>();

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 8, 2009
    #2
    1. Advertising

  3. Fred Zwarts

    Fred Zwarts Guest

    Victor Bazarov wrote:
    > Fred Zwarts wrote:
    >> In a template function I need the minimum and maximum value of the
    >> valid range of values of a type.
    >> For integer types I can use std::numeric_limits<Type>::min () and
    >> std::numeric_limits<Type>::max () for this purpose. But this
    >> template function is also used for floating point types.
    >> For floating point types std::numeric_limits<Type>::min () does not
    >> return what it suggests, the minimum value,
    >> but it returns the smallest value larger than 0. (I wonder why this
    >> confusion was introduced.)
    >> My question is how to find the real minimum value in such cases.
    >> Is -std::numeric_limits<Type>::max () a good choice?

    >
    > Yes. The difference between FP reps and integral reps is that the FP
    > ones are symmetrical AFA the sign goes. Integrals aren't necessarily
    > such. For example, in 2's complement INT_MIN != -INT_MAX.
    >
    >> I know that for signed integer types the assumption that
    >> -std::numeric_limits<Type>::max () is the most negative valid value
    >> for that type
    >> is often wrong. How is that for floating point types?

    >
    > It is that for floating point types.
    >
    >> Would the following code in a template function result in the best
    >> value?
    >>
    >> Type RealMinimun = std::numeric_limits<Type>::min () > 0 ? //
    >> Test for floating point type -std::numeric_limits<Type>::max
    >> () // Floating point minimum :
    >> std::numeric_limits<Type>::min (); // Integer minimum
    >>
    >> This results in a compiler warning when the template is instantiated
    >> for unsigned integer types,
    >> which should not be negated.
    >> I can ignore this warning, because the negation is not used in this
    >> case.

    >
    > Why don't you write a function per type?


    The first reason is that I don't like to copy almost identical code repeatedly.

    The second reason is that I write this code for several platforms,
    which do not all have the same set of types.
    A generic approach solves the problem with less code and with a
    less complicated coverage of the different platforms.

    I have thought about specializations for the floating point types to
    remove the compiler warnings, but even the set of floating point
    types is different on these platforms.
     
    Fred Zwarts, Oct 8, 2009
    #3
  4. Fred Zwarts

    Fred Zwarts Guest

    Victor Bazarov wrote:
    > Fred Zwarts wrote:
    >> In a template function I need the minimum and maximum value of the
    >> valid range of values of a type.
    >> For integer types I can use std::numeric_limits<Type>::min () and
    >> std::numeric_limits<Type>::max () for this purpose. But this
    >> template function is also used for floating point types.
    >> For floating point types std::numeric_limits<Type>::min () does not
    >> return what it suggests, the minimum value,
    >> but it returns the smallest value larger than 0. (I wonder why this
    >> confusion was introduced.)
    >> My question is how to find the real minimum value in such cases.
    >> Is -std::numeric_limits<Type>::max () a good choice?

    >
    > Yes. The difference between FP reps and integral reps is that the FP
    > ones are symmetrical AFA the sign goes.


    Do you know where in the C++ standard this is specified?
     
    Fred Zwarts, Oct 8, 2009
    #4
  5. Fred Zwarts wrote:
    > Victor Bazarov wrote:
    >> Fred Zwarts wrote:
    >>> In a template function I need the minimum and maximum value of the
    >>> valid range of values of a type.
    >>> For integer types I can use std::numeric_limits<Type>::min () and
    >>> std::numeric_limits<Type>::max () for this purpose. But this
    >>> template function is also used for floating point types.
    >>> For floating point types std::numeric_limits<Type>::min () does not
    >>> return what it suggests, the minimum value,
    >>> but it returns the smallest value larger than 0. (I wonder why this
    >>> confusion was introduced.)
    >>> My question is how to find the real minimum value in such cases.
    >>> Is -std::numeric_limits<Type>::max () a good choice?

    >> Yes. The difference between FP reps and integral reps is that the FP
    >> ones are symmetrical AFA the sign goes.

    >
    > Do you know where in the C++ standard this is specified?


    No, and I'm too lazy to look it up for you.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 8, 2009
    #5
  6. On Oct 8, 9:25 am, Pete Becker <> wrote:
    > Fred Zwarts wrote:
    > > Victor Bazarov wrote:
    > >> Fred Zwarts wrote:
    > >>> In a template function I need the minimum and maximum value of the
    > >>> valid range of values of a type.
    > >>> For integer types I can use std::numeric_limits<Type>::min () and
    > >>> std::numeric_limits<Type>::max () for this purpose. But this
    > >>> template function is also used for floating point types.
    > >>> For floating point types std::numeric_limits<Type>::min () does not
    > >>> return what it suggests, the minimum value,
    > >>> but it returns the smallest value larger than 0. (I wonder why this
    > >>> confusion was introduced.)
    > >>> My question is how to find the real minimum value in such cases.
    > >>> Is -std::numeric_limits<Type>::max () a good choice?
    > >> Yes.  The difference between FP reps and integral reps is that the FP
    > >> ones are symmetrical AFA the sign goes.  

    >
    > > Do you know where in the C++ standard this is specified?

    >
    > It's not. But it's true, nonetheless. <g>
    >
    > --
    >    Pete
    > Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
    > "The Standard C++ Library Extensions: a Tutorial and Reference"
    > (www.petebecker.com/tr1book)


    Agh, sorry it is off-topic
    Pate and Victor -- it has been a long time -- why don't you guys join
    stackoverfow group: http://stackoverflow.com/questions/tagged/c++?
    just curious. I am there and I love it!
     
    puzzlecracker, Oct 8, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ioannis Vranos
    Replies:
    13
    Views:
    498
    James Kanze
    Mar 10, 2008
  2. Ioannis Vranos

    Minimum sizes of integral and floating point types

    Ioannis Vranos, Mar 8, 2008, in forum: C Programming
    Replies:
    9
    Views:
    349
    Richard Heathfield
    Mar 9, 2008
  3. ceycey
    Replies:
    5
    Views:
    524
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,369
    Saraswati lakki
    Jan 6, 2012
  5. Marco
    Replies:
    2
    Views:
    111
    Serhiy Storchaka
    Jul 16, 2013
Loading...

Share This Page