Integer overflow: Difference between revisions

Content deleted Content added
WikiCleanerBot (talk | contribs)
m v2.05b - Bot T20 CW#61 - Fix errors for CW project (Reference before punctuation - Link equal to linktext)
Bender the Bot (talk | contribs)
 
(2 intermediate revisions by 2 users not shown)
Line 2:
{{Use American English|date=January 2019}}
 
[[File:Odometer rollover.jpg|thumb|250pxupright=1.3|Integer overflow can be demonstrated through an [[odometer]] overflowing, a mechanical version of the phenomenon. All digits are set to the maximum 9 and the next increment of the white digit causes a cascade of carry-over additions setting all digits to 0, but there is no higher digit (1,000,000s digit) to change to a 1, so the counter resets to zero. This is ''wrapping'' in contrast to ''saturating''.]]
 
In [[computer programming]], an '''integer overflow''' occurs when an [[arithmetic]] operation on integers attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either higher than the maximum or lower than the minimum representable value.
Line 19:
All integers in computer programming have constraints of a max value and min value. The primary factors for determining the range is the allocation of bits and if it is [[Signedness|signed or unsigned]]. The [[Integer (computer science)#Standard_integer|standard integer]] depends on the [[Computing platform|platform]] and [[programming language]]. Additional integer representation can be less than or greater than standard. Examples are the [[Integer (computer science)#Short_integer|short integer]] and [[Integer (computer science)#Long_integer|long integer]] respectively. Even [[Arbitrary-precision arithmetic|arbitrary-precision]] exists, but would be limited by [[Arbitrary-precision arithmetic#Pre-set_precision|pre-set precision]] or available system memory.
 
Most [[Arithmetic logic unit|ALUs]] perform operations on [[Signedness|unsigned]] (positive) [[Binary number|binary numbers]]. These ALUs do not have any capability of dealing with [[Signedness|signed]] (positive and negative) numbers. Because most numbers in programs need to support negative numbers, an abstraction is used, redefining the bits' meaning to include a sign. The most common solution is [[two's complement]]. Most programming languages provide this construct. A signed 32-bit integer, will use the [[Bit numbering#Most_significant_bit|most significant bit]] to signify the [[Sign bit|sign]] (positive or negative), and the remaining [[Bit numbering#Signed_integer_example|31-bits]] to represent the number. When an [[Two's_complement#Arithmetic_operations|operation]] occurs that results in a [[Carry (arithmetic)|carry]] past the 31-bits allocated for the number, the sign bit is overwritten. The ALU doesn't know it did anything wrong. It is up to the program to detect this overflow fault.
 
For usage of unsigned integers of [[register width]], the ALU is not capable of returning a result with more bits outside the it'sits width. The ALU will return the result along with a flag for carry-out. When these flags are returned true, the ALU has detected overflow.
 
After overflow is detected, it is up to the program to handle this with additional logic. The resulting value from the operation is [[Data corruption|corrupted]] and can cause additional issues if not handled properly.
 
Using integers of the same size as the [[Arithmetic logic unit|ALU]]'s [[register width]] will have the best performance in most applications. [[Single instruction, multiple data|SIMD]] [[Instruction set architecture|instruction]] extensions can provide single operations for integers exceeding the register width. For [[x86]] [[32-bit computing|32-bit processors]] the [[Streaming SIMD extensions]] (SSE2) added registers for 64-bit integers. For [[x86-64]] [[64-bit computing|64-bit processors]] the [[Advanced Vector Extensions]] (AVX) added registers up to 512-bit integers.<ref>{{cite web|url=https://www.intel.com/content/www/us/en/content-details/812656/intel-avx-512-fast-modular-multiplication-technique-technology-guide.html|title=Intel® AVX-512 - Fast Modular Multiplication Technique}}</ref>
Line 138:
| [[Python (programming language)|Python]] 2 || {{N/A}} || convert to <var>long</var> type (bigint)
|-
| [[Seed7]] || {{N/A}} || <samp>'''raise''' OVERFLOW_ERROR</samp><ref>[httphttps://seed7.sourceforge.net/manual/errors.htm#OVERFLOW_ERROR Seed7 manual], section 16.3.3 OVERFLOW_ERROR.</ref>
|-
| [[Scheme (programming language)|Scheme]] || {{N/A}} || convert to bigNum