473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

Jul 19 '05 #1
17 5322
"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
"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

"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
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
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
> 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
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
"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

"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
> 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

"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

"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
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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally....
4
by: Porthos | last post by:
Hi All, I'm trying to find the minimum value of a set of data (see below). I want to compare the lengths of these attribute values and display the lowest one. This would be simple if I could...
2
by: slickn_sly | last post by:
int find_index_of_min( float num, int arraySize ) { int index, min; min = 0; for( index = 1; index < arraySize; index++ ) { if( num > num ) { min = index;
6
by: graham.macpherson | last post by:
I have 2 Suse Linux PCs which I compile my code on. Until recently they both had gcc 4.0.X on them, but I upgraded one of them to gcc 4.1.0. I have come across a very strange problem in the...
6
by: Derek Peschel | last post by:
Here are two functions. def invert_dict_to_lists(dict): lists = {} for key in dict: value = dict if not value in lists: lists = else: lists.append(key)
22
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
7
by: junky_fellow | last post by:
Guys, Does the standard allow to an implementation to chose any maximum and minimum value for an object of type int ? For eg. Can an implementation with sizeof(int)=4, chose to have value of...
25
by: Daniel Kraft | last post by:
Hi, I do need to implement something similar to C++'s std::bitset in C; for this, I use an array of int's to get together any desired number of bits, possibly larger than 32/64 or anything like...
13
by: Ioannis Vranos | last post by:
Is there any mentioning in the standard of the number of bits of the various built in types, apart from char/signed char/unsigned char types? Or only about the minimum value ranges of them?
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.