Mercury Bugs - mercury |
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 |
|
Reporter | mgiuca | |
---|
Assigned To | | |
---|
Priority | normal | Severity | feature | Reproducibility | N/A |
---|
Status | new | Resolution | open | |
---|
Platform | | OS | | OS Version | |
---|
Product Version | | |
---|
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. |
---|
Relationships | |
Attached Files | integer.patch (1,243) 2010-04-01 15:37 https://bugs.mercurylang.org/file_download.php?file_id=97&type=bug integer-2.patch (1,249) 2010-04-01 18:09 https://bugs.mercurylang.org/file_download.php?file_id=98&type=bug |
---|
Notes |
|
(0000258)
|
mgiuca
|
2010-04-01 16:05
|
|
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? |
|
|
(0000259)
|
mgiuca
|
2010-04-01 16:14
|
|
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? |
|
|
(0000260)
|
mgiuca
|
2010-04-01 18:10
|
|
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. |
|
|
|
|