473,385 Members | 2,014 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,385 software developers and data experts.

Min and max of a POD

What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.

Jul 2 '08 #1
17 1662
On Jul 2, 5:24*pm, "Adem24" <ade...@adem24adem24.org.invalidwrote:
What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.
Why do you not want to use external definitions?
An alternative to those constants of course are from limits
i.e. numeric_limits<int>::max()

Joe Cook
Jul 2 '08 #2
On Jul 2, 5:24*pm, "Adem24" <ade...@adem24adem24.org.invalidwrote:
What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.
Would you be willing to make an assumption such as a two's complement
representation? Without some assumption like that, I see no way to
query the min/max without external definitions. With such an
assumption, the signed-ness can be queried without external
definitions as well.

-Howard
Jul 2 '08 #3
joseph cook wrote:
On Jul 2, 5:24 pm, "Adem24" <ade...@adem24adem24.org.invalidwrote:
>What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.

Why do you not want to use external definitions?
An alternative to those constants of course are from limits
i.e. numeric_limits<int>::max()

Joe Cook
Just for the record, numeric_limits<int>::max() won't work under Visual
C++ because it defines two macros called "min" and "max", so the
preprocessor will try to expand the macro and the compiler will fail. It
really sucks.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
http://www.usenet.com
Jul 2 '08 #4
"Howard Hinnant" wrote:
"Adem24" wrote:

What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.
Would you be willing to make an assumption such as a two's complement
representation? Without some assumption like that, I see no way to
query the min/max without external definitions. With such an
assumption, the signed-ness can be queried without external
definitions as well.
How would a solution for a two's complement representation look like?

Jul 2 '08 #5
On 3 Jul, 00:28, Fernando Gómez <fernando.a.gome...@gmail.comwrote:
>
Just for the record, numeric_limits<int>::max() won't work under Visual
C++ because it defines two macros called "min" and "max", so the
preprocessor will try to expand the macro and the compiler will fail. It
really sucks.
Only if you include a non-standard header that defines those macros.

DP
Jul 3 '08 #6
On 2 Jul, 23:24, "Adem24" <ade...@adem24adem24.org.invalidwrote:
What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.
For the unsigned types, a conversion from -1 should yield the maximum
value.

unsigned u = -1;

DP
Jul 3 '08 #7
Fernando Gómez wrote:
Just for the record, numeric_limits<int>::max() won't work under Visual
C++ because it defines two macros called "min" and "max", so the
preprocessor will try to expand the macro and the compiler will fail. It
really sucks.
Just for the record, define NOMINMAX before including the Windows
headers, as described in MS KB article 143208.
Jul 3 '08 #8
On Jul 3, 12:28 am, Fernando Gómez <fernando.a.gome...@gmail.com>
wrote:
joseph cook wrote:
On Jul 2, 5:24 pm, "Adem24" <ade...@adem24adem24.org.invalidwrote:
What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.
Why do you not want to use external definitions?
An alternative to those constants of course are from limits
i.e. numeric_limits<int>::max()
Just for the record, numeric_limits<int>::max() won't work
under Visual C++ because it defines two macros called "min"
and "max", so the preprocessor will try to expand the macro
and the compiler will fail. It really sucks.
Funny, I don't have that problem when I compile with VC++. Of
course, like most compilers, by default, it doesn't compile C++,
but only something rather similar, but if you give it the
necessary options, there's no problem. (As a minimum, I think
you need -vmg -GR -EHs -Zc:forScope,wchar_t -DNOMINMAX
-D_CRT_SECURE_NO_DEPRECATE. Some of these may not be necessary
with more recent versions of the compiler, however, and there
may be others which I've missed---since I don't use this
compiler professionally, I'm not fully up to date with it.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 3 '08 #9
On Jul 3, 12:23 am, Howard Hinnant <howard.hinn...@gmail.comwrote:
On Jul 2, 5:24 pm, "Adem24" <ade...@adem24adem24.org.invalidwrote:
What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.
Would you be willing to make an assumption such as a two's
complement representation? Without some assumption like that,
I see no way to query the min/max without external
definitions. With such an assumption, the signed-ness can be
queried without external definitions as well.
For the unsigned, the representation of negative numbers is
irrelevant: the minimum is always 0, and the maximum
"~static_cast< Type >( 0 )" (or "static_cast< Type >( 0 ) - 1",
whichever you prefer).

For most systems, the maximum of a signed value can be obtained
by taking the maximum of the corresponding unsigned (as above),
then shifting it right 1 (still as unsigned). The minimum is a
bit tricky, but you should be able to do it by testing the 2
lower bits of -1, to add the special handling for 1's complement
and signed magnitude: I think the following should work:

- maximum - (static_cast< Type >( -1 ) & 3) == 3)

The only problem is on machines where the maximum value of an
unsigned is equal to the maximum value of the corresponding
signed. (The only two possibilities known to me are that the
unsigned uses the sign bit as part of its value, meaning that
shifting its max one to the right gives the max of a signed, or
that it ignores the sign bit, so that the max of the signed is
equal to the max of the unsigned. Regretfully, I don't know of
any way to determine which one holds, other than by using
external information. On the other hand, the only processor I
know where the unsigned simply ignore the sign bit is the Unisys
MCP, which isn't really a consideration for most people.)

In practice, of course, I use the constants defined in <climits>
if I need an integral constant expression, and the class
template numeric_limits (from <limits>) if I need the value for
a template argument. (And I wait until the next release of the
standard if I need both:-).)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 3 '08 #10
Adem24 wrote:
What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.
Templates might work.

template <typename T, T V, T W, T X, T Y, bool Z=true>
struct max_val
{
static const T newv = V << 1;
static const T neww = W + newv;

static const T val = max_val<T, newv, neww, V, W, (newv V)>::val;

};

template <typename T, T V, T W, T X, T Y>
struct max_val<T, V, W, X, Y, false>
{
static const T val = Y;
};

template <typename T>
struct maxof
{
static const T val = max_val<T,1,1,0,0,true>::val;
};
#include <iostream>

int main()
{
std::cout << "int " << maxof<int>::val << "\n";
std::cout << "unsigned int " << maxof<unsigned int>::val << "\n";
std::cout << "short " << maxof<short>::val << "\n";
std::cout << "char " << int(maxof<char>::val) << "\n";

}
Jul 3 '08 #11
"Gianni Mariani" <gi*******@mariani.wswrote:
Adem24 wrote:
What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.

Templates might work.

template <typename T, T V, T W, T X, T Y, bool Z=true>
struct max_val
{
static const T newv = V << 1;
static const T neww = W + newv;
static const T val = max_val<T, newv, neww, V, W, (newv V)>::val;
};

template <typename T, T V, T W, T X, T Y>
struct max_val<T, V, W, X, Y, false>
{
static const T val = Y;
};

template <typename T>
struct maxof
{
static const T val = max_val<T,1,1,0,0,true>::val;
};
#include <iostream>
int main()
{
std::cout << "int " << maxof<int>::val << "\n";
std::cout << "unsigned int " << maxof<unsigned int>::val << "\n";
std::cout << "short " << maxof<short>::val << "\n";
std::cout << "char " << int(maxof<char>::val) << "\n";
}

This code seems to be too complicated for the Microsoft C++ compiler: :-)

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
C:\PRJ\test\test.cpp(3) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1794)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Command line warning D4028 : minimal rebuild failure, reverting to normal build
Error executing cl.exe.
Creating browse info file...
BSCMAKE: warning BK4503 : minor error in .SBR file '.\Debug\test.sbr' ignored

test.exe - 1 error(s), 2 warning(s)

Jul 3 '08 #12
Eberhard Schefold wrote:
Fernando Gómez wrote:
>Just for the record, numeric_limits<int>::max() won't work under
Visual C++ because it defines two macros called "min" and "max", so
the preprocessor will try to expand the macro and the compiler will
fail. It really sucks.

Just for the record, define NOMINMAX before including the Windows
headers, as described in MS KB article 143208.
IIRC, there are some MFC and WTL classes that won't work if NOMINMAX is
defined. But for the most part, that'll do, yes.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
http://www.usenet.com
Jul 3 '08 #13
Fernando Gómez wrote:
IIRC, there are some MFC and WTL classes that won't work if NOMINMAX is
defined. But for the most part, that'll do, yes.
If that happens, declare

using std::max;
using std::min;

before including the headers, so they'll find the version from the
standard library.
Jul 3 '08 #14
"Gianni Mariani" <gi*******@mariani.wswrote:
Adem24 wrote:

What is the best way to get the minimum value
and the maximum value an integral POD can hold
without using any external definitions like INT_MIN, INT_MAX etc.?
The type (short, int, long, etc.) plus signed/unsigned is known.

Templates might work.

template <typename T, T V, T W, T X, T Y, bool Z=true>
struct max_val
{
static const T newv = V << 1;
static const T neww = W + newv;
static const T val = max_val<T, newv, neww, V, W, (newv V)>::val;
};

template <typename T, T V, T W, T X, T Y>
struct max_val<T, V, W, X, Y, false>
{
static const T val = Y;
};
I wonder if and when the 2nd max_val ever gets used at all...?

template <typename T>
struct maxof
{
static const T val = max_val<T,1,1,0,0,true>::val;
};
#include <iostream>
int main()
{
std::cout << "int " << maxof<int>::val << "\n";
std::cout << "unsigned int " << maxof<unsigned int>::val << "\n";
std::cout << "short " << maxof<short>::val << "\n";
std::cout << "char " << int(maxof<char>::val) << "\n";
}
Jul 3 '08 #15
On Jul 4, 5:52*am, "Adem24" <ade...@adem24adem24.org.invalidwrote:
"Gianni Mariani" <gi4nos...@mariani.wswrote:
I wonder if and when the 2nd max_val ever gets used at all...?
when (newv V) is false.

It's the terminating condition.

Jul 3 '08 #16
"Gianni Mariani" <ow*******@gmail.comwrote:
"Adem24" wrote:
"Gianni Mariani" wrote:
I wonder if and when the 2nd max_val ever gets used at all...?

when (newv V) is false.
It's the terminating condition.
And it really works on your machine?
Is the code you posted here correct or are any modification neccessary?
Which compiler and on which platform did you test it?

Jul 4 '08 #17
On Jul 4, 10:07 am, "Adem24" <ade...@adem24adem24.org.invalidwrote:
"Gianni Mariani" <owebee...@gmail.comwrote:
"Adem24" wrote:
"Gianni Mariani" wrote:
I wonder if and when the 2nd max_val ever gets used at all...?
when (newv V) is false.
It's the terminating condition.

And it really works on your machine?
yes.
Is the code you posted here correct or are any modification neccessary?
afaict, yes.
Which compiler and on which platform did you test it?
I used gcc on cygwin (don't have machine with me so don't know gcc
version).
Also tested it just now on linux boxen with gcc 4.2.2.
Comeau says it's OK as well but has some overflow warnings which are
to be expected since this is a case of find out when the overflow
happens.

Comeau C/C++ 4.3.10.1 (May 29 2008 09:37:15) for
ONLINE_EVALUATION_BETA1
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 4: warning: integer conversion resulted in a
change of sign
static const T newv = V << T(1);
^
detected during:
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)16384, W=(short)32767, X=(short)8192,
Y=(short)16383, Z=true]" at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)8192, W=(short)16383, X=(short)4096,
Y=(short)8191, Z=true]" at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)4096, W=(short)8191, X=(short)2048,
Y=(short)4095, Z=true]" at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)2048, W=(short)4095, X=(short)1024,
Y=(short)2047, Z=true]" at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)1024, W=(short)2047, X=(short)512,
Y=(short)1023, Z=true]" at line 7
[ 6 instantiation contexts not shown ]
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)8, W=(short)15, X=(short)4, Y=(short)7,
Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)4, W=(short)7, X=(short)2, Y=(short)3,
Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)2, W=(short)3, X=(short)1, Y=(short)1,
Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=short,
V=(short)1, W=(short)1, X=(short)0, Y=(short)0,
Z=true]"
at line 21
instantiation of class "maxof<T[with T=short]" at line
31

"ComeauTest.c", line 4: warning: integer conversion resulted in a
change of sign
static const T newv = V << T(1);
^
detected during:
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=char,
V='@', W='\177', X=' ', Y='?', Z=true]" at line
7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=char,
V=' ', W='?', X='\020', Y='\037', Z=true]" at
line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=char,
V='\020', W='\037', X='\b', Y='\017', Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=char,
V='\b', W='\017', X='\004', Y='\a', Z=true]" at
line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=char,
V='\004', W='\a', X='\002', Y='\003', Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=char,
V='\002', W='\003', X='\001', Y='\001', Z=true]"
at line
7
instantiation of class "max_val<T, V, W, X, Y, Z[with
T=char,
V='\001', W='\001', X='\000', Y='\000', Z=true]"
at line
21
instantiation of class "maxof<T[with T=char]" at line 32
Jul 4 '08 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.