473,788 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Max value of an integer type?

I'm writing a template, and I need to know the maximum value of a given
integer type at compile time. something like:

template<class NumType>
class Arb {
public:

static NumType const max = /* maximum value */;

};
If the template were only to be used with unsigned integer types, then
I'd do the following:

template<class NumType>
class Arb {
public:

static NumType const max = -1;

};
I need a compile-time constant which evaluates to the maximum value of an
integer type.

Any ideas?

If it weren't undefined behaviour to overflow a signed integer, I could
use a metaprogramming technique such as the following:

template<class T, bool overflow = false>
struct MaxIntVal {
private:

static T const internal = 1 + MaxIntVal<T,...

public:

static T const val = SCHAR_MAX + internal;
};
--

Frederick Gotham
Jul 4 '06
35 12588

"Frederick Gotham" <fg*******@SPAM .comskrev i meddelandet
news:F9******** ***********@new s.indigo.ie...
posted:
>>
Frederick Gotham wrote:
>>template< class T,
T shift_by,
bool no_more_digits = shift_by == std::numeric_li mits
<T>::digits
>
...

Although this would probably work 99% of the time, I think
technically
that the standard does not require 2s compliment arithmetic (or
whatever it is called) - ie you can't assume setting all bits to 1
gives you the largest value.


"digits" gives you the amount of value bits, excluding the sign bit.

If the sign bit (if any) is zero, and if all the rest of the bits
are 1,
then you have the max value.
This of course assumes that all the bits take part in the value
representation. Not a requirement.

You are now at 99.5% mark, I guess. :-)
There is a reason for the seemingly redundant values in
numeric_limits - implementations are allowed to have unusual values
for some of them.
Bo Persson
Jul 5 '06 #11
Bo Persson posted:
This of course assumes that all the bits take part in the value
representation. Not a requirement.

18.2.1.2 numeric_limits members

static const int digits;

6 Number of radix digits that can be represented without change.
7 For built-in integer types, the number of non-sign bits in the
representation.
--

Frederick Gotham
Jul 5 '06 #12
Bo Persson wrote:
>"digits" gives you the amount of value bits, excluding the sign bit.

If the sign bit (if any) is zero, and if all the rest of the bits
are 1,
then you have the max value.

This of course assumes that all the bits take part in the value
representation.
AFAICS, it doesn't. As Frederick wrote, "digits" gives you the amount of
_value bits_, not the total amount of bits.

Jul 5 '06 #13
* Frederick Gotham:
=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?= posted:

>If you can use numeric_limits: :digits, wouldn't this be simpler?

template <typename T>
struct intinfo {
static const T max = (static_cast<T> (1) <<
std::numeric_l imits<T>::digit s - 1) - 1 << 1 | 1;
};

Or are there some special cases this doesn't handle correctly?


That's brilliant. Here's the updated code:

#include<limits >

template<class T>
struct IntMax {
private:

static T const MSBOnly = static_cast<T>( 1) << std::numeric_li mits
<T>::digits - 1;

public:

static T const val = MSBOnly | MSBOnly - 1;

};
It's good fun to throw academic spanners into practical wheels, so,
where does the standard exclude a grey-code representation? ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #14
Alf P. Steinbach posted:

It's good fun to throw academic spanners into practical wheels, so,
where does the standard exclude a grey-code representation? ;-)

Only caveat that comes to mind is negative zero... but that shouldn't be an
issue here.

Unless you're talking about left-shifting a signed integer type... ?
(Not quite sure what you mean by "exclude a grey-code representation" )
--

Frederick Gotham
Jul 5 '06 #15
* Frederick Gotham:
Alf P. Steinbach posted:

>It's good fun to throw academic spanners into practical wheels, so,
where does the standard exclude a grey-code representation? ;-)


Only caveat that comes to mind is negative zero... but that shouldn't be an
issue here.

Unless you're talking about left-shifting a signed integer type... ?
(Not quite sure what you mean by "exclude a grey-code representation" )
Frederick, I give you also (as I gave Protoman)... WIKIPEDIA!

<url: http://en.wikipedia.or g/wiki/Grey_code>

Note the bit pattern for the highest value when using a given number of
bits.

Sorry for the typo.

Coming from a whaling country, I naturally prefer Earl Grey tea...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #16
Alf P. Steinbach posted:
Note the bit pattern for the highest value when using a given number of
bits.

I was under the assumption that C++ had to store positive values as
follows:

0: 0000
1: 0001
2: 0010
3: 0011
4: 0100
5: 0101
6: 0110
7: 0111
8: 1000
9: 1001
10: 1010
11: 1011
12: 1100
13: 1101
14: 1110
15: 1111

Are you saying that:

(A) The machine could use another method such as "Gray code".
and
(B) My code could break on other systems, and thus isn't trully
portable?
--

Frederick Gotham
Jul 5 '06 #17
Alf P. Steinbach wrote:
It's good fun to throw academic spanners into practical wheels, so,
where does the standard exclude a grey-code representation? ;-)
Hmm, I would have said 3.9.1/7, but after reading it again, it seems that
this actually doesn't prohibit a grey-code representation.

Jul 5 '06 #18
Rolf Magnus wrote:
Alf P. Steinbach wrote:
>It's good fun to throw academic spanners into practical wheels, so,
where does the standard exclude a grey-code representation? ;-)

Hmm, I would have said 3.9.1/7, but after reading it again, it seems that
this actually doesn't prohibit a grey-code representation.
Ok, now I have one: 5.8/2.

Jul 5 '06 #19
Rolf Magnus posted:

Ok, now I have one: 5.8/2.
The value of E1 << E2 is E1 (interpreted as a bit pattern) left-shifted E2
bit positions; vacated bits are zero-filled. If E1 has an unsigned type,
the value of the result is E1 multiplied by the quantity 2 raised to the
power E2, reduced modulo ULONG_MAX+1 if E1 has type unsigned long,
UINT_MAX+1 otherwise.
Is it the "vacated bits are zero-filled" part that you're talking about?

--

Frederick Gotham
Jul 5 '06 #20

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

Similar topics

20
4851
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
4
1831
by: Semi Head | last post by:
Hello folks, I'm looking for a script to validate a specific number value in a standard form input field. An example would be, if someone enters a number into a form input, I want the script to validate it and give an alert if that the number exceeds the set script value. Like if the script value is set for 3000 and the number 3002 is entered, I want an alert to pop and give a warning, BTW - This script must be generic enough to work...
12
3222
by: Francois Grieu | last post by:
The values of ((int)0.7) and ((int)-0.7) seem to be 0 Is this independent of implementation ? TIA, François Grieu
4
2561
by: Chris Bower | last post by:
Reposted from aspnet.buildingcontrols: Ok, I've got a bunch of derived controls that all have a property Rights of type Rights (Rights is an Enumerator). I wrote a custom TypeConverter so that I can use comma separated values in design-time. The TypeConverter works great in design-time. It converts to and from just fine... However, when I try to load any page in the site now I get the following error (Following the error is code for the...
2
7213
by: Jim in Arizona | last post by:
I'm learning form an ASP.NET 1.0 book and I tried out some code that returns this error: Compiler Error Message: BC30311: Value of type 'Integer' cannot be converted to 'ASP.multiclasses_aspx.VehicleKey'. Source Error: Line 106:
2
1683
by: Arne | last post by:
Will the dataset below be returned by value or reference? Public Shared Function getDS() As DataSet Dim ds As New DataSet '... do something Return ds End Function
20
3720
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If MyVariant = 2 Then MsgBox "MyVariant also equals the value 2" 160 If MyVariant = "" Then HowManyCopies = 1 170 If Not IsNumeric(MyVariant) Then HowManyCopies = 1 MsgBox "OK. HowManyCopies has a value of " & CStr(HowManyCopies) 180 For i =...
23
2197
by: Tomás | last post by:
Anything wrong with the following code?: #include <cstdlib> int main() { for (unsigned i = 0; i != 1000; ++i) { int *p = reinterpret_cast<int*>( std::rand() );
22
27077
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);
14
1772
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.