By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

finding the minimum allowed value for a type

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
"Adam H. Peterson" <ah**@email.byu.edu> wrote in message
news:bl***********@acs2.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?
I can't use
(-std::numeric_limits<T>::max()) either,
Why would you want to do that?
because that won't work for
unsigned types
Well, it doesn't make much sense to try to negate
an unsigned type.
(and technically won't work for signed integer types and
others either,
What have you tried that "wont' work"?
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.
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
Jul 19 '05 #2

P: n/a
"Adam H. Peterson" wrote:

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)?
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?
(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.
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
Jul 19 '05 #3

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:ua****************@newsread3.news.pas.earthli nk.net...
"Adam H. Peterson" <ah**@email.byu.edu> wrote in message
news:bl***********@acs2.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();
}
Jul 19 '05 #4

P: n/a
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?

Thanks for the response.

Adam H. Peterson

Jul 19 '05 #5

P: n/a
Mike Wahler wrote:
"Adam H. Peterson" <ah**@email.byu.edu> wrote in message
news:bl***********@acs2.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.

Jul 19 '05 #6

P: n/a
> 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<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(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

Jul 19 '05 #7

P: n/a
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?

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

Jul 19 '05 #8

P: n/a
"Adam H. Peterson" wrote:
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<array[size])
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)
Jul 19 '05 #9

P: n/a

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:S1*****************@newsread3.news.pas.earthl ink.net...
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.
Jul 19 '05 #10

P: n/a
> 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

Jul 19 '05 #11

P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:ua****************@newsread3.news.pas.earthli nk.net...
"Adam H. Peterson" <ah**@email.byu.edu> wrote in message
news:bl***********@acs2.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.
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
Jul 19 '05 #12

P: n/a

"Adam H. Peterson" <ah**@email.byu.edu> wrote in message
news:bl***********@acs2.byu.edu...
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
Jul 19 '05 #13

P: n/a
Adam H. Peterson wrote:
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<T, N>::value/2 == static_cast<T>( 0 ) >
{
};

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 << "Float\n";
std::cout << min_limit< float >::min_exponent << "\n";
std::cout << min_limit< float >::min_value << "\n";

std::cout << "Double\n";
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

Jul 19 '05 #14

P: n/a
In article <bl********@dispatch.concentric.net>, gi*******@mariani.ws
says...

[ ... ]
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.
Jul 19 '05 #15

P: n/a
Jerry Coffin <jc*****@taeus.com> writes:
In article <bl********@dispatch.concentric.net>, gi*******@mariani.ws
says...

[ ... ]
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.


Hm. gcc 3.3 seems to take longer than gcc 3.2. With
g++3 -ftemplate-depth-10000 -Wreturn-type -W -Wpointer-arith -pipe -ggdb
it compiled in 17 seconds with gcc 3.3.1 (RH Linux 7.3) on a 2.2 GHz P4.

regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #16

P: n/a
Frank Schmitt wrote:
....

Hm. gcc 3.3 seems to take longer than gcc 3.2. With
g++3 -ftemplate-depth-10000 -Wreturn-type -W -Wpointer-arith -pipe -ggdb
it compiled in 17 seconds with gcc 3.3.1 (RH Linux 7.3) on a 2.2 GHz P4.


It turns out the machine I used to run the compiler had a runaway
process in the background - I wondered why it was sliggish ...

New processing time is :
31.050u 0.690s 0:32.96 96.2% 0+0k 0+0io 4206pf+0w

Which is inline with expectations.

Still, 20-30 seconds to compile is a bit excessive.

The other issue is that the debug information is also excessive - I'm
not sure what impact that would have except long link times and lots of
disk usage.

Jul 19 '05 #17

P: n/a
> 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


Thanks. I think what Ron wrote will meet my needs. (For some reason,
his post doesn't show up on my news server, and I can only see it
through Google groups. Oh, well.) It's nice that there seems to be a
relatively simple portable standard solution.

Although, if I may say so, it seems to me like the specialized behavior
of min() for floating point types is less than helpful for generic
programming, and it would have been better to have another
numeric_limits member serve that purpose, giving min() a more consistent
usable behavior.

Thanks for the responses, all.

Adam H. Peterson

Jul 19 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.