finding the minimum allowed value for a type

A

Adam H. Peterson

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
 
D

David Rubin

Adam H. Peterson said:
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).

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
 
M

Mike Wahler

Adam H. Peterson said:
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
 
R

Ron Natalie

Mike Wahler said:
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();
}
 
A

Adam H. Peterson

Mike said:
That's the one. :)




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.
 
A

Adam H. Peterson

David said:
"Adam H. Peterson" wrote:
Is there nothing analogous to DBL_MIN and FLT_MIN in C (limits.h)?
Isn't the minimum value for unsigned types zero?
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
 
A

Adam H. Peterson

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
 
K

Kevin Goodsell

Ron said:
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
 
P

Pete Becker

Adam H. Peterson said:
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.
 
A

Adam H. Peterson

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
 
R

Ron Natalie

Kevin Goodsell said:
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.
 
M

Mike Wahler

Ron Natalie said:
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
 
M

Mike Wahler

Adam H. Peterson said:
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
 
G

Gianni Mariani

Adam said:
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
 
J

Jerry Coffin

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

Frank Schmitt

Jerry Coffin said:
[ ... ]
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
 
G

Gianni Mariani

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.
 
A

Adam H. Peterson

I think you could test numeric_limits said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top