Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created January 29, 2016 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/f789f720ba08383f034e to your computer and use it in GitHub Desktop.
Save zoffixznet/f789f720ba08383f034e to your computer and use it in GitHub Desktop.
There are some inconsistencies with the behaviours among the native types, including buggy behaviour of postdecrement/increment ops when the increment/decrement would make underflow/overflow occur.
Here's the summary table. Detailed code executes are in the attachment.
The meaning of headers:
Assigning Too-Low Value: assigning value below one a type can hold, such as -1 to an unsigned type
Assigning Too-High Value: same as above, except too high a value, such as a 50-bit value into int32
Underflow works: decrementing past lowest value flips over to the other end, such as decrementing a 0 on uint8 gives 256
Overflow works: same as above, except on the upper bounder, such as incrementing 256 on uint8 gives 0
Post-[de/in]cremnent on overflow works: best shown in an example:
WORKS: we flipped over the boundary and increment/decrement operation happened just once, returning the unmodified value in the second say():
<ZoffixWin> m: my int $x = 2**63 - 1; say $x; say $x++; say $x
<camelia> rakudo-moar 780192: OUTPUT«9223372036854775807␤9223372036854775807␤-9223372036854775808␤»
<ZoffixWin> m: my int $x = -2**63; say $x; say $x--; say $x
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤-9223372036854775808␤9223372036854775807␤»
BROKEN: operation happens TWICE and second say() behaves as if it were a prefix increment/decrement:
<ZoffixWin> m: my int32 $x = 2**31 - 1; say $x; say $x++; say $x;
<camelia> rakudo-moar 780192: OUTPUT«2147483647␤-2147483649␤-2147483648␤»
<ZoffixWin> m: my int32 $x = -2**31; say $x; say $x--; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-2147483648␤2147483648␤2147483647␤»
And here's the table showing the results of my evaluation:
[Type] [Assigning Too-Low Value] [Assigning Too-High Value] [Underflow works] [Overflow works] [Post-[de/in]crement on overflow works]
int Runtime error Runtime error Yes Yes Yes
int8 Value=0 Value=0 Yes Yes NO
int16 Value=0 Value=0 Yes Yes NO
int32 Value=0 Value=0 Yes Yes NO
int64 Runtime error Runtime error Yes Yes Yes
uint Behaves as int Behaves as int No (behaves as int) No (behaves as int) Yes (but behaves as int)
uint8 Value rolls over Value rolls over No $x -= 1 works, but No $x +=1 work, but
--$x gives negatives ++$x goes over 255
byte
uint16
uint32
uint64
num
num32
num64
int assigning too high/low value:
<ZoffixWin> m: my int $x = 2**100; say $x
<camelia> rakudo-moar 780192: OUTPUT«Cannot unbox 101 bit wide bigint into native integer␤ in block <unit> at /tmp/pVQy7K7HIo line 1␤␤»
<ZoffixWin> m: my int8 $x = -2**100; say $x
<camelia> rakudo-moar 780192: OUTPUT«Cannot unbox 101 bit wide bigint into native integer␤ in block <unit> at /tmp/D57OVre76N line 1␤␤»
int underflow works:
<ZoffixWin> m: my int $x = -2**63; say $x
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤»
<ZoffixWin> m: my int $x = -2**63; --$x; say $x
<camelia> rakudo-moar 780192: OUTPUT«9223372036854775807␤»
int overflow works:
<ZoffixWin> m: my int $x = 2**63 - 1; say $x
<camelia> rakudo-moar 780192: OUTPUT«9223372036854775807␤»
<ZoffixWin> m: my int $x = 2**63 - 1; ++$x; say $x
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤»
int postop works:
<ZoffixWin> m: my int $x = 2**63 - 1; say $x; say $x++; say $x
<camelia> rakudo-moar 780192: OUTPUT«9223372036854775807␤9223372036854775807␤-9223372036854775808␤»
<ZoffixWin> m: my int $x = -2**63; say $x; say $x--; say $x
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤-9223372036854775808␤9223372036854775807␤»
int8 assigning too high/low value:
<ZoffixWin> m: my int8 $x = -2**60; say $x
<camelia> rakudo-moar 780192: OUTPUT«0␤»
<ZoffixWin> m: my int8 $x = 2**60; say $x
<camelia> rakudo-moar 780192: OUTPUT«0␤»
int8 underflow works:
<ZoffixWin> m: my int8 $x = -2**7; say $x
<camelia> rakudo-moar 780192: OUTPUT«-128␤»
<ZoffixWin> m: my int8 $x = -2**7; --$x; say $x
<camelia> rakudo-moar 780192: OUTPUT«127␤»
int8 overflow works:
<ZoffixWin> m: my int8 $x = 2**7-1; say $x
<camelia> rakudo-moar 780192: OUTPUT«127␤»
<ZoffixWin> m: my int8 $x = 2**7-1; ++$x; say $x
<camelia> rakudo-moar 780192: OUTPUT«-128␤»
int8 postop does NOT work (behaves as if it were ++$x and --$x, AND operation happens twice):
<ZoffixWin> m: my int8 $x = -2**7; say $x; say $x--; say $x
<camelia> rakudo-moar 780192: OUTPUT«-128␤128␤127␤»
<ZoffixWin> m: my int8 $x = 2**7 - 1; say $x; say $x++; say $x
<camelia> rakudo-moar 780192: OUTPUT«127␤-129␤-128␤»
int16 underflow works:
<ZoffixWin> m: my int16 $x = -2**15; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-32768␤»
<ZoffixWin> m: my int16 $x = -2**15; --$x; say $x;
<camelia> rakudo-moar 780192: OUTPUT«32767␤»
int16 assigning too high/low value:
<ZoffixWin> m: my int16 $x = -2**60; say $x
<camelia> rakudo-moar 780192: OUTPUT«0␤»
<ZoffixWin> m: my int16 $x = 2**60; say $x
<camelia> rakudo-moar 780192: OUTPUT«0␤»
int16 overflow works:
<ZoffixWin> m: my int16 $x = 2**15 - 1; say $x;
<camelia> rakudo-moar 780192: OUTPUT«32767␤»
<ZoffixWin> m: my int16 $x = 2**15 - 1; ++$x; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-32768␤»
int16 postop does NOT work (behaves as if it were ++$x and --$x, AND operation happens twice):
<ZoffixWin> m: my int16 $x = 2**15 - 1; say $x; say $x++; say $x;
<camelia> rakudo-moar 780192: OUTPUT«32767␤-32769␤-32768␤»
<ZoffixWin> m: my int16 $x = -2**15; say $x; say $x--; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-32768␤32768␤32767␤»
int32 assigning too high/low value:
<ZoffixWin> m: my int32 $x = -2**60; say $x
<camelia> rakudo-moar 780192: OUTPUT«0␤»
<ZoffixWin> m: my int32 $x = 2**60; say $x
<camelia> rakudo-moar 780192: OUTPUT«0␤»
int32 underflow works:
<ZoffixWin> m: my int32 $x = -2**31; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-2147483648␤»
<ZoffixWin> m: my int32 $x = -2**31; --$x; say $x;
<camelia> rakudo-moar 780192: OUTPUT«2147483647␤»
int32 overflow works:
<ZoffixWin> m: my int32 $x = 2**31 - 1; say $x;
<camelia> rakudo-moar 780192: OUTPUT«2147483647␤»
<ZoffixWin> m: my int32 $x = 2**31 - 1; $x++; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-2147483648␤»
int32 postop does NOT work (behaves as if it were ++$x and --$x, AND operation happens twice):
<ZoffixWin> m: my int32 $x = 2**31 - 1; say $x; say $x++; say $x;
<camelia> rakudo-moar 780192: OUTPUT«2147483647␤-2147483649␤-2147483648␤»
<ZoffixWin> m: my int32 $x = -2**31; say $x; say $x--; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-2147483648␤2147483648␤2147483647␤»
int64 assigning too high/low value:
<ZoffixWin> m: my int64 $x = -2**100; say $x
<camelia> rakudo-moar 780192: OUTPUT«Cannot unbox 101 bit wide bigint into native integer␤ in block <unit> at /tmp/H2Wp75nKB8 line 1␤␤»
<ZoffixWin> m: my int64 $x = 2**100; say $x
<camelia> rakudo-moar 780192: OUTPUT«Cannot unbox 101 bit wide bigint into native integer␤ in block <unit> at /tmp/GO5Ce9tN1m line 1␤␤»
int64 underflow works:
<ZoffixWin> m: my int64 $x = -2**63; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤»
<ZoffixWin> m: my int64 $x = -2**63; --$x; say $x;
<camelia> rakudo-moar 780192: OUTPUT«9223372036854775807␤»
int64 overflow works:
<ZoffixWin> m: my int64 $x = 2**63 - 1; say $x;
<camelia> rakudo-moar 780192: OUTPUT«9223372036854775807␤»
<ZoffixWin> m: my int64 $x = 2**63 - 1; ++$x; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤»
int64 postop works:
<ZoffixWin> m: my int64 $x = 2**63 - 1; say $x; say $x++; say $x;
<camelia> rakudo-moar 780192: OUTPUT«9223372036854775807␤9223372036854775807␤-9223372036854775808␤»
<ZoffixWin> m: my int64 $x = -2**63; say $x; say $x--; say $x;
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤-9223372036854775808␤9223372036854775807␤»
uint assigning too high/low value:
<ZoffixWin> m: my uint $x = -100; say $x
<camelia> rakudo-moar 780192: OUTPUT«-100␤»
<ZoffixWin> m: my uint $x = 2**63; say $x
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤»
<ZoffixWin> m: my uint $x = 2**100; say $x
<camelia> rakudo-moar 780192: OUTPUT«Cannot unbox 101 bit wide bigint into native integer␤ in block <unit> at /tmp/BVOc4q6THL line 1␤␤»
<ZoffixWin> m: my uint $x = -2**100; say $x
<camelia> rakudo-moar 780192: OUTPUT«Cannot unbox 101 bit wide bigint into native integer␤ in block <unit> at /tmp/LKnA6GYHaK line 1␤␤»
uint underflow behaves like int:
<ZoffixWin> m: my uint $x = 0; say $x
<camelia> rakudo-moar 780192: OUTPUT«0␤»
<ZoffixWin> m: my uint $x = 0; --$x; say $x
<camelia> rakudo-moar 780192: OUTPUT«-1␤»
uint overflow behaves like int:
<ZoffixWin> m: my uint $x = 2**63; say $x
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤»
<ZoffixWin> m: my uint $x = 2**63; ++$x; say $x
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775807␤»
uint postop works (but behaves like int):
<ZoffixWin> m: my uint $x = 0; say $x; say $x--; say $x
<camelia> rakudo-moar 780192: OUTPUT«0␤0␤-1␤»
<ZoffixWin> m: my uint $x = 2**63; say $x; say $x++; say $x
<camelia> rakudo-moar 780192: OUTPUT«-9223372036854775808␤-9223372036854775808␤-9223372036854775807␤»
uint8 assigning too high/low value:
<ZoffixWin> m: my uint8 $x = -100; say $x
<camelia> rakudo-moar 780192: OUTPUT«156␤»
<ZoffixWin> m: my uint8 $x = -300; say $x
<camelia> rakudo-moar 780192: OUTPUT«212␤»
<ZoffixWin> m: my uint8 $x = 300; say $x
<camelia> rakudo-moar 780192: OUTPUT«44␤»
uint underflow works:
uint overflow works:
uint postop works:
uint assigning too high/low value:
uint underflow works:
uint overflow works:
uint postop works:
uint assigning too high/low value:
uint underflow works:
uint overflow works:
uint postop works:
uint assigning too high/low value:
uint underflow works:
uint overflow works:
uint postop works:
uint assigning too high/low value:
uint underflow works:
uint overflow works:
uint postop works:
uint assigning too high/low value:
uint underflow works:
uint overflow works:
uint postop works:
uint assigning too high/low value:
uint underflow works:
uint overflow works:
uint postop works:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment