summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/01_arithmetic/05_overflow
blob: 8c2f214c2ed734ed51876ed15ceb1184259450c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
For integers, the ucode VM tries to perform unsigned 64bit arithmetic internally
if both operands are positive or if the result is guaranteed to be positive.

In all other cases, calculations are performed using signed 64bit arithmetic
with wrap arounds using twos-complement representation.

Due to this, the minimum and maximum representable values depend on the values
of the involved operands.

-- Testcase --
Unsigned additions roll over back to zero:
{{ 18446744073709551615 + 1 }}

Unsigned multiplications roll over back to zero:
{{ 9223372036854775808 * 2 }}

Signed additions roll over at INT64_MIN/INT64_MAX:
{{ -9223372036854775808 + -1 }}

Signed multiplications roll over back to INT64_MIN:
{{ 18446744073709551615 * -1 }}

Multiplicating two negative operands yields an unsigned result.
{{ -9223372036854775807 * -2 }}

Signed calculations yielding positive results are promoted to unsigned.
{{ -9223372036854775808 + 9223372036854775808 + -9223372036854775807 * -2 }}

Substractions roll over to INT64_MAX on underflow:
{{ 0 - 9223372036854775809 }}
-- End --

-- Expect stdout --
Unsigned additions roll over back to zero:
0

Unsigned multiplications roll over back to zero:
0

Signed additions roll over at INT64_MIN/INT64_MAX:
9223372036854775807

Signed multiplications roll over back to INT64_MIN:
-9223372036854775807

Multiplicating two negative operands yields an unsigned result.
18446744073709551614

Signed calculations yielding positive results are promoted to unsigned.
18446744073709551614

Substractions roll over to INT64_MAX on underflow:
9223372036854775807
-- End --