 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Adam H. Peterson Guest
|
Posted: Fri Sep 26, 2003 5:25 pm Post subject: 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_limits<T>::min(). But that
doesn't work for floating point types. I can't use
(-std::numeric_limits<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_limits<>::min() and just specialize it for floats/doubles?
Or is this already solved somewhere in the library?
Thanks,
Adam H. Peterson
|
|
| Back to top |
|
 |
David Rubin Guest
|
Posted: Fri Sep 26, 2003 7:05 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
"Adam H. Peterson" wrote:
| Quote: |
Is there a standard way to find the minimum value for a data type? I'm
thinking along the lines of std::numeric_limits<T>::min(). But that
doesn't work for floating point types.
|
Is there nothing analogous to DBL_MIN and FLT_MIN in C (limits.h)?
| Quote: | I can't use
(-std::numeric_limits<T>::max()) either, because that won't work for
unsigned types
|
Isn't the minimum value for unsigned types zero?
| Quote: | (and technically won't work for signed integer types and
others either, although it would probably be "good enough" for them).
|
Signed integer types can use std::numeric_limits<T>::min() as you
pointed out.
| Quote: | Do I need to implement a new template that uses
std::numeric_limits<>::min() and just specialize it for floats/doubles?
Or is this already solved somewhere in the library?
|
/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
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Fri Sep 26, 2003 7:10 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
"Adam H. Peterson" <ahp6 (AT) email (DOT) byu.edu> wrote
| Quote: | Is there a standard way to find the minimum value for a data type? I'm
thinking along the lines of std::numeric_limits<T>::min().
|
That's the one. :-)
| Quote: | But that
doesn't work for floating point types.
|
Sure it does. What have you tried?
| Quote: | I can't use
(-std::numeric_limits<T>::max()) either,
|
Why would you want to do that?
| Quote: | because that won't work for
unsigned types
|
Well, it doesn't make much sense to try to negate
an unsigned type.
| Quote: | (and technically won't work for signed integer types and
others either,
|
What have you tried that "wont' work"?
| Quote: | although it would probably be "good enough" for them).
Do I need to implement a new template that uses
std::numeric_limits<>::min() and just specialize it for floats/doubles?
|
The standard library already specializes the template
for all scalar types except pointers.
| Quote: | Or is this already solved somewhere in the library?
|
It's already done. Why not post a small, compilable
program that demonstrates the trouble you're having?
-Mike
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Sep 26, 2003 7:17 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote
| Quote: | "Adam H. Peterson" <ahp6 (AT) email (DOT) byu.edu> wrote in message
news:bl1sq5$2tah$1 (AT) acs2 (DOT) byu.edu...
Is there a standard way to find the minimum value for a data type? I'm
thinking along the lines of std::numeric_limits<T>::min().
That's the one. :-)
|
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_limits<T>::is_integer) return numeric_limits<T>::min();
else return -numeric_limits<T>::max();
}
|
|
| Back to top |
|
 |
Adam H. Peterson Guest
|
Posted: Fri Sep 26, 2003 7:25 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
Mike Wahler wrote:
| Quote: | "Adam H. Peterson" <ahp6 (AT) email (DOT) byu.edu> wrote in message
news:bl1sq5$2tah$1 (AT) acs2 (DOT) byu.edu...
Is there a standard way to find the minimum value for a data type? I'm
thinking along the lines of std::numeric_limits<T>::min().
That's the one. :-)
But that
doesn't work for floating point types.
Sure it does. What have you tried?
|
For floating point types, it returns the minimum number greater than
zero, which is not what I want.
|
|
| Back to top |
|
 |
Adam H. Peterson Guest
|
Posted: Fri Sep 26, 2003 7:28 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
David Rubin wrote:
| Quote: | "Adam H. Peterson" wrote:
snip
Is there nothing analogous to DBL_MIN and FLT_MIN in C (limits.h)?
snip
Isn't the minimum value for unsigned types zero?
snip
Signed integer types can use std::numeric_limits<T>::min() as you
pointed out.
|
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
|
|
| Back to top |
|
 |
Adam H. Peterson Guest
|
Posted: Fri Sep 26, 2003 7:44 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
| Quote: | It's already done. Why not post a small, compilable
program that demonstrates the trouble you're having?
|
Here you go, and thanks for taking the time to respond:
#include <limits>
#include <iostream>
#include <ostream>
template<typename T>
void find_min_max(T const *array, int unsigned size, T &min, T &max) {
min=std::numeric_limits<T>::max();
max=std::numeric_limits<T>::min();
while (size--) {
if (min>array[size])
min=array[size];
if (max
max=array[size];
}
}
int main() {
double sampledata[]={
-1.0,
-2.0,
-4.5,
-0.5,
-3.5
};
double min,max;
find_min_max(sampledata, 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
|
|
| Back to top |
|
 |
Kevin Goodsell Guest
|
Posted: Fri Sep 26, 2003 8:09 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
Ron Natalie wrote:
| Quote: |
What he wants is:
template <typename T> T real_min() {
if(numeric_limits<T>::is_integer) return numeric_limits<T>::min();
else return -numeric_limits<T>::max();
|
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.
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Fri Sep 26, 2003 8:09 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
"Adam H. Peterson" wrote:
| Quote: |
It's already done. Why not post a small, compilable
program that demonstrates the trouble you're having?
Here you go, and thanks for taking the time to respond:
#include <limits
#include
#include
template
void find_min_max(T const *array, int unsigned size, T &min, T &max) {
min=std::numeric_limits
max=std::numeric_limits<T>::min();
while (size--) {
if (min>array[size])
min=array[size];
if (max
max=array[size];
}
}
|
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)
|
|
| Back to top |
|
 |
Adam H. Peterson Guest
|
Posted: Fri Sep 26, 2003 8:23 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
| Quote: | 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.
|
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
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Sep 26, 2003 8:39 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
"Kevin Goodsell" <usenet1.spamfree.fusion (AT) neverbox (DOT) com> wrote
| Quote: | Ron Natalie wrote:
What he wants is:
template <typename T> T real_min() {
if(numeric_limits<T>::is_integer) return numeric_limits<T>::min();
else return -numeric_limits<T>::max();
Is there a requirement that the smallest (most negative) value for a
floating point type is the negation of the largest value?
|
Probably not, but I don't think I've ever encountered a floating point
format where that wasn't the case.
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sat Sep 27, 2003 4:56 am Post subject: Re: finding the minimum allowed value for a type |
|
|
"Ron Natalie" <ron (AT) sensor (DOT) com> wrote
| Quote: |
"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote
"Adam H. Peterson" <ahp6 (AT) email (DOT) byu.edu> wrote in message
news:bl1sq5$2tah$1 (AT) acs2 (DOT) byu.edu...
Is there a standard way to find the minimum value for a data type?
I'm
thinking along the lines of std::numeric_limits<T>::min().
That's the one. :-)
No it's not. numeric_limits<double>::min() reports the smallest
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.
| Quote: | What he wants is the mininum representable (numerically).
What he wants is:
template <typename T> T real_min() {
if(numeric_limits<T>::is_integer) return numeric_limits<T>::min();
else return -numeric_limits<T>::max();
}
|
So I've learned two things:
1. numeric_limits<double>::min() returns smallest *positive* value
2. Pay closer attention to detail :-)
-Mike
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sat Sep 27, 2003 5:01 am Post subject: Re: finding the minimum allowed value for a type |
|
|
"Adam H. Peterson" <ahp6 (AT) email (DOT) byu.edu> wrote
| Quote: | David Rubin wrote:
"Adam H. Peterson" wrote:
snip
Is there nothing analogous to DBL_MIN and FLT_MIN in C (limits.h)?
snip
Isn't the minimum value for unsigned types zero?
snip
Signed integer types can use std::numeric_limits<T>::min() as you
pointed out.
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?
|
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
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sat Sep 27, 2003 7:23 am Post subject: Re: finding the minimum allowed value for a type |
|
|
Adam H. Peterson wrote:
| Quote: | Is there a standard way to find the minimum value for a data type? I'm
thinking along the lines of std::numeric_limits<T>::min(). But that
doesn't work for floating point types. I can't use
(-std::numeric_limits<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_limits<>::min() and just specialize it for floats/doubles?
Or is this already solved somewhere in the library?
|
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
{
};
template<typename 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 << "Floatn";
std::cout << min_limit< float >::min_exponent << "n";
std::cout << min_limit< float >::min_value << "n";
std::cout << "Doublen";
std::cout << min_limit< double >::min_exponent << "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_CALLS
-DBUILD_VERSION=0309262338-i686-uluru-gx86-gianni -DBUILD_ARCH=gx86
-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
|
|
| Back to top |
|
 |
Jerry Coffin Guest
|
Posted: Sat Sep 27, 2003 12:06 pm Post subject: Re: finding the minimum allowed value for a type |
|
|
In article <bl3du4$45a (AT) dispatch (DOT) concentric.net>, [email]gi2nospam (AT) mariani (DOT) ws[/email]
says...
[ ... ]
| Quote: | 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.
|
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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|