|View Issue Details|
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0000141||mercury||Bug||public||2010-04-01 15:37||2010-04-01 18:32|
|Target Version||Fixed in Version|
|Summary||0000141: integer has no method for converting to int with wraparound|
|Description||The integer.int function converts an integer to an int but aborts if the integer is out of range.|
It is occasionally useful to convert an integer to an int with out-of-range numbers being wrapped (truncated) just like conversion from int to short in C, for example.
I have attached a patch which adds a function integer.int_with_overflow which does this.
|Additional Information||The patch sort-of relies upon two's complement (it would probably work for one's complement or sign-bit form as well, but does make some bit-level assumptions). I have included an XXX comment to this effect. I assume this is OK since there are other such comments in the same file.|
|Tags||No tags attached.|
Irk... that patch is wrong. The "Wrapped" value is not necessarily in int range (I thought it worked because conversion to int truncated anyway, but you can't assume that since it's specified as undefined.)
Positive integers in range 2^31 to 2^32-1 need to become negative, and vice versa for negative integers in range -2^31-1 to -2^32. This is tricky to do with integer bit operations since they specifically *don't* wrap values. I'm thinking it'll be easier to use mod/rem but I'll have to think hard about it. Does anyone have a better mathematical formula for wraparound conversion?
Also, is there a test suite I need to write test cases for?
A valid formula appears to be (at least for 2's complement):
((X + int_max + 1) `mod` (int_max+1) * 2) - int_max - 1
Is this overly complicated?
Added a new patch (integer-2.patch) which uses the above formula.
I'm aware that there is a formal patch review process, and this isn't it. I'm just seeing if anyone is interested first.
Yes, we're interested - could you please submit your patch to the mercury-reviews list.
(Or ask one of the developers to do so on your behalf)
The review process used in the Mercury project is documented here:
|2010-04-01 15:37||mgiuca||New Issue|
|2010-04-01 15:37||mgiuca||File Added: integer.patch|
|2010-04-01 16:05||mgiuca||Note Added: 0000258|
|2010-04-01 16:14||mgiuca||Note Added: 0000259|
|2010-04-01 18:09||mgiuca||File Added: integer-2.patch|
|2010-04-01 18:10||mgiuca||Note Added: 0000260|
|2010-04-01 18:32||juliensf||Note Added: 0000261|