Integer overflow: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 116: Line 116:
<br>
<br>
If you mask, you can test it in your program:
If you mask, you can test it in your program:
<lang 360asm> L 2,=F'2147483647' 2**31-1
<syntaxhighlight lang="360asm"> L 2,=F'2147483647' 2**31-1
L 3,=F'1' 1
L 3,=F'1' 1
AR 2,3 add register3 to register2
AR 2,3 add register3 to register2
BO OVERFLOW branch on overflow
BO OVERFLOW branch on overflow
....
....
OVERFLOW EQU *</lang>
OVERFLOW EQU *</syntaxhighlight>
On the other hand,
On the other hand,
you will have the S0C8 system abend code : '''fixed point overflow exception'''
you will have the S0C8 system abend code : '''fixed point overflow exception'''
with the same program, if you unmask bit 20 by:
with the same program, if you unmask bit 20 by:
<lang 360asm> IPM 1 Insert Program Mask
<syntaxhighlight lang="360asm"> IPM 1 Insert Program Mask
O 1,BITFPO unmask Fixed Overflow
O 1,BITFPO unmask Fixed Overflow
SPM 1 Set Program Mask
SPM 1 Set Program Mask
...
...
DS 0F alignment
DS 0F alignment
BITFPO DC BL1'00001000' bit20=1 [start at 16]</lang>
BITFPO DC BL1'00001000' bit20=1 [start at 16]</syntaxhighlight>
=={{header|6502 Assembly}}==
=={{header|6502 Assembly}}==
===8-Bit Overflow===
===8-Bit Overflow===
Line 145: Line 145:
These flags will automatically be set or cleared depending on the results of a calculation that can affect them.
These flags will automatically be set or cleared depending on the results of a calculation that can affect them.


<lang 6502asm>LDA #$7F
<syntaxhighlight lang="6502asm">LDA #$7F
CLC
CLC
ADC #$01
ADC #$01
BVS ErrorHandler ;this branch will always be taken.</lang>
BVS ErrorHandler ;this branch will always be taken.</syntaxhighlight>


<lang 6502asm>LDA #$FF
<syntaxhighlight lang="6502asm">LDA #$FF
CLC
CLC
ADC #$01
ADC #$01
BCS ErrorHandler ;this branch will always be taken.</lang>
BCS ErrorHandler ;this branch will always be taken.</syntaxhighlight>


Keep in mind that not all instructions affect the flags in the same way. The only arithmetic instructions that affect the overflow flag are <code>ADC</code> and <code>SBC</code>. Therefore, signed overflow can be "missed" by the CPU very easily if it occurs in other ways:
Keep in mind that not all instructions affect the flags in the same way. The only arithmetic instructions that affect the overflow flag are <code>ADC</code> and <code>SBC</code>. Therefore, signed overflow can be "missed" by the CPU very easily if it occurs in other ways:


<lang 6502asm>LDX #$7F
<syntaxhighlight lang="6502asm">LDX #$7F
INX ;although X went from $7F to $80, INX does not affect the overflow flag!
INX ;although X went from $7F to $80, INX does not affect the overflow flag!
BVS ErrorHandler ;whether this branch is taken has NOTHING to do with the INX instruction.</lang>
BVS ErrorHandler ;whether this branch is taken has NOTHING to do with the INX instruction.</syntaxhighlight>


<lang 6502asm>LDA #%01000000
<syntaxhighlight lang="6502asm">LDA #%01000000
ORA #%10000000 ;accumulator crossed from below $7F to above $80, but ORA doesn't affect the overflow flag.
ORA #%10000000 ;accumulator crossed from below $7F to above $80, but ORA doesn't affect the overflow flag.
BVS ErrorHandler ;whether this branch is taken has NOTHING to do with the ORA instruction.</lang>
BVS ErrorHandler ;whether this branch is taken has NOTHING to do with the ORA instruction.</syntaxhighlight>




The same is true for unsigned overflow, but less so since the zero flag can be used as a substitute in these cases.
The same is true for unsigned overflow, but less so since the zero flag can be used as a substitute in these cases.
<lang 6502asm>LDX #$FF
<syntaxhighlight lang="6502asm">LDX #$FF
INX ;the carry flag is not affected by this unsigned overflow, but the zero flag will be set
INX ;the carry flag is not affected by this unsigned overflow, but the zero flag will be set
; so we can detect overflow that way instead!
; so we can detect overflow that way instead!
BEQ OverflowOccurred ;notice that we used BEQ here and not BCS.</lang>
BEQ OverflowOccurred ;notice that we used BEQ here and not BCS.</syntaxhighlight>


By default, the CPU will continue with the wrong result, unless you specifically program a branch based on overflow after the calculation. This is because on a hardware level the CPU has no knowledge of whether you intend your data to be signed or unsigned (this is still true even on modern computers).
By default, the CPU will continue with the wrong result, unless you specifically program a branch based on overflow after the calculation. This is because on a hardware level the CPU has no knowledge of whether you intend your data to be signed or unsigned (this is still true even on modern computers).
Line 177: Line 177:
Unlike in [[Z80 Assembly]], the 6502 has no 16-bit registers or built-in 16-bit arithmetic instructions. It ''can'' perform 16-bit or higher addition and subtraction, by separating the number into 8-bit pieces and operating on them separately. Unfortunately, this means that the 6502's flags cannot look at the number as a whole; only the individual bytes. As a result, the CPU will detect "overflow" when any of the bytes cross the $7F-$80 boundary, regardless of whether the byte is the most significant byte or not. This is another reason why the ability to selectively ignore overflow is handy, as it only counts as signed overflow when the most significant byte crosses the $7F-$80 boundary.
Unlike in [[Z80 Assembly]], the 6502 has no 16-bit registers or built-in 16-bit arithmetic instructions. It ''can'' perform 16-bit or higher addition and subtraction, by separating the number into 8-bit pieces and operating on them separately. Unfortunately, this means that the 6502's flags cannot look at the number as a whole; only the individual bytes. As a result, the CPU will detect "overflow" when any of the bytes cross the $7F-$80 boundary, regardless of whether the byte is the most significant byte or not. This is another reason why the ability to selectively ignore overflow is handy, as it only counts as signed overflow when the most significant byte crosses the $7F-$80 boundary.


<lang 6502asm>;adding two 16-bit signed numbers, the first is stored at $10 and $11, the second at $12 and $13.
<syntaxhighlight lang="6502asm">;adding two 16-bit signed numbers, the first is stored at $10 and $11, the second at $12 and $13.
;The result will be stored at $14 and $15.
;The result will be stored at $14 and $15.


Line 192: Line 192:
ADC $13 ;high byte of second operand
ADC $13 ;high byte of second operand
STA $15 ;high byte of result
STA $15 ;high byte of result
BVS HandleOverflow ;only check for overflow when adding the most significant bytes.</lang>
BVS HandleOverflow ;only check for overflow when adding the most significant bytes.</syntaxhighlight>


=={{header|68000 Assembly}}==
=={{header|68000 Assembly}}==
Overflow happens when certain arithmetic operations result in the most significant byte of the register crossing over from 0x7F to 0x80. (Which byte of the 32-bit register is treated as "most significant" depends on the data size of the last instruction. See the example below)
Overflow happens when certain arithmetic operations result in the most significant byte of the register crossing over from 0x7F to 0x80. (Which byte of the 32-bit register is treated as "most significant" depends on the data size of the last instruction. See the example below)


<lang 68000devpac>MOVE.W D0,#0000117F
<syntaxhighlight lang="68000devpac">MOVE.W D0,#0000117F
ADD.W #1,D0 ;DOESN'T SET THE OVERFLOW FLAG, SINCE AT WORD LENGTH WE DIDN'T CROSS FROM 7FFF TO 8000
ADD.W #1,D0 ;DOESN'T SET THE OVERFLOW FLAG, SINCE AT WORD LENGTH WE DIDN'T CROSS FROM 7FFF TO 8000


SUB.B #1,D0 ;WILL SET THE OVERFLOW FLAG SINCE AT BYTE LENGTH WE CROSSED FROM 80 TO 7F</lang>
SUB.B #1,D0 ;WILL SET THE OVERFLOW FLAG SINCE AT BYTE LENGTH WE CROSSED FROM 80 TO 7F</syntaxhighlight>


Like the 6502, the 68000 will continue with the wrong result unless you tell it to stop. As with the majority of computer architectures, whether a value is "signed" or "unsigned" is not actually a property of the value itself, but of the comparators used to evaluate it. Otherwise even unsigned arithmetic would produce overflow errors! There are a few options for handling overflow errors:
Like the 6502, the 68000 will continue with the wrong result unless you tell it to stop. As with the majority of computer architectures, whether a value is "signed" or "unsigned" is not actually a property of the value itself, but of the comparators used to evaluate it. Otherwise even unsigned arithmetic would produce overflow errors! There are a few options for handling overflow errors:
Line 210: Line 210:
In Ada, both predefined and user-defined integer types are in a given range, between Type'First and Type'Last, inclusive. The range of predefined types is implementation specific. When the result of a computation is out of the type's range, the program <b>does not continue with a wrong result, but</b> instead <b>raises an exception</b>.
In Ada, both predefined and user-defined integer types are in a given range, between Type'First and Type'Last, inclusive. The range of predefined types is implementation specific. When the result of a computation is out of the type's range, the program <b>does not continue with a wrong result, but</b> instead <b>raises an exception</b>.


<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;


procedure Overflow is
procedure Overflow is
Line 260: Line 260:
A := A + 1; -- line 49 -- this will later raise a CONSTRAINT_ERROR
A := A + 1; -- line 49 -- this will later raise a CONSTRAINT_ERROR
end loop;
end loop;
end Overflow;</lang>
end Overflow;</syntaxhighlight>


{{out}}
{{out}}
Line 291: Line 291:


Other implementations are at liberty to take any action they wish, including to continue silently with a "wrong" result or to throw a catchable exception (though the latter would require at least one addition to the standard prelude so as to provide the handler routine(s).
Other implementations are at liberty to take any action they wish, including to continue silently with a "wrong" result or to throw a catchable exception (though the latter would require at least one addition to the standard prelude so as to provide the handler routine(s).
<lang algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
print (max int);
print (max int);
print (1+max int)
print (1+max int)
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>+2147483647
<pre>+2147483647
Line 303: Line 303:
Note that, unlike many other languages, there is no presupposition that Algol 68 is running on a binary computer. The second example code below shows that for variables of mode '''long int''' arithmetic is fundamentally decimal in Algol 68 Genie.
Note that, unlike many other languages, there is no presupposition that Algol 68 is running on a binary computer. The second example code below shows that for variables of mode '''long int''' arithmetic is fundamentally decimal in Algol 68 Genie.


<lang algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
print (long max int);
print (long max int);
print (1+ long max int)
print (1+ long max int)
END
END
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 318: Line 318:
=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
The integer variable type is a signed 16-bit integer with a range from -32767 to 32767. When an integer variable is assigned a value less than -32767 or greater than 32767, an "?ILLEGAL QUANTITY ERROR" message is displayed and no change is made to the current value of the variable. All of the expressions for assigning the values use floating point.
The integer variable type is a signed 16-bit integer with a range from -32767 to 32767. When an integer variable is assigned a value less than -32767 or greater than 32767, an "?ILLEGAL QUANTITY ERROR" message is displayed and no change is made to the current value of the variable. All of the expressions for assigning the values use floating point.
<lang>A% = -(-32767-1)</lang>
<syntaxhighlight lang="text">A% = -(-32767-1)</syntaxhighlight>
{{out}}<pre>?ILLEGAL QUANTITY ERROR</pre>
{{out}}<pre>?ILLEGAL QUANTITY ERROR</pre>
<lang>A% = 20000 + 20000</lang>
<syntaxhighlight lang="text">A% = 20000 + 20000</syntaxhighlight>
{{out}}<pre>?ILLEGAL QUANTITY ERROR</pre>
{{out}}<pre>?ILLEGAL QUANTITY ERROR</pre>
<lang>A% = -32767 -32767</lang>
<syntaxhighlight lang="text">A% = -32767 -32767</syntaxhighlight>
{{out}}<pre>?ILLEGAL QUANTITY ERROR</pre>
{{out}}<pre>?ILLEGAL QUANTITY ERROR</pre>
<lang>A% = 182 * 182</lang>
<syntaxhighlight lang="text">A% = 182 * 182</syntaxhighlight>
{{out}}<pre>?ILLEGAL QUANTITY ERROR</pre>
{{out}}<pre>?ILLEGAL QUANTITY ERROR</pre>
It is possible using a POKE statement to assign the value -32768 which would normally be out of range.
It is possible using a POKE statement to assign the value -32768 which would normally be out of range.
<lang>A% = -32767 : POKE PEEK(131) + PEEK(132) * 256, 0 : ? A%</lang>
<syntaxhighlight lang="text">A% = -32767 : POKE PEEK(131) + PEEK(132) * 256, 0 : ? A%</syntaxhighlight>
{{out}}<pre>-32768</pre>
{{out}}<pre>-32768</pre>


Line 334: Line 334:
Arturo has unlimited-precision integers, without the possibility of an overflow, all with the same <code>:integer</code> type.
Arturo has unlimited-precision integers, without the possibility of an overflow, all with the same <code>:integer</code> type.


<lang rebol>big32bit: 2147483646
<syntaxhighlight lang="rebol">big32bit: 2147483646
big64bit: 9223372036854775808
big64bit: 9223372036854775808


Line 344: Line 344:


print big32bit * 2
print big32bit * 2
print big64bit * 2</lang>
print big64bit * 2</syntaxhighlight>


{{out}}
{{out}}
Line 358: Line 358:
Since AutoHotkey treats all integers as signed 64-bit, there is no point in demonstrating overflow with other integer types.
Since AutoHotkey treats all integers as signed 64-bit, there is no point in demonstrating overflow with other integer types.
A AutoHotkey program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
A AutoHotkey program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
<lang AutoHotkey>Msgbox, % "Testing signed 64-bit integer overflow with AutoHotkey:`n" -(-9223372036854775807-1) "`n" 5000000000000000000+5000000000000000000 "`n" -9223372036854775807-9223372036854775807 "`n" 3037000500*3037000500 "`n" (-9223372036854775807-1)//-1</lang>
<syntaxhighlight lang="autohotkey">Msgbox, % "Testing signed 64-bit integer overflow with AutoHotkey:`n" -(-9223372036854775807-1) "`n" 5000000000000000000+5000000000000000000 "`n" -9223372036854775807-9223372036854775807 "`n" 3037000500*3037000500 "`n" (-9223372036854775807-1)//-1</syntaxhighlight>
{{out}}
{{out}}
<pre>Testing signed 64-bit integer overflow with AutoHotkey:
<pre>Testing signed 64-bit integer overflow with AutoHotkey:
Line 373: Line 373:
Overflow does <b>not</b> trigger an exception (because Axe does not support exceptions). After an overflow the program <b>continues with wrong results</b> (specifically, the value modulo 65536).
Overflow does <b>not</b> trigger an exception (because Axe does not support exceptions). After an overflow the program <b>continues with wrong results</b> (specifically, the value modulo 65536).


<lang axe>Disp -65535▶Dec,i
<syntaxhighlight lang="axe">Disp -65535▶Dec,i
Disp 40000+40000▶Dec,i
Disp 40000+40000▶Dec,i
Disp 32767-65535▶Dec,i
Disp 32767-65535▶Dec,i
Disp 257*257▶Dec,i</lang>
Disp 257*257▶Dec,i</syntaxhighlight>


{{out}}
{{out}}
Line 389: Line 389:
For those with a finite integer range, though, the most common stack cell size is a 32 bit signed integer, which will usually just wrap when overflowing (as shown in the sample output below). That said, it's not uncommon for the last expression to produce some kind of runtime error or OS exception, frequently even crashing the interpreter itself.
For those with a finite integer range, though, the most common stack cell size is a 32 bit signed integer, which will usually just wrap when overflowing (as shown in the sample output below). That said, it's not uncommon for the last expression to produce some kind of runtime error or OS exception, frequently even crashing the interpreter itself.


<lang befunge>"a9jc>"*:*+*+:0\- "(-",,:.048*"="99")1 -" >:#,_$v
<syntaxhighlight lang="befunge">"a9jc>"*:*+*+:0\- "(-",,:.048*"="99")1 -" >:#,_$v
v,,,9"="*84 .: ,,"+"*84 .: **:*" }}" ,+55 .-\0-1<
v,,,9"="*84 .: ,,"+"*84 .: **:*" }}" ,+55 .-\0-1<
>:+. 55+, ::0\- :. 48*"-",, \:. 48*"="9,,, -. 55v
>:+. 55+, ::0\- :. 48*"-",, \:. 48*"="9,,, -. 55v
v.*: ,,,,,999"="*84 .: ,,"*"*84 .: *+8*7"s9" ,+<
v.*: ,,,,,999"="*84 .: ,,"*"*84 .: *+8*7"s9" ,+<
>55+, 0\- "(",:.048*"="99"1-/)1 -">:#,_$ 1-01-/.@</lang>
>55+, 0\- "(",:.048*"="99"1-/)1 -">:#,_$ 1-01-/.@</syntaxhighlight>


{{out}}
{{out}}
Line 410: Line 410:
An overflow for signed integer arithmetic is undefined behavior.
An overflow for signed integer arithmetic is undefined behavior.
A C program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
A C program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int main (int argc, char *argv[])
int main (int argc, char *argv[])
Line 437: Line 437:
printf("%lu\n", 4294967296LU * 4294967296LU);
printf("%lu\n", 4294967296LU * 4294967296LU);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 474: Line 474:
The default behavior can be changed with a compiler flag.
The default behavior can be changed with a compiler flag.


<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
public class IntegerOverflow
public class IntegerOverflow
Line 522: Line 522:
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 558: Line 558:


{{works with | g++ | 4.7}}
{{works with | g++ | 4.7}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
#include <cstdint>
#include <limits>
#include <limits>
Line 592: Line 592:
<< 4294967296LU * 4294967296LU << '\n';
<< 4294967296LU * 4294967296LU << '\n';
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 628: Line 628:


By default, Clojure throws Exceptions on overflow conditions:
By default, Clojure throws Exceptions on overflow conditions:
<lang clojure>(* -1 (dec -9223372036854775807))
<syntaxhighlight lang="clojure">(* -1 (dec -9223372036854775807))
(+ 5000000000000000000 5000000000000000000)
(+ 5000000000000000000 5000000000000000000)
(- -9223372036854775807 9223372036854775807)
(- -9223372036854775807 9223372036854775807)
(* 3037000500 3037000500)</lang>
(* 3037000500 3037000500)</syntaxhighlight>
{{out}} for all of the above statements:
{{out}} for all of the above statements:
<pre>ArithmeticException integer overflow clojure.lang.Numbers.throwIntOverflow</pre>
<pre>ArithmeticException integer overflow clojure.lang.Numbers.throwIntOverflow</pre>


If you want to silently overflow, you can set the special *unchecked-math* variable to true or use the special operations, unchecked-add, unchecked-multiply, etc..
If you want to silently overflow, you can set the special *unchecked-math* variable to true or use the special operations, unchecked-add, unchecked-multiply, etc..
<lang clojure>(set! *unchecked-math* true)
<syntaxhighlight lang="clojure">(set! *unchecked-math* true)
(* -1 (dec -9223372036854775807)) ;=> -9223372036854775808
(* -1 (dec -9223372036854775807)) ;=> -9223372036854775808
(+ 5000000000000000000 5000000000000000000) ;=> -8446744073709551616
(+ 5000000000000000000 5000000000000000000) ;=> -8446744073709551616
Line 643: Line 643:
; Note: The following division will currently silently overflow regardless of *unchecked-math*
; Note: The following division will currently silently overflow regardless of *unchecked-math*
; See: http://dev.clojure.org/jira/browse/CLJ-1253
; See: http://dev.clojure.org/jira/browse/CLJ-1253
(/ (dec -9223372036854775807) -1) ;=> -9223372036854775808</lang>
(/ (dec -9223372036854775807) -1) ;=> -9223372036854775808</syntaxhighlight>


Clojure supports an arbitrary precision integer, BigInt and alternative math operators suffixed with an apostrophe: +', -', *', inc', and dec'. These operators auto-promote to BigInt upon overflow.
Clojure supports an arbitrary precision integer, BigInt and alternative math operators suffixed with an apostrophe: +', -', *', inc', and dec'. These operators auto-promote to BigInt upon overflow.
Line 649: Line 649:
=={{header|COBOL}}==
=={{header|COBOL}}==
COBOL uses decimal arithmetic, so the examples given in the specification are not directly relevant. This program declares a variable that can store three decimal digits, and attempts to assign a four-digit number to it. The result is that the number is truncated to fit, with only the three least significant digits actually being stored; and the program then proceeds. This behaviour may sometimes be what we want.
COBOL uses decimal arithmetic, so the examples given in the specification are not directly relevant. This program declares a variable that can store three decimal digits, and attempts to assign a four-digit number to it. The result is that the number is truncated to fit, with only the three least significant digits actually being stored; and the program then proceeds. This behaviour may sometimes be what we want.
<lang cobol>IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
PROGRAM-ID. PROCRUSTES-PROGRAM.
PROGRAM-ID. PROCRUSTES-PROGRAM.
DATA DIVISION.
DATA DIVISION.
Line 658: Line 658:
MOVE 1002 TO X.
MOVE 1002 TO X.
DISPLAY X UPON CONSOLE.
DISPLAY X UPON CONSOLE.
STOP RUN.</lang>
STOP RUN.</syntaxhighlight>
{{out}}
{{out}}
<pre>002</pre>
<pre>002</pre>
Line 677: Line 677:
A small example:
A small example:


<lang cobol> identification division.
<syntaxhighlight lang="cobol"> identification division.
program-id. overflowing.
program-id. overflowing.


Line 754: Line 754:


goback.
goback.
end program overflowing.</lang>
end program overflowing.</syntaxhighlight>


{{out}}
{{out}}
Line 779: Line 779:
=={{header|Computer/zero Assembly}}==
=={{header|Computer/zero Assembly}}==
Arithmetic is performed modulo 256; overflow is not detected. This fragment:
Arithmetic is performed modulo 256; overflow is not detected. This fragment:
<lang czasm> LDA ff
<syntaxhighlight lang="czasm"> LDA ff
ADD one
ADD one


Line 785: Line 785:


ff: 255
ff: 255
one: 1</lang>
one: 1</syntaxhighlight>
causes the accumulator to adopt the value 0. With a little care, the programmer can exploit this behaviour by treating each eight-bit word as either an unsigned byte or a signed byte using two's complement (although the instruction set does not provide explicit support for negative numbers). On the two's complement interpretation, the code given above would express the computation "–1 + 1 = 0".
causes the accumulator to adopt the value 0. With a little care, the programmer can exploit this behaviour by treating each eight-bit word as either an unsigned byte or a signed byte using two's complement (although the instruction set does not provide explicit support for negative numbers). On the two's complement interpretation, the code given above would express the computation "–1 + 1 = 0".


Line 793: Line 793:
Additionally, standard functions are available to perform arithmetic on int, long, uint, ulong values that modify a boolean value to signal when an overflow has occurred.
Additionally, standard functions are available to perform arithmetic on int, long, uint, ulong values that modify a boolean value to signal when an overflow has occurred.


<lang d>void main() @safe {
<syntaxhighlight lang="d">void main() @safe {
import std.stdio;
import std.stdio;


Line 829: Line 829:
immutable r = muls(46_341, 46_341, overflow);
immutable r = muls(46_341, 46_341, overflow);
writeln("\n", r, " ", overflow);
writeln("\n", r, " ", overflow);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Signed 32-bit:
<pre>Signed 32-bit:
Line 867: Line 867:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
For the 64-bit integer type a FreeBASIC program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
For the 64-bit integer type a FreeBASIC program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
<lang c>#include <stdio.h></lang>
<syntaxhighlight lang="c">#include <stdio.h></syntaxhighlight>


<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


' The suffixes L, LL, UL and ULL are added to the numbers to make it
' The suffixes L, LL, UL and ULL are added to the numbers to make it
Line 916: Line 916:
Print
Print
Print "Press any key to quit"
Print "Press any key to quit"
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 955: Line 955:
[http://play.golang.org/p/jsPWC8KGzD Run this in the Go playground].
[http://play.golang.org/p/jsPWC8KGzD Run this in the Go playground].
A Go program does <b>not</b> recognize an integer overflow and the program <b>continues with wrong results</b>.
A Go program does <b>not</b> recognize an integer overflow and the program <b>continues with wrong results</b>.
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 1,062: Line 1,062:
u64 = 4294967296
u64 = 4294967296
fmt.Printf(" %d * %d: %d\n", u64, u64, u64*u64)
fmt.Printf(" %d * %d: %d\n", u64, u64, u64*u64)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>32 bit signed integers
<pre>32 bit signed integers
Line 1,100: Line 1,100:
Groovy does not recognize integer overflow in any bounded integral type and the program '''continues with wrong results'''. All bounded integral types use ''2's-complement'' arithmetic.
Groovy does not recognize integer overflow in any bounded integral type and the program '''continues with wrong results'''. All bounded integral types use ''2's-complement'' arithmetic.


<lang groovy>println "\nSigned 32-bit (failed):"
<syntaxhighlight lang="groovy">println "\nSigned 32-bit (failed):"
assert -(-2147483647-1) != 2147483648g
assert -(-2147483647-1) != 2147483648g
println(-(-2147483647-1))
println(-(-2147483647-1))
Line 1,160: Line 1,160:
println(3037000500g * 3037000500g)
println(3037000500g * 3037000500g)
assert (-9223372036854775807g-1g).intdiv(-1) == 9223372036854775808g
assert (-9223372036854775807g-1g).intdiv(-1) == 9223372036854775808g
println((-9223372036854775807g-1g).intdiv(-1))</lang>
println((-9223372036854775807g-1g).intdiv(-1))</syntaxhighlight>


Output:
Output:
Line 1,199: Line 1,199:
=={{header|Haskell}}==
=={{header|Haskell}}==
Haskell supports both fixed sized signed integers (Int) and unbounded integers (Integer). Various sizes of signed and unsigned integers are available in Data.Int and Data.Word, respectively. The Haskell 2010 Language Report explains the following: "The results of exceptional conditions (such as overflow or underflow) on the fixed-precision numeric types are undefined; an implementation may choose error (⊥, semantically), a truncated value, or a special value such as infinity, indefinite, etc" (http://www.haskell.org/definition/haskell2010.pdf Section 6.4 Paragraph 4).
Haskell supports both fixed sized signed integers (Int) and unbounded integers (Integer). Various sizes of signed and unsigned integers are available in Data.Int and Data.Word, respectively. The Haskell 2010 Language Report explains the following: "The results of exceptional conditions (such as overflow or underflow) on the fixed-precision numeric types are undefined; an implementation may choose error (⊥, semantically), a truncated value, or a special value such as infinity, indefinite, etc" (http://www.haskell.org/definition/haskell2010.pdf Section 6.4 Paragraph 4).
<lang Haskell>import Data.Int
<syntaxhighlight lang="haskell">import Data.Int
import Data.Word
import Data.Word
import Control.Exception
import Control.Exception
Line 1,224: Line 1,224:
f ((10000000000000000000 + 10000000000000000000) :: Word64)
f ((10000000000000000000 + 10000000000000000000) :: Word64)
f ((9223372036854775807 - 18446744073709551615) :: Word64)
f ((9223372036854775807 - 18446744073709551615) :: Word64)
f ((4294967296 * 4294967296) :: Word64)</lang>
f ((4294967296 * 4294967296) :: Word64)</syntaxhighlight>
{{out}}
{{out}}
<pre>-2147483648
<pre>-2147483648
Line 1,251: Line 1,251:
Also, negative numbers do not use - for the negative sign in J (a preceding - means to negate the argument on the right - in some cases this is the same kind of result, but in other cases it's different). Instead, use _ to denote negative numbers. Also, J does not use / for division, instead J uses % for division. With those changes, here's what the results look like in a 32 bit version of J:
Also, negative numbers do not use - for the negative sign in J (a preceding - means to negate the argument on the right - in some cases this is the same kind of result, but in other cases it's different). Instead, use _ to denote negative numbers. Also, J does not use / for division, instead J uses % for division. With those changes, here's what the results look like in a 32 bit version of J:


<lang J> -(_2147483647-1)
<syntaxhighlight lang="j"> -(_2147483647-1)
2.14748e9
2.14748e9
2000000000 + 2000000000
2000000000 + 2000000000
Line 1,289: Line 1,289:
_9.22337e18
_9.22337e18
4294967296 * 4294967296
4294967296 * 4294967296
1.84467e19</lang>
1.84467e19</syntaxhighlight>


And, here's what it looks like in a 64 bit version of J:
And, here's what it looks like in a 64 bit version of J:


<lang J> -(_2147483647-1)
<syntaxhighlight lang="j"> -(_2147483647-1)
2147483648
2147483648
2000000000 + 2000000000
2000000000 + 2000000000
Line 1,331: Line 1,331:
_9.22337e18
_9.22337e18
4294967296 * 4294967296
4294967296 * 4294967296
1.84467e19</lang>
1.84467e19</syntaxhighlight>


That said, note that the above was with default 6 digits of "printing precision". Here's how things look with that limit relaxed:
That said, note that the above was with default 6 digits of "printing precision". Here's how things look with that limit relaxed:
Line 1,337: Line 1,337:
32 bit J:
32 bit J:


<lang J> -(_2147483647-1)
<syntaxhighlight lang="j"> -(_2147483647-1)
2147483648
2147483648
2000000000 + 2000000000
2000000000 + 2000000000
Line 1,375: Line 1,375:
_9223372036854775800
_9223372036854775800
4294967296 * 4294967296
4294967296 * 4294967296
18446744073709552000</lang>
18446744073709552000</syntaxhighlight>


64 bit J:
64 bit J:


<lang J> -(_2147483647-1)
<syntaxhighlight lang="j"> -(_2147483647-1)
2147483648
2147483648
2000000000 + 2000000000
2000000000 + 2000000000
Line 1,417: Line 1,417:
_9223372036854775800
_9223372036854775800
4294967296 * 4294967296
4294967296 * 4294967296
18446744073709552000</lang>
18446744073709552000</syntaxhighlight>


Finally, note that both versions of J support arbitrary precision integers. These are not the default, for performance reasons, but are available for cases where their performance penalty is acceptable.
Finally, note that both versions of J support arbitrary precision integers. These are not the default, for performance reasons, but are available for cases where their performance penalty is acceptable.
Line 1,424: Line 1,424:
The type int is a signed 32-bit integer and the type long is a 64-bit integer.
The type int is a signed 32-bit integer and the type long is a 64-bit integer.
A Java program does <b>not</b> recognize an integer overflow and the program <b>continues with wrong results</b>.
A Java program does <b>not</b> recognize an integer overflow and the program <b>continues with wrong results</b>.
<lang java>public class integerOverflow {
<syntaxhighlight lang="java">public class integerOverflow {


public static void main(String[] args) {
public static void main(String[] args) {
Line 1,441: Line 1,441:
}
}


}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,475: Line 1,475:
''The task''
''The task''


<syntaxhighlight lang="jq">
<lang jq>
def compare:
def compare:
if type == "string" then "\n\(.)\n"
if type == "string" then "\n\(.)\n"
Line 1,514: Line 1,514:
[4294967296 * 4294967296, "18446744073709551616"]
[4294967296 * 4294967296, "18446744073709551616"]


| compare</lang>
| compare</syntaxhighlight>


===jq 1.6===
===jq 1.6===
Line 1,582: Line 1,582:
=={{header|Julia}}==
=={{header|Julia}}==
'''Plain Integer Types and Their Limits'''
'''Plain Integer Types and Their Limits'''
<lang julia>using Printf
<syntaxhighlight lang="julia">using Printf
S = subtypes(Signed)
S = subtypes(Signed)
U = subtypes(Unsigned)
U = subtypes(Unsigned)
Line 1,590: Line 1,590:
@printf("%8s: [%s, %s]\n", s, typemin(s), typemax(s))
@printf("%8s: [%s, %s]\n", s, typemin(s), typemax(s))
@printf("%8s: [%s, %s]\n", u, typemin(u), typemax(u))
@printf("%8s: [%s, %s]\n", u, typemin(u), typemax(u))
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Integer limits:
<pre>Integer limits:
Line 1,608: Line 1,608:
Julia does not throw an explicit error on integer overflow.
Julia does not throw an explicit error on integer overflow.


<lang julia>println("Add one to typemax:")
<syntaxhighlight lang="julia">println("Add one to typemax:")
for t in S
for t in S
over = typemax(t) + one(t)
over = typemax(t) + one(t)
@printf("%8s → %-25s (%s)\n", t, over, typeof(over))
@printf("%8s → %-25s (%s)\n", t, over, typeof(over))
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,625: Line 1,625:
A Kotlin program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
A Kotlin program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.


<lang scala>// version 1.0.5-2
<syntaxhighlight lang="scala">// version 1.0.5-2


/* Kotlin (like Java) does not have unsigned integer types but we can simulate
/* Kotlin (like Java) does not have unsigned integer types but we can simulate
Line 1,653: Line 1,653:
println((2147483647L - 4294967295L.toUInt()).toUInt())
println((2147483647L - 4294967295L.toUInt()).toUInt())
println((65537L * 65537L).toUInt())
println((65537L * 65537L).toUInt())
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,682: Line 1,682:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>
<syntaxhighlight lang="ksh">
#!/bin/ksh
#!/bin/ksh


Line 1,705: Line 1,705:
(( LONG_INT = 2**63 -1 )) ; print " LONG_INT (2^63 -1) = $LONG_INT"
(( LONG_INT = 2**63 -1 )) ; print " LONG_INT (2^63 -1) = $LONG_INT"
(( LONG_INT = 2**63 )) ; print " LONG_INT (2^63) : $LONG_INT"
(( LONG_INT = 2**63 )) ; print " LONG_INT (2^63) : $LONG_INT"
</syntaxhighlight>
</lang>
{{out}}<pre>
{{out}}<pre>
SHORT_INT (2^15 -1) = 32767
SHORT_INT (2^15 -1) = 32767
Line 1,718: Line 1,718:
Lingo uses 32-bit signed integers.
Lingo uses 32-bit signed integers.
A Lingo program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
A Lingo program does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
<lang c>#include <stdio.h></lang>
<syntaxhighlight lang="c">#include <stdio.h></syntaxhighlight>


<lang lingo>put -(-2147483647-1)
<syntaxhighlight lang="lingo">put -(-2147483647-1)
-- -2147483648
-- -2147483648


Line 1,733: Line 1,733:


put (-2147483647-1) / -1
put (-2147483647-1) / -1
--> crashes Director (jeez!)</lang>
--> crashes Director (jeez!)</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
Lua 5.3+ supports integer and floating sub-types of its generic number type. The ''standard'' implementation is 64-bit signed, under/overflow is not recognized.
Lua 5.3+ supports integer and floating sub-types of its generic number type. The ''standard'' implementation is 64-bit signed, under/overflow is not recognized.
<lang Lua>assert(math.type~=nil, "Lua 5.3+ required for this test.")
<syntaxhighlight lang="lua">assert(math.type~=nil, "Lua 5.3+ required for this test.")
minint, maxint = math.mininteger, math.maxinteger
minint, maxint = math.mininteger, math.maxinteger
print("min, max int64 = " .. minint .. ", " .. maxint)
print("min, max int64 = " .. minint .. ", " .. maxint)
print("min-1 underflow = " .. (minint-1) .. " equals max? " .. tostring(minint-1==maxint))
print("min-1 underflow = " .. (minint-1) .. " equals max? " .. tostring(minint-1==maxint))
print("max+1 overflow = " .. (maxint+1) .. " equals min? " .. tostring(maxint+1==minint))</lang>
print("max+1 overflow = " .. (maxint+1) .. " equals min? " .. tostring(maxint+1==minint))</syntaxhighlight>
{{out}}
{{out}}
<pre>min, max int64 = -9223372036854775808, 9223372036854775807
<pre>min, max int64 = -9223372036854775808, 9223372036854775807
Line 1,748: Line 1,748:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Long A
Long A
Try ok {
Try ok {
Line 1,786: Line 1,786:
Print Hex$(Eval(DataMem, 0!b))="BBBBAAAA"
Print Hex$(Eval(DataMem, 0!b))="BBBBAAAA"
Print Eval(DataMem, 0!b)=Eval(DataMem, 0!a2)*0x10000+Eval(DataMem, 0!a1)
Print Eval(DataMem, 0!b)=Eval(DataMem, 0!a2)*0x10000+Eval(DataMem, 0!a1)
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica and Wolfram Language uses arbitrary number types. There is a $MaxNumber which is approximately 1.60521676193366172702774105306375828321e1355718576299609, but extensive research has shown it to allow numbers up to <lang Mathematica>$MaxNumber +
Mathematica and Wolfram Language uses arbitrary number types. There is a $MaxNumber which is approximately 1.60521676193366172702774105306375828321e1355718576299609, but extensive research has shown it to allow numbers up to <syntaxhighlight lang="mathematica">$MaxNumber +
10^-15.954589770191003298111788092733772206160314 $MaxNumber</lang>I haven't bothered testing it to any more precision. If you try to use any number above that, it returns an Overflow[].
10^-15.954589770191003298111788092733772206160314 $MaxNumber</syntaxhighlight>I haven't bothered testing it to any more precision. If you try to use any number above that, it returns an Overflow[].


=={{header|Nim}}==
=={{header|Nim}}==
Line 1,799: Line 1,799:


Catching an overflow (when --panics is off) is done this way:
Catching an overflow (when --panics is off) is done this way:
<lang Nim>try:
<syntaxhighlight lang="nim">try:
var x: int32 = -2147483647
var x: int32 = -2147483647
x = -(x - 1) # Raise overflow.
x = -(x - 1) # Raise overflow.
echo x
echo x
except OverflowDefect:
except OverflowDefect:
echo "Overflow detected"</lang>
echo "Overflow detected"</syntaxhighlight>


It is possible to tell the compiler to not generate code to detect overflows by using pragmas “push” and “pop”:
It is possible to tell the compiler to not generate code to detect overflows by using pragmas “push” and “pop”:
<lang Nim>{.push overflowChecks: off.}
<syntaxhighlight lang="nim">{.push overflowChecks: off.}
try:
try:
var x: int32 = -2147483647
var x: int32 = -2147483647
Line 1,814: Line 1,814:
except OverflowDefect:
except OverflowDefect:
echo "Overflow detected" # Not executed.
echo "Overflow detected" # Not executed.
{.pop.}</lang>
{.pop.}</syntaxhighlight>


It is also possible to suppress all overflow checks by using compile option <code>--overflowChecks:off</code>. Also, compiling with option <code>-d:danger</code> suppress these checks and several others.
It is also possible to suppress all overflow checks by using compile option <code>--overflowChecks:off</code>. Also, compiling with option <code>-d:danger</code> suppress these checks and several others.
Line 1,823: Line 1,823:
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.
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:"
<syntaxhighlight lang="nim">echo "For 32 bits signed integers with overflow check suppressed:"
{.push overflowChecks: off.}
{.push overflowChecks: off.}
var a: int32
var a: int32
Line 1,874: Line 1,874:
echo " 9223372036854775807 - 18446744073709551615 gives ", d # 9223372036854775808.
echo " 9223372036854775807 - 18446744073709551615 gives ", d # 9223372036854775808.
d = 4294967296u64 * 4294967296u64
d = 4294967296u64 * 4294967296u64
echo " 4294967296 * 4294967296 gives ", d # 0.</lang>
echo " 4294967296 * 4294967296 gives ", d # 0.</syntaxhighlight>


{{out}}
{{out}}
Line 1,916: Line 1,916:
=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Machine-sized integers can be used inside a <code>Vecsmall</code>:
Machine-sized integers can be used inside a <code>Vecsmall</code>:
<lang parigp>Vecsmall([1])
<syntaxhighlight lang="parigp">Vecsmall([1])
Vecsmall([2^64])</lang>
Vecsmall([2^64])</syntaxhighlight>
{{out}}
{{out}}
<pre>%1 = Vecsmall([1])
<pre>%1 = Vecsmall([1])
Line 1,938: Line 1,938:
Using Perl 5.18 on 64-bit Linux with use integer:
Using Perl 5.18 on 64-bit Linux with use integer:
The Perl 5 program below does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
The Perl 5 program below does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
<lang c>#include <stdio.h></lang>
<syntaxhighlight lang="c">#include <stdio.h></syntaxhighlight>


<lang perl>
<syntaxhighlight lang="perl">
use strict;
use strict;
use warnings;
use warnings;
Line 1,952: Line 1,952:
say(3037000500 * 3037000500);
say(3037000500 * 3037000500);
say((-9223372036854775807-1) / -1);
say((-9223372036854775807-1) / -1);
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,970: Line 1,970:
Integer overflow is handled by automatic promotion to atom (an IEEE float, 64/80 bit for the 32/64 bit implementations respectively),
Integer overflow is handled by automatic promotion to atom (an IEEE float, 64/80 bit for the 32/64 bit implementations respectively),
which triggers a run-time type check if stored in a variable declared as integer, eg:
which triggers a run-time type check if stored in a variable declared as integer, eg:
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000000000</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1000000000</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000000000</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1000000000</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,992: Line 1,992:
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)<br>
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)<br>
8080 PL/M does not check for overflow, incrementing the largest integer values wraps around to 0 (numbers are insigned in 8080 PL/M) and the program <b>continues with wrong results</b>.
8080 PL/M does not check for overflow, incrementing the largest integer values wraps around to 0 (numbers are insigned in 8080 PL/M) and the program <b>continues with wrong results</b>.
<lang pli>100H: /* SHOW INTEGER OVERFLOW */
<syntaxhighlight lang="pli">100H: /* SHOW INTEGER OVERFLOW */


/* CP/M SYSTEM CALL */
/* CP/M SYSTEM CALL */
Line 2,034: Line 2,034:
CALL PRNL;
CALL PRNL;


EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,046: Line 2,046:
https://docs.microsoft.com/en-us/dotnet/api/system.decimal?view=netframework-4.8#remarks
https://docs.microsoft.com/en-us/dotnet/api/system.decimal?view=netframework-4.8#remarks


<lang powershell>
<syntaxhighlight lang="powershell">
try {
try {
# All of these raise an exception, which is caught below.
# All of these raise an exception, which is caught below.
Line 2,077: Line 2,077:
$Error.Exception
$Error.Exception
}
}
</syntaxhighlight>
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
CPU=x64, OS=Windows7
CPU=x64, OS=Windows7
<lang purebasic>#MAX_BYTE =127
<syntaxhighlight lang="purebasic">#MAX_BYTE =127


#MAX_ASCII=255 ;=MAX_CHAR Ascii-Mode
#MAX_ASCII=255 ;=MAX_CHAR Ascii-Mode
Line 2,136: Line 2,136:
say("Quad",q1,q2,SizeOf(q1))
say("Quad",q1,q2,SizeOf(q1))


Input()</lang>
Input()</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,154: Line 2,154:
Python 2.X has a 32 bit signed integer type called 'int' that automatically converts to type 'long' on overflow. Type long is of arbitrary precision adjusting its precision up to computer limits, as needed.
Python 2.X has a 32 bit signed integer type called 'int' that automatically converts to type 'long' on overflow. Type long is of arbitrary precision adjusting its precision up to computer limits, as needed.


<lang python>Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
<syntaxhighlight lang="python">Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
Type "copyright", "credits" or "license()" for more information.
>>> for calc in ''' -(-2147483647-1)
>>> for calc in ''' -(-2147483647-1)
Line 2,171: Line 2,171:
Expression: '46341 * 46341' evaluates to 2147488281 of type <type 'long'>
Expression: '46341 * 46341' evaluates to 2147488281 of type <type 'long'>
Expression: '(-2147483647-1) / -1' evaluates to 2147483648 of type <type 'long'>
Expression: '(-2147483647-1) / -1' evaluates to 2147483648 of type <type 'long'>
>>> </lang>
>>> </syntaxhighlight>


===Python 3.x===
===Python 3.x===
Python 3.X has the one 'int' type that is of arbitrary precision. Implementations ''may'' use 32 bit integers for speed and silently shift to arbitrary precision to avoid overflow.
Python 3.X has the one 'int' type that is of arbitrary precision. Implementations ''may'' use 32 bit integers for speed and silently shift to arbitrary precision to avoid overflow.
<lang python>Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
<syntaxhighlight lang="python">Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
Type "copyright", "credits" or "license()" for more information.
>>> for calc in ''' -(-2147483647-1)
>>> for calc in ''' -(-2147483647-1)
Line 2,192: Line 2,192:
Expression: '46341 * 46341' evaluates to 2147488281 of type <class 'int'>
Expression: '46341 * 46341' evaluates to 2147488281 of type <class 'int'>
Expression: '(-2147483647-1) / -1' evaluates to 2147483648.0 of type <class 'float'>
Expression: '(-2147483647-1) / -1' evaluates to 2147483648.0 of type <class 'float'>
>>> </lang>
>>> </syntaxhighlight>


Note: In Python 3.X the division operator used between two ints returns a floating point result, (as this was seen as most often required and expected in the Python community). Use <code>//</code> to get integer division.
Note: In Python 3.X the division operator used between two ints returns a floating point result, (as this was seen as most often required and expected in the Python community). Use <code>//</code> to get integer division.
Line 2,207: Line 2,207:
The unsafe operations expects fixnums in the arguments, and that the result is also a fixnum. They don't autopromote the result. They are faster but they should be used only in special cases, where the values known to be bounded. We can use them to see the behavior after an overflow. In case of a overflow they have undefined behaviour, so they may give different results or change without warning in future versions. (I don't expect that they will change soon, but there is no official guaranty.)
The unsafe operations expects fixnums in the arguments, and that the result is also a fixnum. They don't autopromote the result. They are faster but they should be used only in special cases, where the values known to be bounded. We can use them to see the behavior after an overflow. In case of a overflow they have undefined behaviour, so they may give different results or change without warning in future versions. (I don't expect that they will change soon, but there is no official guaranty.)


<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require racket/unsafe/ops)
(require racket/unsafe/ops)


Line 2,226: Line 2,226:


(/ -1073741824 -1) ;==> 1073741824
(/ -1073741824 -1) ;==> 1073741824
(unsafe-fxquotient -1073741824 -1) ;==> -1073741824</lang>
(unsafe-fxquotient -1073741824 -1) ;==> -1073741824</syntaxhighlight>


The 64-bit version is similar. The fixnum are effectively 63-bits signed integers.
The 64-bit version is similar. The fixnum are effectively 63-bits signed integers.
Line 2,235: Line 2,235:
The Raku program below does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.
The Raku program below does <b>not</b> recognize a signed integer overflow and the program <b>continues with wrong results</b>.


<lang perl6>my int64 ($a, $b, $c) = 9223372036854775807, 5000000000000000000, 3037000500;
<syntaxhighlight lang="raku" line>my int64 ($a, $b, $c) = 9223372036854775807, 5000000000000000000, 3037000500;
.say for -(-$a - 1), $b + $b, -$a - $a, $c * $c, (-$a - 1)/-1;</lang>
.say for -(-$a - 1), $b + $b, -$a - $a, $c * $c, (-$a - 1)/-1;</syntaxhighlight>
{{out}}
{{out}}
<pre>-9223372036854775808
<pre>-9223372036854775808
Line 2,252: Line 2,252:
For newer versions of REXX, the &nbsp; '''signal on lostDigits''' &nbsp; statement can be used to accomplish the same results &nbsp;
For newer versions of REXX, the &nbsp; '''signal on lostDigits''' &nbsp; statement can be used to accomplish the same results &nbsp;
<br>(for detecting a loss of significance [digits]).
<br>(for detecting a loss of significance [digits]).
<lang rexx>/*REXX program displays values when integers have an overflow or underflow. */
<syntaxhighlight lang="rexx">/*REXX program displays values when integers have an overflow or underflow. */
numeric digits 9 /*the REXX default is 9 decimal digits.*/
numeric digits 9 /*the REXX default is 9 decimal digits.*/
call showResult( 999999997 + 1 )
call showResult( 999999997 + 1 )
Line 2,267: Line 2,267:
else _=' [underflow]' /*did it underflow? */
else _=' [underflow]' /*did it underflow? */
say right(x, 20) _ /*show the result. */
say right(x, 20) _ /*show the result. */
return x /*return the value. */</lang>
return x /*return the value. */</syntaxhighlight>
'''output''' &nbsp; using the default input(s): <br><br>
'''output''' &nbsp; using the default input(s): <br><br>
Output note: &nbsp; (as it happens, all of the results below are numerically correct)
Output note: &nbsp; (as it happens, all of the results below are numerically correct)
Line 2,286: Line 2,286:
Bignum objects are created automatically when integer calculations would otherwise overflow a Fixnum.
Bignum objects are created automatically when integer calculations would otherwise overflow a Fixnum.
When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.
When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.
<lang ruby>2.1.1 :001 > a = 2**62 -1
<syntaxhighlight lang="ruby">2.1.1 :001 > a = 2**62 -1
=> 4611686018427387903
=> 4611686018427387903
2.1.1 :002 > a.class
2.1.1 :002 > a.class
Line 2,294: Line 2,294:
2.1.1 :004 > (b-1).class
2.1.1 :004 > (b-1).class
=> Fixnum
=> Fixnum
</syntaxhighlight>
</lang>
Since Ruby 2.4 these different classes have disappeared: all numbers in above code are of class Integer.
Since Ruby 2.4 these different classes have disappeared: all numbers in above code are of class Integer.


Line 2,320: Line 2,320:
The following code will always panic when run in any mode
The following code will always panic when run in any mode


<syntaxhighlight lang="rust">
<lang Rust>
// The following will panic!
// The following will panic!
let i32_1 : i32 = -(-2_147_483_647 - 1);
let i32_1 : i32 = -(-2_147_483_647 - 1);
Line 2,334: Line 2,334:
let i64_4 : i64 = 3_037_000_500 * 3_037_000_500;
let i64_4 : i64 = 3_037_000_500 * 3_037_000_500;
let i64_5 : i64 = (-9_223_372_036_854_775_807 - 1) / -1;
let i64_5 : i64 = (-9_223_372_036_854_775_807 - 1) / -1;
</syntaxhighlight>
</lang>


In order to declare overflow/underflow behaviour as intended (and, thus, valid in both debug and release modes), Rust provides two mechanisms:
In order to declare overflow/underflow behaviour as intended (and, thus, valid in both debug and release modes), Rust provides two mechanisms:
Line 2,348: Line 2,348:
<br>
<br>


<syntaxhighlight lang="rust">
<lang Rust>
// The following will never panic!
// The following will never panic!
println!("{:?}", 65_537u32.checked_mul(65_537)); // None
println!("{:?}", 65_537u32.checked_mul(65_537)); // None
Line 2,358: Line 2,358:
println!("{:?}", 65_537i32.saturating_mul(65_537)); // 2147483647
println!("{:?}", 65_537i32.saturating_mul(65_537)); // 2147483647
println!("{:?}", 65_537i32.wrapping_mul(-65_537)); // -131073
println!("{:?}", 65_537i32.wrapping_mul(-65_537)); // -131073
</syntaxhighlight>
</lang>


Second, a generic <code>Wrapping<T></code> one-element tuple type is provided which implements the same basic operations as the <code>wrapping_...</code> methods, but allows you to use normal operators and then use the <code>.0</code> field accessor to retrieve the value once you are finished.<ref>{{Cite web |url=https://doc.rust-lang.org/std/num/struct.Wrapping.html |title=Struct std::num::Wrapping |website=The Rust Standard Library |access-date=2019-11-18}}</ref>
Second, a generic <code>Wrapping<T></code> one-element tuple type is provided which implements the same basic operations as the <code>wrapping_...</code> methods, but allows you to use normal operators and then use the <code>.0</code> field accessor to retrieve the value once you are finished.<ref>{{Cite web |url=https://doc.rust-lang.org/std/num/struct.Wrapping.html |title=Struct std::num::Wrapping |website=The Rust Standard Library |access-date=2019-11-18}}</ref>
Line 2,365: Line 2,365:
{{works with|Java|8}}
{{works with|Java|8}}
Math.addExact works for both 32-bit unsigned and 64-bit unsigned integers, but Java does not support signed integers.
Math.addExact works for both 32-bit unsigned and 64-bit unsigned integers, but Java does not support signed integers.
<lang Scala>import Math.{addExact => ++, multiplyExact => **, negateExact => ~~, subtractExact => --}
<syntaxhighlight lang="scala">import Math.{addExact => ++, multiplyExact => **, negateExact => ~~, subtractExact => --}


def requireOverflow(f: => Unit) =
def requireOverflow(f: => Unit) =
Line 2,379: Line 2,379:
println("Test - Expect Undetected overflow:")
println("Test - Expect Undetected overflow:")
requireOverflow(++(1,1)) // Undetected overflow
requireOverflow(++(1,1)) // Undetected overflow
</syntaxhighlight>
</lang>


=={{header|Seed7}}==
=={{header|Seed7}}==
Line 2,385: Line 2,385:
The type [http://seed7.sourceforge.net/manual/types.htm#integer integer] is a 64-bit signed integer type.
The type [http://seed7.sourceforge.net/manual/types.htm#integer integer] is a 64-bit signed integer type.
All computations with the type integer are checked for overflow.
All computations with the type integer are checked for overflow.
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const proc: writeResult (ref func integer: expression) is func
const proc: writeResult (ref func integer: expression) is func
Line 2,403: Line 2,403:
writeResult(3037000500 * 3037000500);
writeResult(3037000500 * 3037000500);
writeResult((-9223372036854775807-1) div -1);
writeResult((-9223372036854775807-1) div -1);
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 2,417: Line 2,417:
{{trans|Raku}}
{{trans|Raku}}
Sidef has unlimited precision integers.
Sidef has unlimited precision integers.
<lang ruby>var (a, b, c) = (9223372036854775807, 5000000000000000000, 3037000500);
<syntaxhighlight lang="ruby">var (a, b, c) = (9223372036854775807, 5000000000000000000, 3037000500);
[-(-a - 1), b + b, -a - a, c * c, (-a - 1)/-1].each { say _ };</lang>
[-(-a - 1), b + b, -a - a, c * c, (-a - 1)/-1].each { say _ };</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,432: Line 2,432:
However, to emulate wrong behavior (eg. when interfacing to external programs or document formats), it can be emulated.
However, to emulate wrong behavior (eg. when interfacing to external programs or document formats), it can be emulated.
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}
<lang smalltalk>2147483647 + 1. -> 2147483648
<syntaxhighlight lang="smalltalk">2147483647 + 1. -> 2147483648
2147483647 add_32: 1 -> -2147483648
2147483647 add_32: 1 -> -2147483648
4294967295 + 1. -> 4294967296
4294967295 + 1. -> 4294967296
16rFFFFFFFF add_32u: 1. -> 0
16rFFFFFFFF add_32u: 1. -> 0
... simular stuff for sub32/mul32 ...</lang>
... simular stuff for sub32/mul32 ...</syntaxhighlight>


=={{header|Swift}}==
=={{header|Swift}}==
<lang Swift>// By default, all overflows in Swift result in a runtime exception, which is always fatal
<syntaxhighlight lang="swift">// By default, all overflows in Swift result in a runtime exception, which is always fatal
// However, you can opt-in to overflow behavior with the overflow operators and continue with wrong results
// However, you can opt-in to overflow behavior with the overflow operators and continue with wrong results


Line 2,490: Line 2,490:
println(uInt64)
println(uInt64)
uInt64 = 4294967296 &* 4294967296
uInt64 = 4294967296 &* 4294967296
println(uInt64)</lang>
println(uInt64)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,522: Line 2,522:
=={{header|Standard ML}}==
=={{header|Standard ML}}==
PolyML
PolyML
<lang Standard ML>~(~9223372036854775807-1) ;
<syntaxhighlight lang="standard ml">~(~9223372036854775807-1) ;
poly: : error: Overflow exception raised while converting ~9223372036854775807 to int
poly: : error: Overflow exception raised while converting ~9223372036854775807 to int
Int.maxInt ;
Int.maxInt ;
Line 2,532: Line 2,532:
2147483648 * 2147483648 ;
2147483648 * 2147483648 ;
Exception- Overflow raised
Exception- Overflow raised
</syntaxhighlight>
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
Tcl (since 8.5) uses logical signed integers throughout that are “large enough to hold the number you are using” (being internally anything from a single machine word up to a bignum). The only way to get 32-bit and 64-bit values in arithmetic is to apply a clamping function at appropriate points:
Tcl (since 8.5) uses logical signed integers throughout that are “large enough to hold the number you are using” (being internally anything from a single machine word up to a bignum). The only way to get 32-bit and 64-bit values in arithmetic is to apply a clamping function at appropriate points:
<lang tcl>proc tcl::mathfunc::clamp32 {x} {
<syntaxhighlight lang="tcl">proc tcl::mathfunc::clamp32 {x} {
expr {$x<0 ? -((-$x) & 0x7fffffff) : $x & 0x7fffffff}
expr {$x<0 ? -((-$x) & 0x7fffffff) : $x & 0x7fffffff}
}
}
puts [expr { clamp32(2000000000 + 2000000000) }]; # ==> 1852516352</lang>
puts [expr { clamp32(2000000000 + 2000000000) }]; # ==> 1852516352</syntaxhighlight>
Tcl 8.4 used a mix of 32-bit and 64-bit numbers on 32-bit platforms and 64-bit numbers only on 64-bit platforms. Users are recommended to upgrade to avoid this complexity.
Tcl 8.4 used a mix of 32-bit and 64-bit numbers on 32-bit platforms and 64-bit numbers only on 64-bit platforms. Users are recommended to upgrade to avoid this complexity.


=={{header|True BASIC}}==
=={{header|True BASIC}}==
<lang qbasic>PRINT "Signed 32-bit:"
<syntaxhighlight lang="qbasic">PRINT "Signed 32-bit:"
PRINT -(-2147483647-1) !-2147483648
PRINT -(-2147483647-1) !-2147483648
PRINT 2000000000 + 2000000000 !4000000000
PRINT 2000000000 + 2000000000 !4000000000
Line 2,555: Line 2,555:
!returns the largest number that can be represented in your computer
!returns the largest number that can be represented in your computer
END WHEN
END WHEN
END</lang>
END</syntaxhighlight>


=={{header|VBScript}}==
=={{header|VBScript}}==
Line 2,563: Line 2,563:
<br>- Yes, because typename(2147483647)="Long" and typename(2147483648)="Double", so we have switched from fixed binary integer to double floating point. But thanks to mantissa precision there is no harm. The integer overflow is when you reach 10^15, because you are now out of the integer set : (1E+15)+1=1E+15 !?.
<br>- Yes, because typename(2147483647)="Long" and typename(2147483648)="Double", so we have switched from fixed binary integer to double floating point. But thanks to mantissa precision there is no harm. The integer overflow is when you reach 10^15, because you are now out of the integer set : (1E+15)+1=1E+15 !?.
<br>A good way to test integer overflow is to use the vartype() or typename() builtin functions.
<br>A good way to test integer overflow is to use the vartype() or typename() builtin functions.
<lang vb>'Binary Integer overflow - vbs
<syntaxhighlight lang="vb">'Binary Integer overflow - vbs
i=(-2147483647-1)/-1
i=(-2147483647-1)/-1
wscript.echo i
wscript.echo i
Line 2,576: Line 2,576:
i1=1000000000000000-1 '1E+15-1
i1=1000000000000000-1 '1E+15-1
i2=i1+1 '1E+15
i2=i1+1 '1E+15
wscript.echo Cstr(i1) & " , " & Cstr(i2)</lang>
wscript.echo Cstr(i1) & " , " & Cstr(i2)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,590: Line 2,590:
{{works with|Visual Basic|VB6 Standard}}
{{works with|Visual Basic|VB6 Standard}}
Overflow is well handled, except for a strange bug in the computation of f the constant -(-2147483648).
Overflow is well handled, except for a strange bug in the computation of f the constant -(-2147483648).
<lang vb> 'Binary Integer overflow - vb6 - 28/02/2017
<syntaxhighlight lang="vb"> 'Binary Integer overflow - vb6 - 28/02/2017
Dim i As Long '32-bit signed integer
Dim i As Long '32-bit signed integer
i = -(-2147483647 - 1) '=-2147483648 ?! bug
i = -(-2147483647 - 1) '=-2147483648 ?! bug
Line 2,602: Line 2,602:
i = 46341 * 46341 'Run-time error '6' : Overflow
i = 46341 * 46341 'Run-time error '6' : Overflow
i = (-2147483647 - 1) / -1 'Run-time error '6' : Overflow
i = (-2147483647 - 1) / -1 'Run-time error '6' : Overflow
</lang>
</syntaxhighlight>
'''Error handling - method 1'''
'''Error handling - method 1'''
<lang vb> i=0
<syntaxhighlight lang="vb"> i=0
On Error Resume Next
On Error Resume Next
i = 2147483647 + 1
i = 2147483647 + 1
Debug.Print i 'i=0
Debug.Print i 'i=0
</syntaxhighlight>
</lang>
'''Error handling - method 2'''
'''Error handling - method 2'''
<lang vb> i=0
<syntaxhighlight lang="vb"> i=0
On Error GoTo overflow
On Error GoTo overflow
i = 2147483647 + 1
i = 2147483647 + 1
Line 2,616: Line 2,616:
overflow:
overflow:
Debug.Print "Error: " & Err.Description '-> Error: Overflow
Debug.Print "Error: " & Err.Description '-> Error: Overflow
</syntaxhighlight>
</lang>
'''Error handling - method 3'''
'''Error handling - method 3'''
<lang vb> On Error GoTo 0
<syntaxhighlight lang="vb"> On Error GoTo 0
i = 2147483647 + 1 'Run-time error '6' : Overflow
i = 2147483647 + 1 'Run-time error '6' : Overflow
Debug.Print i
Debug.Print i
</syntaxhighlight>
</lang>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
Line 2,632: Line 2,632:


'''32-bit signed integer'''
'''32-bit signed integer'''
<lang vbnet> Dim i As Integer '32-bit signed integer</lang>
<syntaxhighlight lang="vbnet"> Dim i As Integer '32-bit signed integer</syntaxhighlight>
Pre-compilation error:
Pre-compilation error:
'Error: Constant expression not representable in type 'Integer'
'Error: Constant expression not representable in type 'Integer'
for:
for:
<lang vbnet> i = -(-2147483647 - 1)
<syntaxhighlight lang="vbnet"> i = -(-2147483647 - 1)
i = 0 - (-2147483647 - 1)
i = 0 - (-2147483647 - 1)
i = -(-2147483647L - 1)
i = -(-2147483647L - 1)
Line 2,644: Line 2,644:
i = -2147483647 - 2147483647
i = -2147483647 - 2147483647
i = 46341 * 46341
i = 46341 * 46341
i = (-2147483647 - 1) / -1 </lang>
i = (-2147483647 - 1) / -1 </syntaxhighlight>
Execution error:
Execution error:
'An unhandled exception of type 'System.OverflowException' occurred
'An unhandled exception of type 'System.OverflowException' occurred
'Additional information: Arithmetic operation resulted in an overflow.
'Additional information: Arithmetic operation resulted in an overflow.
for:
for:
<lang vbnet> i = -Int(-2147483647 - 1)
<syntaxhighlight lang="vbnet"> i = -Int(-2147483647 - 1)
i = -2147483647: i = -(i - 1) </lang>
i = -2147483647: i = -(i - 1) </syntaxhighlight>
'''32-bit unsigned integer'''<br>
'''32-bit unsigned integer'''<br>
In Visual Basic .Net there is no specific UInteger constants as in C.
In Visual Basic .Net there is no specific UInteger constants as in C.
<lang vbnet> Dim i As UInteger '32-bit unsigned integer</lang>
<syntaxhighlight lang="vbnet"> Dim i As UInteger '32-bit unsigned integer</syntaxhighlight>
Pre-compilation error:
Pre-compilation error:
'Error: Constant expression not representable in type 'UInteger'
'Error: Constant expression not representable in type 'UInteger'
for:
for:
<lang vbnet> i = -4294967295
<syntaxhighlight lang="vbnet"> i = -4294967295
i = 3000000000 + 3000000000
i = 3000000000 + 3000000000
i = 2147483647 - 4294967295
i = 2147483647 - 4294967295
i = 65537 * 65537 </lang>
i = 65537 * 65537 </syntaxhighlight>
Execution error:
Execution error:
'An unhandled exception of type 'System.OverflowException' occurred
'An unhandled exception of type 'System.OverflowException' occurred
'Additional information: Arithmetic operation resulted in an overflow.
'Additional information: Arithmetic operation resulted in an overflow.
for:
for:
<lang vbnet> i = 3000000000 : i = i + i </lang>
<syntaxhighlight lang="vbnet"> i = 3000000000 : i = i + i </syntaxhighlight>
'''64-bit signed integer'''
'''64-bit signed integer'''
<lang vbnet> Dim i As Long '64-bit signed integer</lang>
<syntaxhighlight lang="vbnet"> Dim i As Long '64-bit signed integer</syntaxhighlight>
Pre-compilation error:
Pre-compilation error:
'Error: Constant expression not representable in type 'Long'
'Error: Constant expression not representable in type 'Long'
for:
for:
<lang vbnet> i = -(-9223372036854775807 - 1)
<syntaxhighlight lang="vbnet"> i = -(-9223372036854775807 - 1)
i = 5000000000000000000 + 5000000000000000000
i = 5000000000000000000 + 5000000000000000000
i = -9223372036854775807 - 9223372036854775807
i = -9223372036854775807 - 9223372036854775807
i = 3037000500 * 3037000500
i = 3037000500 * 3037000500
i = (-9223372036854775807 - 1) / -1</lang>
i = (-9223372036854775807 - 1) / -1</syntaxhighlight>
Execution error:
Execution error:
'An unhandled exception of type 'System.OverflowException' occurred
'An unhandled exception of type 'System.OverflowException' occurred
'Additional information: Arithmetic operation resulted in an overflow.
'Additional information: Arithmetic operation resulted in an overflow.
for:
for:
<lang vbnet> i = -9223372036854775807 : i = -(i - 1)</lang>
<syntaxhighlight lang="vbnet"> i = -9223372036854775807 : i = -(i - 1)</syntaxhighlight>


'''64-bit unsigned integer'''<br>
'''64-bit unsigned integer'''<br>
In Visual Basic .Net there is no specific ULong constants as in C.
In Visual Basic .Net there is no specific ULong constants as in C.
And 'Long' constants are not good enough.
And 'Long' constants are not good enough.
<lang vbnet> Dim i As ULong '64-bit unsigned integer</lang>
<syntaxhighlight lang="vbnet"> Dim i As ULong '64-bit unsigned integer</syntaxhighlight>
Pre-compilation error:
Pre-compilation error:
'Error: Overflow
'Error: Overflow
for:
for:
<lang vbnet> i = -18446744073709551615
<syntaxhighlight lang="vbnet"> i = -18446744073709551615
i = 10000000000000000000 + 10000000000000000000
i = 10000000000000000000 + 10000000000000000000
i = 9223372036854775807 - 18446744073709551615</lang>
i = 9223372036854775807 - 18446744073709551615</syntaxhighlight>
Pre-compilation error:
Pre-compilation error:
'Error: Constant expression not representable in type 'Long'
'Error: Constant expression not representable in type 'Long'
for:
for:
<lang vbnet> i = 4294967296 * 4294967296</lang>
<syntaxhighlight lang="vbnet"> i = 4294967296 * 4294967296</syntaxhighlight>
Execution error:
Execution error:
'An unhandled exception of type 'System.OverflowException' occurred
'An unhandled exception of type 'System.OverflowException' occurred
'Additional information: Arithmetic operation resulted in an overflow.
'Additional information: Arithmetic operation resulted in an overflow.
for:
for:
<lang vbnet> i = 4294967296 : i = i * i</lang>
<syntaxhighlight lang="vbnet"> i = 4294967296 : i = i * i</syntaxhighlight>


'''how the exception is catched'''
'''how the exception is catched'''
<lang vbnet> Dim i As Integer '32-bit signed integer
<syntaxhighlight lang="vbnet"> Dim i As Integer '32-bit signed integer
Try
Try
i = -2147483647 : i = -(i - 1)
i = -2147483647 : i = -(i - 1)
Line 2,709: Line 2,709:
Catch ex As Exception
Catch ex As Exception
Debug.Print("Exception raised : " & ex.Message)
Debug.Print("Exception raised : " & ex.Message)
End Try</lang>
End Try</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,722: Line 2,722:


However, within this overall framework, Wren also has an unsigned 32-bit integer sub-system when dealing with bitwise operations. All values are converted internally to such integers before the corresponding C bitwise operation is performed (Wren's VM is written in C) and can therefore overflow without warning. Fortunately, we can easily observe these effects by performing the operations required by the task and then (for example) right shifting them by 0 places.
However, within this overall framework, Wren also has an unsigned 32-bit integer sub-system when dealing with bitwise operations. All values are converted internally to such integers before the corresponding C bitwise operation is performed (Wren's VM is written in C) and can therefore overflow without warning. Fortunately, we can easily observe these effects by performing the operations required by the task and then (for example) right shifting them by 0 places.
<lang ecmascript>var exprs = [-4294967295, 3000000000 + 3000000000, 2147483647 - 4294967295, 65537 * 65537]
<syntaxhighlight lang="ecmascript">var exprs = [-4294967295, 3000000000 + 3000000000, 2147483647 - 4294967295, 65537 * 65537]


for (expr in exprs) System.print(expr >> 0)</lang>
for (expr in exprs) System.print(expr >> 0)</syntaxhighlight>
System.print("Unsigned 32-bit:")
System.print("Unsigned 32-bit:")
{{out}}
{{out}}
Line 2,742: Line 2,742:
continues with wrong results.
continues with wrong results.


<lang XPL0>int N;
<syntaxhighlight lang="xpl0">int N;
[N:= -(-2147483647-1);
[N:= -(-2147483647-1);
IntOut(0, N); CrLf(0);
IntOut(0, N); CrLf(0);
Line 2,753: Line 2,753:
N:= (-2147483647-1)/-1;
N:= (-2147483647-1)/-1;
IntOut(0, N); CrLf(0);
IntOut(0, N); CrLf(0);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 2,773: Line 2,773:
* <code>PO</code> parity odd, no overflow
* <code>PO</code> parity odd, no overflow


<lang z80>ld a,&7F
<syntaxhighlight lang="z80">ld a,&7F
add 1
add 1
jp pe,ErrorHandler ;pe = parity even, but in this case it represents overflow set</lang>
jp pe,ErrorHandler ;pe = parity even, but in this case it represents overflow set</syntaxhighlight>


Like other CPUs, the Z80 has no way of knowing whether a value is intended to be signed or unsigned, and unless you explicitly have a jump, call, or return based on overflow after a calculation, the CPU '''will continue with the wrong result.'''
Like other CPUs, the Z80 has no way of knowing whether a value is intended to be signed or unsigned, and unless you explicitly have a jump, call, or return based on overflow after a calculation, the CPU '''will continue with the wrong result.'''
Line 2,785: Line 2,785:
zkl uses C's 64 bit integer math and the results are OS dependent. Integers are signed. GMP can be used for big ints.
zkl uses C's 64 bit integer math and the results are OS dependent. Integers are signed. GMP can be used for big ints.
A zkl program does <b>not</b> recognize an integer overflow and the program <b>continues with wrong results</b>.
A zkl program does <b>not</b> recognize an integer overflow and the program <b>continues with wrong results</b>.
<lang zkl>print("Signed 64-bit:\n");
<syntaxhighlight lang="zkl">print("Signed 64-bit:\n");
println(-(-9223372036854775807-1));
println(-(-9223372036854775807-1));
println(5000000000000000000+5000000000000000000);
println(5000000000000000000+5000000000000000000);
println(-9223372036854775807 - 9223372036854775807);
println(-9223372036854775807 - 9223372036854775807);
println(3037000500 * 3037000500);
println(3037000500 * 3037000500);
println((-9223372036854775807-1) / -1);</lang>
println((-9223372036854775807-1) / -1);</syntaxhighlight>
{{out}}
{{out}}
Linux/BSD/clang
Linux/BSD/clang