Topic: numeric_limits::min () doesn't support generic programming


Author: "Ron" <nospam@this-address.org>
Date: 1999/10/15
Raw View
Daniel M. Pfeffer <pfefferd@nospam.internet-zahav.net> wrote in message
news:7u2e76$7dl$1@lnews.actcom.co.il...
> Ron <nospam@this-email-address.org> wrote in message
> news:V8vM3.444$p27.28574@typ12.nn.bcandid.com...
> >
> > Recently I've had an unpleasant experience using
std::numeric_limits::min
> ()
> > in a template class. According to the C++ specification, this function
> > returns the specialization's "minimum finite value", which the spec says
> is
> > "Equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, etc.". The spec
also
> > notes that "For floating types with denormalization, returns the minimum
> > positive normalized value".
> >
> > This formulation creates problems when it comes to generic programming,
> > because min () returns one kind of thing (the most negative
representable
> > value) when specialized for an integral type, and another kind of thing
> (the
> > minimum positive normalized value) when specialized for a floating type.
> > This inconsistency violates the prime directive of generic programming,
> > which is that the qualitative meaning of an operation should not depend
> upon
> > the type upon which it operates.
>
> Not being privy to the ANSI C++ committee's deliberations, I can only
assume
> that what is returned by std::numeric_limits<floating-point-type>::min ()
is
> the smallest value with fully significant digits. The 'digits' member
> represents the number of radix digits available in the implementation.
> Denormalised values have fewer radix digits than normalised values (the
> first N digits are always 0), which would invalidate this.
>
> Rather than modify the definition of min(), they provided the has_denorm
> member to indicate that the type supports denormalised values. It should
> then be possible to construct the 'smallest denormalised value' by
dividing
> min() by radix^(digits-1).

Well yes, but I want the most negative representable value, not the smallest
(positive) denormalized value. Any ideas?

Thanks,

Ron
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Daniel M. Pfeffer" <pfefferd@nospam.internet-zahav.net>
Date: 1999/10/14
Raw View
Ron <nospam@this-email-address.org> wrote in message
news:V8vM3.444$p27.28574@typ12.nn.bcandid.com...
>
> Recently I've had an unpleasant experience using std::numeric_limits::min
()
> in a template class. According to the C++ specification, this function
> returns the specialization's "minimum finite value", which the spec says
is
> "Equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, etc.". The spec also
> notes that "For floating types with denormalization, returns the minimum
> positive normalized value".
>
> This formulation creates problems when it comes to generic programming,
> because min () returns one kind of thing (the most negative representable
> value) when specialized for an integral type, and another kind of thing
(the
> minimum positive normalized value) when specialized for a floating type.
> This inconsistency violates the prime directive of generic programming,
> which is that the qualitative meaning of an operation should not depend
upon
> the type upon which it operates.

Not being privy to the ANSI C++ committee's deliberations, I can only assume
that what is returned by std::numeric_limits<floating-point-type>::min () is
the smallest value with fully significant digits. The 'digits' member
represents the number of radix digits available in the implementation.
Denormalised values have fewer radix digits than normalised values (the
first N digits are always 0), which would invalidate this.

Rather than modify the definition of min(), they provided the has_denorm
member to indicate that the type supports denormalised values. It should
then be possible to construct the 'smallest denormalised value' by dividing
min() by radix^(digits-1).

--
Daniel Pfeffer
--------------
Remove 'nospam' from my address in order to contact me directly
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Ron" <nospam@this-email-address.org>
Date: 1999/10/12
Raw View
Recently I've had an unpleasant experience using std::numeric_limits::min ()
in a template class. According to the C++ specification, this function
returns the specialization's "minimum finite value", which the spec says is
"Equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, etc.". The spec also
notes that "For floating types with denormalization, returns the minimum
positive normalized value".

This formulation creates problems when it comes to generic programming,
because min () returns one kind of thing (the most negative representable
value) when specialized for an integral type, and another kind of thing (the
minimum positive normalized value) when specialized for a floating type.
This inconsistency violates the prime directive of generic programming,
which is that the qualitative meaning of an operation should not depend upon
the type upon which it operates.

This inconsistency also raises the very practical question of whether there
is any portable way for a program to discover the most negative value that
can be represented by a particular numeric type. In my implementation, it so
happens that -std::numeric_limits <...>::max () returns that number for all
floating point types (I checked the bits), but there's no guarantee that
this is so for every implementation. Armed with this fact, I worked around
the problem by extending numeric_limits thusly:

    template <class T> class better_numeric_limits : public
std::numeric_limits <T>
    {
       public:
         static T most_negative (void)
          {
             if (std::numeric_limits <T>::is_integer)
             {
                return std::numeric_limits <T>::min ();
             }
             else
             {
                return -std::numeric_limits <T>::max ();
             }
          };

          static T most_positive (void)
          {
             return std::numeric_limits <T>::max ();
          };
    };

Now, my questions: (1) does anyone know why min () was implemented this way
(other than to maintain consistency with C code which, since C code can't
use numeric_limits anyway, seems a poor reason) and (2) does anyone know a
portable way for a program to determine the most negative value that can be
represented by a particular numeric type?

Thanks,

Ron




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]