Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Unsigned integer wrapping on overflow

Reply
Thread Tools

Unsigned integer wrapping on overflow

 
 
James Kanze
Guest
Posts: n/a
 
      08-28-2010
On Aug 28, 4:45 pm, Juha Nieminen <(E-Mail Removed)> wrote:
> joe <(E-Mail Removed)> wrote:
> > Wouldn't it be better if unsigned integers saturated upon overflow
> > instead of wrapping?


> Would make implenting a linear congruential generator harder.


Only bad ones, for random numbers. But I do count on the
current semantics for hash code generation.

--
James Kanze
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-28-2010
James Kanze wrote:

> On Aug 28, 4:45 pm, Juha Nieminen <(E-Mail Removed)> wrote:
>> joe <(E-Mail Removed)> wrote:
>> > Wouldn't it be better if unsigned integers saturated upon overflow
>> > instead of wrapping?

>
>> Would make implenting a linear congruential generator harder.

>
> Only bad ones, for random numbers.


Actually, line 26 in Table 1 (Knuth: TAOCP, Vol 2, section 3.3.4) has
modulus 2^64 and is a pretty good RNG.

> But I do count on the current semantics for hash code generation.


In implementing an unlimited integer type, wrapping also comes in handy.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
joe
Guest
Posts: n/a
 
      08-29-2010
James Kanze wrote:
> On Aug 27, 8:28 pm, "joe" <(E-Mail Removed)> wrote:
>> Wouldn't it be better if unsigned integers saturated upon
>> overflow instead of wrapping?

>
> It would be "better" (for some suitable definition of better) if
> all types triggered some sort of hardware trap on overflow. (The
> "undefined behavior" of signed integral types and floating point
> types on overflow is designed to allow that.) Practically
> speaking, however, this would result in significantly slower
> programs on most platforms (or would require significantly more
> intelligent compilers to get close to current performance).


So apparently not "better" then, but bad! So, do you think that having a
few saturating integers in the standard in ADDITION to the current ones
would be a good idea?

>
> Why undefined behavior is not the case for unsigned integral
> types is somewhat of a mystery. I'm guessing that it is due to
> some pre-standard code counting on wrapping.



 
Reply With Quote
 
Seungbeom Kim
Guest
Posts: n/a
 
      09-13-2010
On 2010-08-27 15:59, joe wrote:
> Öö Tiib wrote:
>>
>> You are not forced to use arithmetic provided by language. You can
>> easily make your own numeric classes and their operations.

>
> Like MS's SafeInt, I know. But if it was built-in to the low level, there
> wouldn't be a need for those kinds of things. If people are doing it in
> programmer-land, then surely the compiler could do it more efficiently?
> And it doesn't have to be one way or the other, a new set of safe
> primitive integers could be added for those who want to use them.


Maybe in C, where they prefer to add things (such as _Complex) directly
to the core language, provided there is a sufficient demand, of course.
However, C++ prefers to focus on providing facilities upon which people
can build things they want; e.g. classes and templates instead of _Complex.

--
Seungbeom Kim
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Re: Unsigned Integer Overflow on Multiplication and Division Keith Thompson C Programming 2 05-13-2010 08:48 PM
Re: Unsigned Integer Overflow on Multiplication and Division Eric Sosman C Programming 1 05-13-2010 08:18 PM
Unsigned integer overflow detection Raymond C++ 8 08-20-2007 06:44 PM
unsigned integer overflow behaviour bartek C++ 3 02-06-2004 09:47 PM



Advertisments