Live data from Hacker News

Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

bitbucket.org

11–20 of 130 posts

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#11

> The built-in integer types in C++ (int, unsigned, long long, etc.) are mostly unusable because of the lax requirements on bounds. This must be some new definition of the word 'unusable.'

I agree with that statement. How can you use type when you don't know range of values. I want to store year. From 1850 to 2050. What type should I choose? I want to store colour value: from 0 to 2^24-1. What type should I choose?

int, long. The standard specifies their minimum sizes.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#12
post #3

> bounded::integer uses built-in integers as the template parameter to determine its bounds. This means that it cannot store an integer larger than an unsigned 64-bit or unsigned 128-bit integer (depending on the platform) or smaller than a signed 64-bit or signed 128-bit integer. This restriction should be removed at some point in the future. So when an overflow happens here I assume it's a compile time error (UB du…

I am not aware of any compiler or platform where int is 128 bits. The most common these days, even on 64 bit platforms, is 32 bits, which they don't even mention.

I am not sure why they chose to speculate about sizeof(int) in this way, vs. expressing the limit in terms of something more concrete.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#13
post #11

Earlier quoted context omitted.

I agree with that statement. How can you use type when you don't know range of values. I want to store year. From 1850 to 2050. What type should I choose? I want to store colour value: from 0 to 2^24-1. What type should I choose?

int, long. The standard specifies their minimum sizes.

X86 has also had saturation arithmetic since MMX was introduced.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#14
post #3

> bounded::integer uses built-in integers as the template parameter to determine its bounds. This means that it cannot store an integer larger than an unsigned 64-bit or unsigned 128-bit integer (depending on the platform) or smaller than a signed 64-bit or signed 128-bit integer. This restriction should be removed at some point in the future. So when an overflow happens here I assume it's a compile time error (UB du…

I am not aware of any compiler or platform where int is 128 bits. The most common these days, even on 64 bit platforms, is 32 bits, which they don't even mention. I am not sure why they chose to speculate about sizeof(int) in this way, vs. expressing the limit in terms of something more concrete.

The author probably meant the largest available integral type, which can be __int128 on gcc and clang.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#15
post #11

Earlier quoted context omitted.

int, long. The standard specifies their minimum sizes.

X86 has also had saturation arithmetic since MMX was introduced.

You should never rely on the underlying CPU architecture's behavior on integer overflow when you write C or C++. It is undefined and not unspecified or implementation defined.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#16
post #3

> bounded::integer uses built-in integers as the template parameter to determine its bounds. This means that it cannot store an integer larger than an unsigned 64-bit or unsigned 128-bit integer (depending on the platform) or smaller than a signed 64-bit or signed 128-bit integer. This restriction should be removed at some point in the future. So when an overflow happens here I assume it's a compile time error (UB du…

I am not aware of any compiler or platform where int is 128 bits. The most common these days, even on 64 bit platforms, is 32 bits, which they don't even mention. I am not sure why they chose to speculate about sizeof(int) in this way, vs. expressing the limit in terms of something more concrete.

[deleted]

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#17
post #3

> bounded::integer uses built-in integers as the template parameter to determine its bounds. This means that it cannot store an integer larger than an unsigned 64-bit or unsigned 128-bit integer (depending on the platform) or smaller than a signed 64-bit or signed 128-bit integer. This restriction should be removed at some point in the future. So when an overflow happens here I assume it's a compile time error (UB du…

I am not aware of any compiler or platform where int is 128 bits. The most common these days, even on 64 bit platforms, is 32 bits, which they don't even mention. I am not sure why they chose to speculate about sizeof(int) in this way, vs. expressing the limit in terms of something more concrete.

How are you envisioning this library being used?

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#18

Maybe Ada's approach of every number type requiring explicit bounds is a good one.

Or Lisp’s approach of just using ”big” ints by default (which is practically the same but with an implicit default of “infinity”). And trust the compiler to use fixints internally when the programmer has declared it safe to do so.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#20
post #11

Earlier quoted context omitted.

int, long. The standard specifies their minimum sizes.

X86 has also had saturation arithmetic since MMX was introduced.

.... However, unless you use non portable intrinsics, you can't make the compiler use MMX. So in practice you get wrap on overflow.

Worse, the compiler is entitled to use saturation arithmetic if you've assumed wrapping.

Post reply on HN