Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


"Wrapping"? Why is it not called "trimming"?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 gasto   Members   -  Reputation: 255

Like
1Likes
Like

Posted 18 January 2014 - 05:23 PM

Why is type conversion in C(akin languages)from a higher to a lower precision type not called "trimming" instead of "wrapping"?

 

It makes more sense, since in little endian systems the least significant byte(s) is(are) trimmed while the rest(most significant)byte(s) is(are) cut off.

 

In fact, the wrapping takes place in bit shifting operations.


Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13929

Like
1Likes
Like

Posted 18 January 2014 - 06:13 PM

Let's say you have 16-bit shorts and 32-bit ints. If you cast an unsigned int to an unsigned short,

for 0 you get 0,
for 1 you get 1,
...
for 65535 you get 65535,
for 65536 you get 0,
for 65537 you get 1,
...

In that sense, the numbers wrap around at 2^16. That's how I understand it.

#3 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 18 January 2014 - 06:34 PM

C is not only for little endian machines. C actually specifies undefined results when trying to store a value larger than a variable can fit. So char c = 1000 leaves c in an undefined state on machines where char is 8 bits.

 

The practical effect of most implementations is that the value wraps in the exact same way that integer overflows occur. This is only a side effect of implementation details though, and is not guaranteed behavior in C. The frequency with which projects rely on this behavior does suggest it is unlikely to change wherever it exists today, but it's still just an implementation detail.

 

And I hope trimming never catches on to explain this process. Trimming implies a far more invasive process in my mind (trimming trailing/leading whitespaces for example).



#4 Bacterius   Crossbones+   -  Reputation: 9289

Like
1Likes
Like

Posted 18 January 2014 - 06:45 PM


The practical effect of most implementations is that the value wraps in the exact same way that integer overflows occur. This is only a side effect of implementation details though, and is not guaranteed behavior in C. The frequency with which projects rely on this behavior does suggest it is unlikely to change wherever it exists today, but it's still just an implementation detail.

 

It is actually guaranteed to behave this way for unsigned types. For signed types, overflow is UB, though on any system you'll encounter today it will probably either wrap around (2's complement) or saturate (most DSPs?).


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 SiCrane   Moderators   -  Reputation: 9673

Like
3Likes
Like

Posted 18 January 2014 - 06:46 PM

Actually, I've rarely heard of an integer conversion to a smaller type as "wrapping". Much more common is referring it to as a "narrowing" conversion.



#6 phantom   Moderators   -  Reputation: 7585

Like
2Likes
Like

Posted 18 January 2014 - 06:56 PM

I don't think I've ever heard it referred to as 'wrapping'; 'cast' or 'convert' sure but never 'wrapping'.

The only time I've heard wrapping used is in cases of, well, wrapping such as an unsigned int at max wrapping around to zero when being added to and the like.

#7 SiCrane   Moderators   -  Reputation: 9673

Like
1Likes
Like

Posted 19 January 2014 - 01:22 AM

It is actually guaranteed to behave this way for unsigned types. For signed types, overflow is UB, though on any system you'll encounter today it will probably either wrap around (2's complement) or saturate (most DSPs?).

That's not quite right. Specific behavior depends on language. In C++ with unsigned integer destination types, the result is always the least integer modulo congruent to the source integer modulo 2^n where n is the number of bits in the destination type. For a signed integer type, if the source integer value can be represented by the destination type, the value is unchanged. However, if not the result is implementation defined, not undefined.

C acts a lot like C++, other than weird wording in the standard; however, if the destination is a signed type and the original value cannot be represented in the destination type, the result can be an implementation defined value or an implementation defined signal can be raised.

C# on the other hand, for narrowing conversions that cannot be represented in the destination type, the result depends on whether or not overflow detection is enabled. If so, a narrowing conversion for an integer value that cannot be represented in the destination type will throw a System.OverflowException. If overflow checking isn't on, the result always consists of discarding the most significant bits of the source.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS