2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,16 +1,26 @@
|
|||
Some languages support one or more integer types of the underlying processor.
|
||||
This integer types have fixed size. Usually 8-bit, 16-bit, 32-bit or 64-bit.
|
||||
The integers supported by such a type can be signed or unsigned.
|
||||
Arithmetic for machine level integers can often be done by single CPU instructions.
|
||||
This allows high performance and is the main reason to support machine level integers.
|
||||
|
||||
This integer types have fixed size; usually '''8'''-bit, '''16'''-bit, '''32'''-bit, or '''64'''-bit.
|
||||
<br>The integers supported by such a type can be ''signed'' or ''unsigned''.
|
||||
|
||||
Arithmetic for machine level integers can often be done by single CPU instructions.
|
||||
<br>This allows high performance and is the main reason to support machine level integers.
|
||||
|
||||
|
||||
;Definition:
|
||||
An integer overflow happens when the result of a computation does not fit into the fixed size integer.
|
||||
The result can be too small or too big to be representable in the fixed size integer.
|
||||
|
||||
When a language has fixed size integer types, the task is to write a program that
|
||||
|
||||
;Task:
|
||||
When a language has fixed size integer types, create a program that
|
||||
does arithmetic computations for the fixed size integers of the language.
|
||||
|
||||
These computations must be done such that the result would overflow.
|
||||
|
||||
The program should demonstrate what the following expressions do.
|
||||
|
||||
|
||||
For 32-bit signed integers:
|
||||
{|class="wikitable"
|
||||
!Expression
|
||||
|
|
@ -31,6 +41,7 @@ For 32-bit signed integers:
|
|||
| (-2147483647-1) / -1
|
||||
| 2147483648
|
||||
|}
|
||||
|
||||
For 64-bit signed integers:
|
||||
{|class="wikitable"
|
||||
!Expression
|
||||
|
|
@ -51,6 +62,7 @@ For 64-bit signed integers:
|
|||
| (-9223372036854775807-1) / -1
|
||||
| 9223372036854775808
|
||||
|}
|
||||
|
||||
For 32-bit unsigned integers:
|
||||
{|class="wikitable"
|
||||
!Expression
|
||||
|
|
@ -68,6 +80,7 @@ For 32-bit unsigned integers:
|
|||
| 65537 * 65537
|
||||
| 4295098369
|
||||
|}
|
||||
|
||||
For 64-bit unsigned integers:
|
||||
{|class="wikitable"
|
||||
!Expression
|
||||
|
|
@ -85,6 +98,7 @@ For 64-bit unsigned integers:
|
|||
| 4294967296 * 4294967296
|
||||
| 18446744073709551616
|
||||
|}
|
||||
|
||||
When the integer overflow does trigger an exception show how the exception is catched.
|
||||
When the integer overflow produces some value print it.
|
||||
It should be explicitly noted when an integer overflow is not recognized and the program continues with wrong results.
|
||||
|
|
@ -93,3 +107,4 @@ When a language has no fixed size integer type or when no integer overflow can o
|
|||
for other reasons this should be noted.
|
||||
It is okay to mention, when a language supports unlimited precision integers, but
|
||||
this task is NOT the place to demonstrate the capabilities of unlimited precision integers.
|
||||
<br><br>
|
||||
|
|
|
|||
49
Task/Integer-overflow/C-sharp/integer-overflow.cs
Normal file
49
Task/Integer-overflow/C-sharp/integer-overflow.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
|
||||
public class IntegerOverflow
|
||||
{
|
||||
public static void Main() {
|
||||
unchecked {
|
||||
Console.WriteLine("For 32-bit signed integers:");
|
||||
Console.WriteLine(-(-2147483647 - 1));
|
||||
Console.WriteLine(2000000000 + 2000000000);
|
||||
Console.WriteLine(-2147483647 - 2147483647);
|
||||
Console.WriteLine(46341 * 46341);
|
||||
Console.WriteLine((-2147483647 - 1) / -1);
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("For 64-bit signed integers:");
|
||||
Console.WriteLine(-(-9223372036854775807L - 1));
|
||||
Console.WriteLine(5000000000000000000L + 5000000000000000000L);
|
||||
Console.WriteLine(-9223372036854775807L - 9223372036854775807L);
|
||||
Console.WriteLine(3037000500L * 3037000500L);
|
||||
Console.WriteLine((-9223372036854775807L - 1) / -1);
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("For 32-bit unsigned integers:");
|
||||
//Negating a 32-bit unsigned integer will convert it to a signed 64-bit integer.
|
||||
Console.WriteLine(-4294967295U);
|
||||
Console.WriteLine(3000000000U + 3000000000U);
|
||||
Console.WriteLine(2147483647U - 4294967295U);
|
||||
Console.WriteLine(65537U * 65537U);
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("For 64-bit unsigned integers:");
|
||||
// The - operator cannot be applied to 64-bit unsigned integers; it will always give a compile-time error.
|
||||
//Console.WriteLine(-18446744073709551615UL);
|
||||
Console.WriteLine(10000000000000000000UL + 10000000000000000000UL);
|
||||
Console.WriteLine(9223372036854775807UL - 18446744073709551615UL);
|
||||
Console.WriteLine(4294967296UL * 4294967296UL);
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
int i = 2147483647;
|
||||
Console.WriteLine(i + 1);
|
||||
try {
|
||||
checked { Console.WriteLine(i + 1); }
|
||||
} catch (OverflowException) {
|
||||
Console.WriteLine("Overflow!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
10
Task/Integer-overflow/COBOL/integer-overflow.cobol
Normal file
10
Task/Integer-overflow/COBOL/integer-overflow.cobol
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. PROCRUSTES-PROGRAM.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 WS-EXAMPLE.
|
||||
05 X PIC 999.
|
||||
PROCEDURE DIVISION.
|
||||
MOVE 1002 TO X.
|
||||
DISPLAY X UPON CONSOLE.
|
||||
STOP RUN.
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*REXX pgm displays values when integers have an overflow or underflow.*/
|
||||
numeric digits 9 /*default is nine decimal digits.*/
|
||||
/*REXX program displays values when integers have an overflow or underflow. */
|
||||
numeric digits 9 /*the REXX default is 9 decimal digits.*/
|
||||
call showResult( 999999997 + 1 )
|
||||
call showResult( 999999998 + 1 )
|
||||
call showResult( 999999999 + 1 )
|
||||
|
|
@ -7,10 +7,10 @@ call showResult( -999999998 - 2 )
|
|||
call showResult( 40000 * 25000 )
|
||||
call showResult( -50000 * 20000 )
|
||||
call showResult( 50000 *-30000 )
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SHOWRESULT subroutine───────────────*/
|
||||
showResult: procedure; parse arg x,,_; x=x/1 /*normalize X. */
|
||||
if pos('E',x)\==0 then if x>0 then _=' [overflow]' /*did it ↑flow?*/
|
||||
else _=' [underflow]' /*did it ↓flow?*/
|
||||
say right(x,20) _ /*show result. */
|
||||
return x /*return value.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
showResult: procedure; parse arg x,,_; x=x/1 /*normalize X. */
|
||||
if pos(., x)\==0 then if x>0 then _=' [overflow]' /*did it overflow? */
|
||||
else _=' [underflow]' /*did it underflow? */
|
||||
say right(x, 20) _ /*show the result. */
|
||||
return x /*return the value. */
|
||||
|
|
|
|||
13
Task/Integer-overflow/Rust/integer-overflow-1.rust
Normal file
13
Task/Integer-overflow/Rust/integer-overflow-1.rust
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// The following will panic!
|
||||
let i32_1 : i32 = -(-2_147_483_647 - 1);
|
||||
let i32_2 : i32 = 2_000_000_000 + 2_000_000_000;
|
||||
let i32_3 : i32 = -2_147_483_647 - 2_147_483_647;
|
||||
let i32_4 : i32 = 46341 * 46341;
|
||||
let i32_5 : i32 = (-2_147_483_647 - 1) / -1;
|
||||
|
||||
// These will panic! also
|
||||
let i64_1 : i64 = -(-9_223_372_036_854_775_807 - 1);
|
||||
let i64_2 : i64 = 5_000_000_000_000_000_000 + 5_000_000_000_000_000_000;
|
||||
let i64_3 : i64 = -9_223_372_036_854_775_807 - 9_223_372_036_854_775_807;
|
||||
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;
|
||||
21
Task/Integer-overflow/Rust/integer-overflow-2.rust
Normal file
21
Task/Integer-overflow/Rust/integer-overflow-2.rust
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// The following will panic!, but only in release mode
|
||||
let u32_1 : u32 = !4_294_967_295 + 1;
|
||||
let u32_2 : u32 = 3_000_000_000 + 3_000_000_000;
|
||||
let u32_3 : u32 = 2_147_483_647 - 4_294_967_295;
|
||||
let u32_4 : u32 = 65_537 * 65_537;
|
||||
|
||||
// The following panics! in release mode
|
||||
let u64_1 : u64 = !18_446_744_073_709_551_615 + 1;
|
||||
let u64_2 : u64 = 10_000_000_000_000_000_000 + 10_000_000_000_000_000_000;
|
||||
let u64_3 : u64 = 9_223_372_036_854_775_807 - 18_446_744_073_709_551_615;
|
||||
let u64_4 : u64 = 4_294_967_296 * 4_294_967_296;
|
||||
|
||||
println!("{}", u32_1); // 1
|
||||
println!("{}", u32_2); // 1705032704
|
||||
println!("{}", u32_3); // 2147483648
|
||||
println!("{}", u32_4); // 131703
|
||||
|
||||
println!("{}", u64_1); // 1
|
||||
println!("{}", u64_2); // 1553255926290448384
|
||||
println!("{}", u64_3); // 9223372036854775808
|
||||
println!("{}", u64_4); // 0
|
||||
9
Task/Integer-overflow/Rust/integer-overflow-3.rust
Normal file
9
Task/Integer-overflow/Rust/integer-overflow-3.rust
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// The following will never panic!
|
||||
println!("{:?}", 65_537u32.checked_mul(65_537)); // None
|
||||
println!("{:?}", 65_537u32.saturating_mul(65_537)); // 4294967295
|
||||
println!("{:?}", 65_537u32.wrapping_mul(65_537)); // 131073
|
||||
|
||||
// These will never panic! either
|
||||
println!("{:?}", 65_537i32.checked_mul(65_537)); // None
|
||||
println!("{:?}", 65_537i32.saturating_mul(65_537)); // 2147483647
|
||||
println!("{:?}", 65_537i32.wrapping_mul(-65_537)); // -131073
|
||||
Loading…
Add table
Add a link
Reference in a new issue