finding the minimum allowed value for a type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam H. Peterson

    finding the minimum allowed value for a type

    Is there a standard way to find the minimum value for a data type? I'm
    thinking along the lines of std::numeric_li mits<T>::min(). But that
    doesn't work for floating point types. I can't use
    (-std::numeric_li mits<T>::max()) either, because that won't work for
    unsigned types (and technically won't work for signed integer types and
    others either, although it would probably be "good enough" for them).

    Do I need to implement a new template that uses
    std::numeric_li mits<>::min() and just specialize it for floats/doubles?
    Or is this already solved somewhere in the library?

    Thanks,
    Adam H. Peterson

  • Mike Wahler

    #2
    Re: finding the minimum allowed value for a type

    "Adam H. Peterson" <ahp6@email.byu .edu> wrote in message
    news:bl1sq5$2ta h$1@acs2.byu.ed u...[color=blue]
    > Is there a standard way to find the minimum value for a data type? I'm
    > thinking along the lines of std::numeric_li mits<T>::min().[/color]

    That's the one. :-)
    [color=blue]
    > But that
    > doesn't work for floating point types.[/color]

    Sure it does. What have you tried?
    [color=blue]
    >I can't use
    > (-std::numeric_li mits<T>::max()) either,[/color]

    Why would you want to do that?
    [color=blue]
    >because that won't work for
    > unsigned types[/color]

    Well, it doesn't make much sense to try to negate
    an unsigned type.
    [color=blue]
    >(and technically won't work for signed integer types and
    > others either,[/color]

    What have you tried that "wont' work"?
    [color=blue]
    >although it would probably be "good enough" for them).
    >
    > Do I need to implement a new template that uses
    > std::numeric_li mits<>::min() and just specialize it for floats/doubles?[/color]

    The standard library already specializes the template
    for all scalar types except pointers.
    [color=blue]
    > Or is this already solved somewhere in the library?[/color]

    It's already done. Why not post a small, compilable
    program that demonstrates the trouble you're having?

    -Mike


    Comment

    • David Rubin

      #3
      Re: finding the minimum allowed value for a type

      "Adam H. Peterson" wrote:[color=blue]
      >
      > Is there a standard way to find the minimum value for a data type? I'm
      > thinking along the lines of std::numeric_li mits<T>::min(). But that
      > doesn't work for floating point types.[/color]

      Is there nothing analogous to DBL_MIN and FLT_MIN in C (limits.h)?
      [color=blue]
      > I can't use
      > (-std::numeric_li mits<T>::max()) either, because that won't work for
      > unsigned types[/color]

      Isn't the minimum value for unsigned types zero?
      [color=blue]
      > (and technically won't work for signed integer types and
      > others either, although it would probably be "good enough" for them).[/color]

      Signed integer types can use std::numeric_li mits<T>::min() as you
      pointed out.
      [color=blue]
      > Do I need to implement a new template that uses
      > std::numeric_li mits<>::min() and just specialize it for floats/doubles?
      > Or is this already solved somewhere in the library?[/color]

      /david

      --
      Andre, a simple peasant, had only one thing on his mind as he crept
      along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
      -- unknown

      Comment

      • Ron Natalie

        #4
        Re: finding the minimum allowed value for a type


        "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:ua0db.5252 $NX3.922@newsre ad3.news.pas.ea rthlink.net...[color=blue]
        > "Adam H. Peterson" <ahp6@email.byu .edu> wrote in message
        > news:bl1sq5$2ta h$1@acs2.byu.ed u...[color=green]
        > > Is there a standard way to find the minimum value for a data type? I'm
        > > thinking along the lines of std::numeric_li mits<T>::min().[/color]
        >
        > That's the one. :-)
        >[/color]

        No it's not. numeric_limits< double>::min() reports the smallest positilve representable number.
        What he wants is the mininum representable (numerically).

        What he wants is:

        template <typename T> T real_min() {
        if(numeric_limi ts<T>::is_integ er) return numeric_limits< T>::min();
        else return -numeric_limits< T>::max();
        }


        Comment

        • Adam H. Peterson

          #5
          Re: finding the minimum allowed value for a type

          David Rubin wrote:[color=blue]
          > "Adam H. Peterson" wrote:[/color]
          <snip>[color=blue]
          > Is there nothing analogous to DBL_MIN and FLT_MIN in C (limits.h)?[/color]
          <snip>[color=blue]
          > Isn't the minimum value for unsigned types zero?[/color]
          <snip>[color=blue]
          > Signed integer types can use std::numeric_li mits<T>::min() as you
          > pointed out.
          >[/color]

          But I'm interested in doing this in a template, where I don't know if
          I'll be passed a signed type, an unsigned type, or a floating point type
          (or possibly a UDT for which I would insist that the proper template be
          specialized). Inside the template, I don't know which of these cases to
          apply.

          So what is the best approach for an arbitrary type?

          Thanks for the response.

          Adam H. Peterson

          Comment

          • Adam H. Peterson

            #6
            Re: finding the minimum allowed value for a type

            Mike Wahler wrote:[color=blue]
            > "Adam H. Peterson" <ahp6@email.byu .edu> wrote in message
            > news:bl1sq5$2ta h$1@acs2.byu.ed u...
            >[color=green]
            >>Is there a standard way to find the minimum value for a data type? I'm
            >>thinking along the lines of std::numeric_li mits<T>::min().[/color]
            >
            >
            > That's the one. :-)
            >
            >[color=green]
            >> But that
            >>doesn't work for floating point types.[/color]
            >
            >
            > Sure it does. What have you tried?[/color]

            For floating point types, it returns the minimum number greater than
            zero, which is not what I want.

            Comment

            • Adam H. Peterson

              #7
              Re: finding the minimum allowed value for a type

              > It's already done. Why not post a small, compilable[color=blue]
              > program that demonstrates the trouble you're having?[/color]

              Here you go, and thanks for taking the time to respond:


              #include <limits>
              #include <iostream>
              #include <ostream>

              template<typena me T>
              void find_min_max(T const *array, int unsigned size, T &min, T &max) {
              min=std::numeri c_limits<T>::ma x();
              max=std::numeri c_limits<T>::mi n();
              while (size--) {
              if (min>array[size])
              min=array[size];
              if (max<array[size])
              max=array[size];
              }
              }

              int main() {
              double sampledata[]={
              -1.0,
              -2.0,
              -4.5,
              -0.5,
              -3.5
              };
              double min,max;
              find_min_max(sa mpledata, 5, min, max);
              std::cout
              << "Values range from "
              << min
              << " to "
              << max
              << std::endl;
              }



              The problem comes when find_min_max gives a positive (very small) value
              for max, even though all values in the array are negative. On my
              machine, it prints:

              Values range from -4.5 to 2.22507e-308



              Adam H. Peterson

              Comment

              • Kevin Goodsell

                #8
                Re: finding the minimum allowed value for a type

                Ron Natalie wrote:
                [color=blue]
                >
                > What he wants is:
                >
                > template <typename T> T real_min() {
                > if(numeric_limi ts<T>::is_integ er) return numeric_limits< T>::min();
                > else return -numeric_limits< T>::max();[/color]

                Is there a requirement that the smallest (most negative) value for a
                floating point type is the negation of the largest value?

                -Kevin
                --
                My email address is valid, but changes periodically.
                To contact me please use the address from a recent posting.

                Comment

                • Pete Becker

                  #9
                  Re: finding the minimum allowed value for a type

                  "Adam H. Peterson" wrote:[color=blue]
                  >[color=green]
                  > > It's already done. Why not post a small, compilable
                  > > program that demonstrates the trouble you're having?[/color]
                  >
                  > Here you go, and thanks for taking the time to respond:
                  >
                  > #include <limits>
                  > #include <iostream>
                  > #include <ostream>
                  >
                  > template<typena me T>
                  > void find_min_max(T const *array, int unsigned size, T &min, T &max) {
                  > min=std::numeri c_limits<T>::ma x();
                  > max=std::numeri c_limits<T>::mi n();
                  > while (size--) {
                  > if (min>array[size])
                  > min=array[size];
                  > if (max<array[size])
                  > max=array[size];
                  > }
                  > }
                  >[/color]

                  You don't need the minimum and maximum possible values. Initialize min
                  and max with array[size - 1], and run your loop down from there.

                  --

                  Pete Becker
                  Dinkumware, Ltd. (http://www.dinkumware.com)

                  Comment

                  • Ron Natalie

                    #10
                    Re: finding the minimum allowed value for a type


                    "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
                    news:S11db.5384 $NX3.1801@newsr ead3.news.pas.e arthlink.net...[color=blue]
                    > Ron Natalie wrote:
                    >[color=green]
                    > >
                    > > What he wants is:
                    > >
                    > > template <typename T> T real_min() {
                    > > if(numeric_limi ts<T>::is_integ er) return numeric_limits< T>::min();
                    > > else return -numeric_limits< T>::max();[/color]
                    >
                    > Is there a requirement that the smallest (most negative) value for a
                    > floating point type is the negation of the largest value?[/color]

                    Probably not, but I don't think I've ever encountered a floating point
                    format where that wasn't the case.


                    Comment

                    • Adam H. Peterson

                      #11
                      Re: finding the minimum allowed value for a type

                      > You don't need the minimum and maximum possible values. Initialize min[color=blue]
                      > and max with array[size - 1], and run your loop down from there.[/color]

                      Thank you. That works in this case, but there are other cases where
                      that's not necessarily an option. This program was by way of example.

                      Adam H. Peterson

                      Comment

                      • Mike Wahler

                        #12
                        Re: finding the minimum allowed value for a type


                        "Ron Natalie" <ron@sensor.com > wrote in message
                        news:3f7490cd$0 $167$9a6e19ea@n ews.newshosting .com...[color=blue]
                        >
                        > "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message[/color]
                        news:ua0db.5252 $NX3.922@newsre ad3.news.pas.ea rthlink.net...[color=blue][color=green]
                        > > "Adam H. Peterson" <ahp6@email.byu .edu> wrote in message
                        > > news:bl1sq5$2ta h$1@acs2.byu.ed u...[color=darkred]
                        > > > Is there a standard way to find the minimum value for a data type?[/color][/color][/color]
                        I'm[color=blue][color=green][color=darkred]
                        > > > thinking along the lines of std::numeric_li mits<T>::min().[/color]
                        > >
                        > > That's the one. :-)
                        > >[/color]
                        >
                        > No it's not. numeric_limits< double>::min() reports the smallest[/color]
                        positilve representable number.

                        Oops, I overlooked that when I looked it up. Not
                        surprising, since most of my applications are financial
                        or business type, and I almost always use integral types
                        for currency, inventory quantites, etc. Floating-point
                        stuff is not as familiar or common to me as with many
                        other folks.
                        [color=blue]
                        > What he wants is the mininum representable (numerically).
                        >
                        > What he wants is:
                        >
                        > template <typename T> T real_min() {
                        > if(numeric_limi ts<T>::is_integ er) return numeric_limits< T>::min();
                        > else return -numeric_limits< T>::max();
                        > }[/color]

                        So I've learned two things:

                        1. numeric_limits< double>::min() returns smallest *positive* value
                        2. Pay closer attention to detail :-)

                        -Mike


                        Comment

                        • Mike Wahler

                          #13
                          Re: finding the minimum allowed value for a type


                          "Adam H. Peterson" <ahp6@email.byu .edu> wrote in message
                          news:bl241k$34s 6$1@acs2.byu.ed u...[color=blue]
                          > David Rubin wrote:[color=green]
                          > > "Adam H. Peterson" wrote:[/color]
                          > <snip>[color=green]
                          > > Is there nothing analogous to DBL_MIN and FLT_MIN in C (limits.h)?[/color]
                          > <snip>[color=green]
                          > > Isn't the minimum value for unsigned types zero?[/color]
                          > <snip>[color=green]
                          > > Signed integer types can use std::numeric_li mits<T>::min() as you
                          > > pointed out.
                          > >[/color]
                          >
                          > But I'm interested in doing this in a template, where I don't know if
                          > I'll be passed a signed type, an unsigned type, or a floating point type
                          > (or possibly a UDT for which I would insist that the proper template be
                          > specialized). Inside the template, I don't know which of these cases to
                          > apply.
                          >
                          > So what is the best approach for an arbitrary type?[/color]

                          I think you could test numeric_limits< T>::is_integer
                          and numeric_limits< T>::is_signed to do special
                          processing for floating point types, e.g. what
                          Ron showed: -numeric_limits< T>::max()

                          -Mike


                          Comment

                          • Gianni Mariani

                            #14
                            Re: finding the minimum allowed value for a type

                            Adam H. Peterson wrote:[color=blue]
                            > Is there a standard way to find the minimum value for a data type? I'm
                            > thinking along the lines of std::numeric_li mits<T>::min(). But that
                            > doesn't work for floating point types. I can't use
                            > (-std::numeric_li mits<T>::max()) either, because that won't work for
                            > unsigned types (and technically won't work for signed integer types and
                            > others either, although it would probably be "good enough" for them).
                            >
                            > Do I need to implement a new template that uses
                            > std::numeric_li mits<>::min() and just specialize it for floats/doubles?
                            > Or is this already solved somewhere in the library?[/color]

                            This below I think gives you what you want - however it's impractical
                            because of the excessive compilation time but ... if compilers were a
                            little faster, it may actually be practical. However, it's just a toy I
                            played with that shows computation of numeric constants using templates.

                            You could easily extend this to compute compile time constants for
                            things like e, pi, ln 10 nCr, factorial, and even a prime number in such
                            a way that the constants would be compatible for different machine formats.

                            Besides that, it's also an interesting compiler stress test.

                            #include <iostream>

                            template <typename T, int N>
                            struct pow_n
                            {
                            static const T value = pow_n<T, N-1>::value/2;
                            };

                            template <typename T>
                            struct pow_n<T, 0>
                            {
                            static const T value = static_cast<T>( 1 );
                            };

                            template <typename T, int N, bool v> struct min_f;

                            template <typename T, int N>
                            struct min_f<T,N,true>
                            {
                            static const T min_value = pow_n<T,N-1>::value;

                            static const int min_exponent = N-1;
                            };

                            template <typename T, int N, bool v>
                            struct min_f : min_f<T, N+1, pow_n<T, N>::value/2 == static_cast<T>( 0 ) >
                            {
                            };

                            template<typena me T>
                            struct min_limit
                            {
                            static const int min_exponent = min_f<T,0,false >::min_exponent ;
                            static const T min_value = min_f<T,0,false >::min_value;

                            };

                            int main()
                            {
                            std::cout << "Float\n";
                            std::cout << min_limit< float >::min_expone nt << "\n";
                            std::cout << min_limit< float >::min_value << "\n";

                            std::cout << "Double\n";
                            std::cout << min_limit< double >::min_expone nt << "\n";
                            std::cout << min_limit< double >::min_value << "\n";

                            };


                            The code above took 3min 14sec in compilation time on a 800Mhz x86-P3
                            machine with gcc 3.3.1 with the -ftemplate-depth-10000 flag.
                            (Obviously miniscule execution time).

                            g++ -ftemplate-depth-10000 -Wreturn-type -W -Wpointer-arith -pipe -ggdb3
                            -fcheck-new -fPIC -D_POSIX_THREADS -D_POSIX_THREAD_ SAFE_FUNCTIONS
                            -D_REENTRANT -DACE_HAS_AIO_CA LLS
                            -DBUILD_VERSION= 0309262338-i686-uluru-gx86-gianni -DBUILD_ARCH=gx8 6
                            -I ./ -I work.gx86 -I
                            /home/gianni/limbo/asmx/makexs/src/hello_library/code/ qqq.cpp
                            -o qqq
                            171.260u 2.830s 3:13.92 89.7% 0+0k 0+0io 3932pf+0w
                            [gianni@uluru makexs]$ time qqq
                            Float
                            149
                            1.4013e-45
                            Double
                            1074
                            4.94066e-324
                            0.000u 0.000s 0:00.00 0.0% 0+0k 0+0io 219pf+0w



                            Comment

                            • Jerry Coffin

                              #15
                              Re: finding the minimum allowed value for a type

                              In article <bl3du4$45a@dis patch.concentri c.net>, gi2nospam@maria ni.ws
                              says...

                              [ ... ]
                              [color=blue]
                              > The code above took 3min 14sec in compilation time on a 800Mhz x86-P3
                              > machine with gcc 3.3.1 with the -ftemplate-depth-10000 flag.[/color]

                              Hmmm...strange -- it gave identical results for me, but compiled in only
                              21 seconds with gcc 3.2 (the mingw port). This was on a 1.2 GHz P3, so
                              I expected it to be a _little_ faster, but not nearly this much.

                              --
                              Later,
                              Jerry.

                              The universe is a figment of its own imagination.

                              Comment

                              Working...