Topic: numeric_limits::min
Author: Pete Becker <pbecker@oec.com>
Date: 1996/10/16 Raw View
Phil Weiss wrote:
>
> I'm wondering if someone can give me the rational behind a 'feature' of
> numeric_limits. The same problem existed in C as well, so this may
> simply be just a continuation of this.
>
> The question question I have is this: why does
> numeric_limits<Type_>::min mean two different things depending on
> whether Type_ is an integer or floating point type? For integer types
> it is the minimum finite value. For floating point types, it is the
> minimum positive normalized value.
>
> I need to do some screening for values to make sure that they are within
> the valid range for a type. Under C, I had to hard code the values in
> (or use limits.h and float.h macros). Under C++ I can parameterize
> this, but I have to check for is_integer first.
>
> Anyway, the rationale behind the difference would be nice.
The difference arises because floating point values are not integers --
they have different properties. Integral values don't necessarily
present uniform ranges, so the minimum value can be different from the
negative of the maximum value. That's why we need to be able to know
both. With floating point, on the other hand, it's much more useful to
know the closest value to 0 that can be represented with full precision,
which is the minimum positive normalized value.
-- Pete
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: ccshan@husc.harvard.edu (Chung-chieh Shan)
Date: 1996/10/17 Raw View
Pete Becker (pbecker@oec.com) wrote:
> The difference arises because floating point values are not integers --
> they have different properties. Integral values don't necessarily
> present uniform ranges, so the minimum value can be different from the
> negative of the maximum value. That's why we need to be able to know
> both. With floating point, on the other hand, it's much more useful to
> know the closest value to 0 that can be represented with full precision,
> which is the minimum positive normalized value.
The question then becomes, since the two "minima" are in fact
different semantically, shouldn't they be named differently
also? For integral values, the "minimum positive normalized
value" would be 1, and for floats, the "minimum" would simply
be minus the maximum. The distinction can become useful if
one considers fixed-point numbers.
--
blue | Ken; Shan, Chung-chieh; Sian7, Tiong1-kiat8; ccshan@fas.harvard.edu.
() | Your code today becomes the mind tomorrow: Your plan its means,
/\ | your dream its ends, your ideal its elegance. Hack on.
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Nathan Myers <ncm@cantrip.org>
Date: 1996/10/17 Raw View
> Phil Weiss wrote:
> >
> > I'm wondering if someone can give me the rational behind a 'feature' of
> > numeric_limits. The same problem existed in C as well, so this may
> > simply be just a continuation of this.
> >
> > The question question I have is this: why does
> > numeric_limits<Type_>::min mean two different things depending on
> > whether Type_ is an integer or floating point type? For integer types
> > it is the minimum finite value. For floating point types, it is the
> > minimum positive normalized value.
> >
> > I need to do some screening for values to make sure that they are within
> > the valid range for a type. Under C, I had to hard code the values in
> > (or use limits.h and float.h macros). Under C++ I can parameterize
> > this, but I have to check for is_integer first.
Pete Becker replied:
> The difference arises because floating point values are not integers --
> they have different properties. Integral values don't necessarily
> present uniform ranges, so the minimum value can be different from the
> negative of the maximum value. That's why we need to be able to know
> both. With floating point, on the other hand, it's much more useful to
> know the closest value to 0 that can be represented with full precision,
> which is the minimum positive normalized value.
The real answer (I was involved in the design of numeric_limits<>) is:
1. We recognized that "least positive" and "smallest" are two different
meanings, and would have liked to give them different names, even though
only one of the names would be very useful for any given type; however,
2. The C standard merges them, and since numeric_limits<> was intended to
be easy to use by those converting from C, we did not want to diverge
too far from its design; anyway,
3. The test for is_integer will be optimized out of your code, so it
won't cost you at runtime; and finally,
4. numeric_limits<> is a (partial) binding of the ISO LIA-1 standard, which
also merges the two kinds of min().
In other words, there was no good reason beyond historical consistency,
and a measure of humility before others more expert than us in matters
numerical.
Nathan Myers
ncm@cantrip.org http://www.cantrip.org/
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: weiss@pacsim.com (Phil Weiss)
Date: 1996/10/16 Raw View
I'm wondering if someone can give me the rational behind a 'feature' of
numeric_limits. The same problem existed in C as well, so this may
simply be just a continuation of this.
The question question I have is this: why does
numeric_limits<Type_>::min mean two different things depending on
whether Type_ is an integer or floating point type? For integer types
it is the minimum finite value. For floating point types, it is the
minimum positive normalized value.
I need to do some screening for values to make sure that they are within
the valid range for a type. Under C, I had to hard code the values in
(or use limits.h and float.h macros). Under C++ I can parameterize
this, but I have to check for is_integer first.
Anyway, the rationale behind the difference would be nice.
Phil.
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]