Anonymous user
Integer overflow: Difference between revisions
Replaced the existing program which doesn't actually checked overflow for int32 and uint32 by another one.
(Added description of Nim overflow detection.) |
(Replaced the existing program which doesn't actually checked overflow for int32 and uint32 by another one.) |
||
Line 1,512:
=={{header|Nim}}==
===General behavior regarding overflow===
In Nim, overflow during operations on signed integers is detected and raises an exception. Starting from version 1.4, overflows are defects. For now, by default, defects can be caught but in future versions this may and probably would change. Using compile option <code>--panics:on</code> makes defects impossible to catch.
Line 1,538 ⟶ 1,540:
===Program to check behavior when overflow is not detected===
This program presents the behavior when overflow checks are suppressed. Remember that for signed integers, this is not the normal behavior and that the result is always wrong when an overflow occurs.
<lang Nim>echo "For 32 bits signed integers with overflow check suppressed:"
{.push overflowChecks: off.}
var a: int32
a = -(-2147483647i32 - 1'i32)
echo " -(-2147483647-1) gives ", a # -2147483648.
a = 2000000000i32 + 2000000000i32
echo " 2000000000 + 2000000000 gives ", a # -294967296.
a = -2147483647i32 - 2147483647i32
echo " -2147483647 - 2147483647 gives ", a # 2.
a = 46341i32 * 46341i32
echo " 46341 * 46341 gives ", a # -2147479015.
a = (-2147483647i32 - 1i32) div -1i32
echo " (-2147483647-1) / -1 gives ", a # -2147483648.
{.pop.}
echo ""
echo "For 64 bits signed integers with overflow check suppressed:"
{.push overflowChecks: off.}
var b: int64
b = -(-9223372036854775807i64 - 1i64)
echo " -(-9223372036854775807-1) gives ", b # -9223372036854775808.
b = 5000000000000000000i64 + 5000000000000000000i64
echo " 5000000000000000000 + 5000000000000000000 gives ", b # -8446744073709551616.
b = -9223372036854775807i64 - 9223372036854775807i64
echo " -9223372036854775807 - 9223372036854775807 gives ", b # 2.
b = 3037000500i64 * 3037000500i64
echo " 3037000500 * 3037000500 gives ", b # -9223372036709301616.
b = (-9223372036854775807i64 - 1i64) div -1i64
echo " (-9223372036854775807-1) / -1 gives ", b # -9223372036854775808.
{.pop.}
echo ""
echo "For 32 bits unsigned integers:"
var c: uint32
echo " -4294967295 doesn’t compile."
c = 3000000000u32 + 3000000000u32
echo " 3000000000 + 3000000000 gives ", c # 1705032704.
c = 2147483647u32 - 4294967295u32
echo " 2147483647 - 4294967295 gives ", c # 2147483648.
c = 65537u32 * 65537u32
echo " 65537 * 65537 gives ", c # 131073.
echo ""
echo "For 64 bits unsigned integers:"
var d: uint64
echo " -18446744073709551615 doesn’t compile."
d = 10000000000000000000u64 + 10000000000000000000u64
echo " 10000000000000000000 + 10000000000000000000 gives ", d # 1553255926290448384.
d = 9223372036854775807u64 - 18446744073709551615u64
echo " 9223372036854775807 - 18446744073709551615 gives ", d # 9223372036854775808.
d = 4294967296u64 * 4294967296u64
echo " 4294967296 * 4294967296 gives ", d # 0.</lang>
{{out}}
<pre>For 32 bits signed integers with overflow check suppressed:
-(-2147483647-1) gives -2147483648
2000000000 + 2000000000 gives -294967296
-2147483647 - 2147483647 gives 2
46341 * 46341 gives -2147479015
For 64 bits signed integers with overflow check suppressed:
-(-9223372036854775807-1) gives -9223372036854775808
5000000000000000000 + 5000000000000000000 gives -8446744073709551616
-9223372036854775807 - 9223372036854775807 gives 2
3037000500 * 3037000500 gives -9223372036709301616
(-9223372036854775807-1) / -1 gives -9223372036854775808
For 32 bits unsigned integers:
-4294967295 doesn’t compile.
3000000000 + 3000000000 gives 1705032704
2147483647 - 4294967295 gives 2147483648
65537 * 65537 gives 131073
For 64 bits unsigned integers:
-18446744073709551615 doesn’t compile.
10000000000000000000 + 10000000000000000000 gives 1553255926290448384
9223372036854775807 - 18446744073709551615 gives 9223372036854775808
4294967296 * 4294967296 gives 0</pre>
=={{header|Oforth}}==
|