Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
95
Task/Integer-overflow/00DESCRIPTION
Normal file
95
Task/Integer-overflow/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
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.
|
||||
|
||||
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
|
||||
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
|
||||
!Result that does not fit into a 32-bit signed integer
|
||||
|-
|
||||
| -(-2147483647-1)
|
||||
| 2147483648
|
||||
|-
|
||||
| 2000000000 + 2000000000
|
||||
| 4000000000
|
||||
|-
|
||||
| -2147483647 - 2147483647
|
||||
| -4294967294
|
||||
|-
|
||||
| 46341 * 46341
|
||||
| 2147488281
|
||||
|-
|
||||
| (-2147483647-1) / -1
|
||||
| 2147483648
|
||||
|}
|
||||
For 64-bit signed integers:
|
||||
{|class="wikitable"
|
||||
!Expression
|
||||
!Result that does not fit into a 64-bit signed integer
|
||||
|-
|
||||
| -(-9223372036854775807-1)
|
||||
| 9223372036854775808
|
||||
|-
|
||||
| 5000000000000000000+5000000000000000000
|
||||
| 10000000000000000000
|
||||
|-
|
||||
| -9223372036854775807 - 9223372036854775807
|
||||
| -18446744073709551614
|
||||
|-
|
||||
| 3037000500 * 3037000500
|
||||
| 9223372037000250000
|
||||
|-
|
||||
| (-9223372036854775807-1) / -1
|
||||
| 9223372036854775808
|
||||
|}
|
||||
For 32-bit unsigned integers:
|
||||
{|class="wikitable"
|
||||
!Expression
|
||||
!Result that does not fit into a 32-bit unsigned integer
|
||||
|-
|
||||
| -4294967295
|
||||
| -4294967295
|
||||
|-
|
||||
| 3000000000 + 3000000000
|
||||
| 6000000000
|
||||
|-
|
||||
| 2147483647 - 4294967295
|
||||
| -2147483648
|
||||
|-
|
||||
| 65537 * 65537
|
||||
| 4295098369
|
||||
|}
|
||||
For 64-bit unsigned integers:
|
||||
{|class="wikitable"
|
||||
!Expression
|
||||
!Result that does not fit into a 64-bit unsigned integer
|
||||
|-
|
||||
| -18446744073709551615
|
||||
| -18446744073709551615
|
||||
|-
|
||||
| 10000000000000000000 + 10000000000000000000
|
||||
| 20000000000000000000
|
||||
|-
|
||||
| 9223372036854775807 - 18446744073709551615
|
||||
| -9223372036854775808
|
||||
|-
|
||||
| 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.
|
||||
This should be done for signed and unsigned integers of various sizes supported by the language.
|
||||
When a language has no fixed size integer type or when no integer overflow can occur
|
||||
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.
|
||||
51
Task/Integer-overflow/Ada/integer-overflow.ada
Normal file
51
Task/Integer-overflow/Ada/integer-overflow.ada
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Overflow is
|
||||
|
||||
generic
|
||||
type T is Range <>;
|
||||
Name_Of_T: String;
|
||||
procedure Print_Bounds; -- first instantiate this with T, Name
|
||||
-- then call the instantiation
|
||||
procedure Print_Bounds is
|
||||
begin
|
||||
Put_Line(" " & Name_Of_T & " " & T'Image(T'First)
|
||||
& " .." & T'Image(T'Last));
|
||||
end Print_Bounds;
|
||||
|
||||
procedure P_Int is new Print_Bounds(Integer, "Integer ");
|
||||
procedure P_Nat is new Print_Bounds(Natural, "Natural ");
|
||||
procedure P_Pos is new Print_Bounds(Positive, "Positive");
|
||||
procedure P_Long is new Print_Bounds(Long_Integer, "Long ");
|
||||
|
||||
type Unsigned_Byte is range 0 .. 255;
|
||||
type Signed_Byte is range -128 .. 127;
|
||||
type Unsigned_Word is range 0 .. 2**32-1;
|
||||
type Thousand is range 0 .. 999;
|
||||
type Signed_Double is range - 2**63 .. 2**63-1;
|
||||
type Crazy is range -11 .. -3;
|
||||
|
||||
procedure P_UB is new Print_Bounds(Unsigned_Byte, "U 8 ");
|
||||
procedure P_SB is new Print_Bounds(Signed_Byte, "S 8 ");
|
||||
procedure P_UW is new Print_Bounds(Unsigned_Word, "U 32 ");
|
||||
procedure P_Th is new Print_Bounds(Thousand, "Thous");
|
||||
procedure P_SD is new Print_Bounds(Signed_Double, "S 64 ");
|
||||
procedure P_Cr is new Print_Bounds(Crazy, "Crazy");
|
||||
|
||||
A: Crazy := Crazy'First;
|
||||
|
||||
begin
|
||||
Put_Line("Predefined Types:");
|
||||
P_Int; P_Nat; P_Pos; P_Long;
|
||||
New_Line;
|
||||
|
||||
Put_Line("Types defined by the user:");
|
||||
P_UB; P_SB; P_UW; P_Th; P_SD; P_Cr;
|
||||
New_Line;
|
||||
|
||||
Put_Line("Forcing a variable to overflow:");
|
||||
loop -- endless loop
|
||||
Put(" " & Crazy'Image(A) & "+1");
|
||||
A := A + 1;
|
||||
end loop;
|
||||
end Overflow;
|
||||
1
Task/Integer-overflow/AutoHotkey/integer-overflow.ahk
Normal file
1
Task/Integer-overflow/AutoHotkey/integer-overflow.ahk
Normal file
|
|
@ -0,0 +1 @@
|
|||
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
|
||||
28
Task/Integer-overflow/C/integer-overflow.c
Normal file
28
Task/Integer-overflow/C/integer-overflow.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
printf("Signed 32-bit:\n");
|
||||
printf("%d\n", -(-2147483647-1));
|
||||
printf("%d\n", 2000000000 + 2000000000);
|
||||
printf("%d\n", -2147483647 - 2147483647);
|
||||
printf("%d\n", 46341 * 46341);
|
||||
printf("%d\n", (-2147483647-1) / -1);
|
||||
printf("Signed 64-bit:\n");
|
||||
printf("%ld\n", -(-9223372036854775807-1));
|
||||
printf("%ld\n", 5000000000000000000+5000000000000000000);
|
||||
printf("%ld\n", -9223372036854775807 - 9223372036854775807);
|
||||
printf("%ld\n", 3037000500 * 3037000500);
|
||||
printf("%ld\n", (-9223372036854775807-1) / -1);
|
||||
printf("Unsigned 32-bit:\n");
|
||||
printf("%u\n", -4294967295U);
|
||||
printf("%u\n", 3000000000U + 3000000000U);
|
||||
printf("%u\n", 2147483647U - 4294967295U);
|
||||
printf("%u\n", 65537U * 65537U);
|
||||
printf("Unsigned 64-bit:\n");
|
||||
printf("%lu\n", -18446744073709551615LU);
|
||||
printf("%lu\n", 10000000000000000000LU + 10000000000000000000LU);
|
||||
printf("%lu\n", 9223372036854775807LU - 18446744073709551615LU);
|
||||
printf("%lu\n", 4294967296LU * 4294967296LU);
|
||||
return 0;
|
||||
}
|
||||
4
Task/Integer-overflow/Clojure/integer-overflow-1.clj
Normal file
4
Task/Integer-overflow/Clojure/integer-overflow-1.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(* -1 (dec -9223372036854775807))
|
||||
(+ 5000000000000000000 5000000000000000000)
|
||||
(- -9223372036854775807 9223372036854775807)
|
||||
(* 3037000500 3037000500)
|
||||
8
Task/Integer-overflow/Clojure/integer-overflow-2.clj
Normal file
8
Task/Integer-overflow/Clojure/integer-overflow-2.clj
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(set! *unchecked-math* true)
|
||||
(* -1 (dec -9223372036854775807)) ;=> -9223372036854775808
|
||||
(+ 5000000000000000000 5000000000000000000) ;=> -8446744073709551616
|
||||
(- -9223372036854775807 9223372036854775807) ;=> 2
|
||||
(* 3037000500 3037000500) ;=> -9223372036709301616
|
||||
; Note: The following division will currently silently overflow regardless of *unchecked-math*
|
||||
; See: http://dev.clojure.org/jira/browse/CLJ-1253
|
||||
(/ (dec -9223372036854775807) -1) ;=> -9223372036854775808
|
||||
37
Task/Integer-overflow/D/integer-overflow.d
Normal file
37
Task/Integer-overflow/D/integer-overflow.d
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
void main() @safe {
|
||||
import std.stdio;
|
||||
|
||||
writeln("Signed 32-bit:");
|
||||
writeln(-(-2_147_483_647 - 1));
|
||||
writeln(2_000_000_000 + 2_000_000_000);
|
||||
writeln(-2147483647 - 2147483647);
|
||||
writeln(46_341 * 46_341);
|
||||
writeln((-2_147_483_647 - 1) / -1);
|
||||
|
||||
writeln("\nSigned 64-bit:");
|
||||
writeln(-(-9_223_372_036_854_775_807 - 1));
|
||||
writeln(5_000_000_000_000_000_000 + 5_000_000_000_000_000_000);
|
||||
writeln(-9_223_372_036_854_775_807 - 9_223_372_036_854_775_807);
|
||||
writeln(3_037_000_500 * 3_037_000_500);
|
||||
writeln((-9_223_372_036_854_775_807 - 1) / -1);
|
||||
|
||||
writeln("\nUnsigned 32-bit:");
|
||||
writeln(-4_294_967_295U);
|
||||
writeln(3_000_000_000U + 3_000_000_000U);
|
||||
writeln(2_147_483_647U - 4_294_967_295U);
|
||||
writeln(65_537U * 65_537U);
|
||||
|
||||
writeln("\nUnsigned 64-bit:");
|
||||
writeln(-18_446_744_073_709_551_615UL);
|
||||
writeln(10_000_000_000_000_000_000UL + 10_000_000_000_000_000_000UL);
|
||||
writeln(9_223_372_036_854_775_807UL - 18_446_744_073_709_551_615UL);
|
||||
writeln(4_294_967_296UL * 4_294_967_296UL);
|
||||
|
||||
import core.checkedint;
|
||||
bool overflow = false;
|
||||
// Checked signed multiplication.
|
||||
// Eventually such functions will be recognized by D compilers
|
||||
// and they will be implemented with efficient intrinsics.
|
||||
immutable r = muls(46_341, 46_341, overflow);
|
||||
writeln("\n", r, " ", overflow);
|
||||
}
|
||||
108
Task/Integer-overflow/Go/integer-overflow.go
Normal file
108
Task/Integer-overflow/Go/integer-overflow.go
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// Go's builtin integer types are:
|
||||
// int, int8, int16, int32, int64
|
||||
// uint, uint8, uint16, uint32, uint64
|
||||
// byte, rune, uintptr
|
||||
//
|
||||
// int is either 32 or 64 bit, depending on the system
|
||||
// uintptr is large enough to hold the bit pattern of any pointer
|
||||
// byte is 8 bits like int8
|
||||
// rune is 32 bits like int32
|
||||
//
|
||||
// Overflow and underflow is silent. The math package defines a number
|
||||
// of constants that can be helpfull, e.g.:
|
||||
// math.MaxInt64 = 1<<63 - 1
|
||||
// math.MinInt64 = -1 << 63
|
||||
// math.MaxUint64 = 1<<64 - 1
|
||||
//
|
||||
// The math/big package implements multi-precision
|
||||
// arithmetic (big numbers).
|
||||
//
|
||||
// In all cases assignment from one type to another requires
|
||||
// an explicit cast, even if the types are otherwise identical
|
||||
// (e.g. rune and int32 or int and either int32 or int64).
|
||||
// Casts silently truncate if required.
|
||||
//
|
||||
// Invalid:
|
||||
// var i int = int32(0)
|
||||
// var r rune = int32(0)
|
||||
// var b byte = int8(0)
|
||||
//
|
||||
// Valid:
|
||||
var i64 int64 = 42
|
||||
var i32 int32 = int32(i64)
|
||||
var i16 int16 = int16(i64)
|
||||
var i8 int8 = int8(i16)
|
||||
var i int = int(i8)
|
||||
var r rune = rune(i)
|
||||
var b byte = byte(r)
|
||||
var u64 uint64 = uint64(b)
|
||||
var u32 uint32
|
||||
|
||||
//const c int = -(-2147483647 - 1) // Compiler error on 32 bit systems, ok on 64 bit
|
||||
const c = -(-2147483647 - 1) // Allowed even on 32 bit systems, c is untyped
|
||||
i64 = c
|
||||
//i32 = c // Compiler error
|
||||
//i32 = -(-2147483647 - 1) // Compiler error
|
||||
i32 = -2147483647
|
||||
i32 = -(-i32 - 1)
|
||||
fmt.Println("32 bit signed integers")
|
||||
fmt.Printf(" -(-2147483647 - 1) = %d, got %d\n", i64, i32)
|
||||
|
||||
i64 = 2000000000 + 2000000000
|
||||
//i32 = 2000000000 + 2000000000 // Compiler error
|
||||
i32 = 2000000000
|
||||
i32 = i32 + i32
|
||||
fmt.Printf(" 2000000000 + 2000000000 = %d, got %d\n", i64, i32)
|
||||
i64 = -2147483647 - 2147483647
|
||||
i32 = 2147483647
|
||||
i32 = -i32 - i32
|
||||
fmt.Printf(" -2147483647 - 2147483647 = %d, got %d\n", i64, i32)
|
||||
i64 = 46341 * 46341
|
||||
i32 = 46341
|
||||
i32 = i32 * i32
|
||||
fmt.Printf(" 46341 * 46341 = %d, got %d\n", i64, i32)
|
||||
i64 = (-2147483647 - 1) / -1
|
||||
i32 = -2147483647
|
||||
i32 = (i32 - 1) / -1
|
||||
fmt.Printf(" (-2147483647-1) / -1 = %d, got %d\n", i64, i32)
|
||||
|
||||
fmt.Println("\n64 bit signed integers")
|
||||
i64 = -9223372036854775807
|
||||
fmt.Printf(" -(%d - 1): %d\n", i64, -(i64 - 1))
|
||||
i64 = 5000000000000000000
|
||||
fmt.Printf(" %d + %d: %d\n", i64, i64, i64+i64)
|
||||
i64 = 9223372036854775807
|
||||
fmt.Printf(" -%d - %d: %d\n", i64, i64, -i64-i64)
|
||||
i64 = 3037000500
|
||||
fmt.Printf(" %d * %d: %d\n", i64, i64, i64*i64)
|
||||
i64 = -9223372036854775807
|
||||
fmt.Printf(" (%d - 1) / -1: %d\n", i64, (i64-1)/-1)
|
||||
|
||||
fmt.Println("\n32 bit unsigned integers:")
|
||||
//u32 = -4294967295 // Compiler error
|
||||
u32 = 4294967295
|
||||
fmt.Printf(" -%d: %d\n", u32, -u32)
|
||||
u32 = 3000000000
|
||||
fmt.Printf(" %d + %d: %d\n", u32, u32, u32+u32)
|
||||
a := uint32(2147483647)
|
||||
u32 = 4294967295
|
||||
fmt.Printf(" %d - %d: %d\n", a, u32, a-u32)
|
||||
u32 = 65537
|
||||
fmt.Printf(" %d * %d: %d\n", u32, u32, u32*u32)
|
||||
|
||||
fmt.Println("\n64 bit unsigned integers:")
|
||||
u64 = 18446744073709551615
|
||||
fmt.Printf(" -%d: %d\n", u64, -u64)
|
||||
u64 = 10000000000000000000
|
||||
fmt.Printf(" %d + %d: %d\n", u64, u64, u64+u64)
|
||||
aa := uint64(9223372036854775807)
|
||||
u64 = 18446744073709551615
|
||||
fmt.Printf(" %d - %d: %d\n", aa, u64, aa-u64)
|
||||
u64 = 4294967296
|
||||
fmt.Printf(" %d * %d: %d\n", u64, u64, u64*u64)
|
||||
}
|
||||
26
Task/Integer-overflow/Haskell/integer-overflow.hs
Normal file
26
Task/Integer-overflow/Haskell/integer-overflow.hs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import Data.Int
|
||||
import Data.Word
|
||||
import Control.Exception
|
||||
|
||||
f x = do
|
||||
catch (print x) (\e -> print (e :: ArithException))
|
||||
|
||||
main = do
|
||||
f ((- (-2147483647 - 1)) :: Int32)
|
||||
f ((2000000000 + 2000000000) :: Int32)
|
||||
f (((-2147483647) - 2147483647) :: Int32)
|
||||
f ((46341 * 46341) :: Int32)
|
||||
f ((((-2147483647) - 1) `div` (-1)) :: Int32)
|
||||
f ((- ((-9223372036854775807) - 1)) :: Int64)
|
||||
f ((5000000000000000000 + 5000000000000000000) :: Int64)
|
||||
f (((-9223372036854775807) - 9223372036854775807) :: Int64)
|
||||
f ((3037000500 * 3037000500) :: Int64)
|
||||
f ((((-9223372036854775807) - 1) `div` (-1)) :: Int64)
|
||||
f ((-4294967295) :: Word32)
|
||||
f ((3000000000 + 3000000000) :: Word32)
|
||||
f ((2147483647 - 4294967295) :: Word32)
|
||||
f ((65537 * 65537) :: Word32)
|
||||
f ((-18446744073709551615) :: Word64)
|
||||
f ((10000000000000000000 + 10000000000000000000) :: Word64)
|
||||
f ((9223372036854775807 - 18446744073709551615) :: Word64)
|
||||
f ((4294967296 * 4294967296) :: Word64)
|
||||
39
Task/Integer-overflow/J/integer-overflow-1.j
Normal file
39
Task/Integer-overflow/J/integer-overflow-1.j
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
-(_2147483647-1)
|
||||
2.14748e9
|
||||
2000000000 + 2000000000
|
||||
4e9
|
||||
_2147483647 - 2147483647
|
||||
_4.29497e9
|
||||
46341 * 46341
|
||||
2.14749e9
|
||||
(_2147483647-1) % -1
|
||||
2.14748e9
|
||||
|
||||
-(_9223372036854775807-1)
|
||||
9.22337e18
|
||||
5000000000000000000+5000000000000000000
|
||||
1e19
|
||||
_9223372036854775807 - 9223372036854775807
|
||||
_1.84467e19
|
||||
3037000500 * 3037000500
|
||||
9.22337e18
|
||||
(_9223372036854775807-1) % -1
|
||||
9.22337e18
|
||||
|
||||
_4294967295
|
||||
_4.29497e9
|
||||
3000000000 + 3000000000
|
||||
6e9
|
||||
2147483647 - 4294967295
|
||||
_2.14748e9
|
||||
65537 * 65537
|
||||
4.2951e9
|
||||
|
||||
_18446744073709551615
|
||||
_1.84467e19
|
||||
10000000000000000000 + 10000000000000000000
|
||||
2e19
|
||||
9223372036854775807 - 18446744073709551615
|
||||
_9.22337e18
|
||||
4294967296 * 4294967296
|
||||
1.84467e19
|
||||
39
Task/Integer-overflow/J/integer-overflow-2.j
Normal file
39
Task/Integer-overflow/J/integer-overflow-2.j
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
-(_2147483647-1)
|
||||
2147483648
|
||||
2000000000 + 2000000000
|
||||
4000000000
|
||||
_2147483647 - 2147483647
|
||||
_4294967294
|
||||
46341 * 46341
|
||||
2147488281
|
||||
(_2147483647-1) % -1
|
||||
2.14748e9
|
||||
|
||||
-(_9223372036854775807-1)
|
||||
9.22337e18
|
||||
5000000000000000000+5000000000000000000
|
||||
1e19
|
||||
_9223372036854775807 - 9223372036854775807
|
||||
_1.84467e19
|
||||
3037000500 * 3037000500
|
||||
9.22337e18
|
||||
(_9223372036854775807-1) % -1
|
||||
9.22337e18
|
||||
|
||||
_4294967295
|
||||
_4294967295
|
||||
3000000000 + 3000000000
|
||||
6000000000
|
||||
2147483647 - 4294967295
|
||||
_2147483648
|
||||
65537 * 65537
|
||||
4295098369
|
||||
|
||||
_18446744073709551615
|
||||
_1.84467e19
|
||||
10000000000000000000 + 10000000000000000000
|
||||
2e19
|
||||
9223372036854775807 - 18446744073709551615
|
||||
_9.22337e18
|
||||
4294967296 * 4294967296
|
||||
1.84467e19
|
||||
39
Task/Integer-overflow/J/integer-overflow-3.j
Normal file
39
Task/Integer-overflow/J/integer-overflow-3.j
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
-(_2147483647-1)
|
||||
2147483648
|
||||
2000000000 + 2000000000
|
||||
4000000000
|
||||
_2147483647 - 2147483647
|
||||
_4294967294
|
||||
46341 * 46341
|
||||
2147488281
|
||||
(_2147483647-1) % -1
|
||||
2147483648
|
||||
|
||||
-(_9223372036854775807-1)
|
||||
9223372036854775800
|
||||
5000000000000000000+5000000000000000000
|
||||
10000000000000000000
|
||||
_9223372036854775807 - 9223372036854775807
|
||||
_18446744073709552000
|
||||
3037000500 * 3037000500
|
||||
9223372037000249300
|
||||
(_9223372036854775807-1) % -1
|
||||
9223372036854775800
|
||||
|
||||
_4294967295
|
||||
_4294967295
|
||||
3000000000 + 3000000000
|
||||
6000000000
|
||||
2147483647 - 4294967295
|
||||
_2147483648
|
||||
65537 * 65537
|
||||
4295098369
|
||||
|
||||
_18446744073709551615
|
||||
_18446744073709552000
|
||||
10000000000000000000 + 10000000000000000000
|
||||
20000000000000000000
|
||||
9223372036854775807 - 18446744073709551615
|
||||
_9223372036854775800
|
||||
4294967296 * 4294967296
|
||||
18446744073709552000
|
||||
39
Task/Integer-overflow/J/integer-overflow-4.j
Normal file
39
Task/Integer-overflow/J/integer-overflow-4.j
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
-(_2147483647-1)
|
||||
2147483648
|
||||
2000000000 + 2000000000
|
||||
4000000000
|
||||
_2147483647 - 2147483647
|
||||
_4294967294
|
||||
46341 * 46341
|
||||
2147488281
|
||||
(_2147483647-1) % -1
|
||||
2147483648
|
||||
|
||||
-(_9223372036854775807-1)
|
||||
9223372036854775800
|
||||
5000000000000000000+5000000000000000000
|
||||
10000000000000000000
|
||||
_9223372036854775807 - 9223372036854775807
|
||||
_18446744073709552000
|
||||
3037000500 * 3037000500
|
||||
9223372037000249300
|
||||
(_9223372036854775807-1) % -1
|
||||
9223372036854775800
|
||||
|
||||
_4294967295
|
||||
_4294967295
|
||||
3000000000 + 3000000000
|
||||
6000000000
|
||||
2147483647 - 4294967295
|
||||
_2147483648
|
||||
65537 * 65537
|
||||
4295098369
|
||||
|
||||
_18446744073709551615
|
||||
_18446744073709552000
|
||||
10000000000000000000 + 10000000000000000000
|
||||
20000000000000000000
|
||||
9223372036854775807 - 18446744073709551615
|
||||
_9223372036854775800
|
||||
4294967296 * 4294967296
|
||||
18446744073709552000
|
||||
18
Task/Integer-overflow/Java/integer-overflow.java
Normal file
18
Task/Integer-overflow/Java/integer-overflow.java
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
public class integerOverflow {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Signed 32-bit:");
|
||||
System.out.println(-(-2147483647-1));
|
||||
System.out.println(2000000000 + 2000000000);
|
||||
System.out.println(-2147483647 - 2147483647);
|
||||
System.out.println(46341 * 46341);
|
||||
System.out.println((-2147483647-1) / -1);
|
||||
System.out.println("Signed 64-bit:");
|
||||
System.out.println(-(-9223372036854775807L-1));
|
||||
System.out.println(5000000000000000000L+5000000000000000000L);
|
||||
System.out.println(-9223372036854775807L - 9223372036854775807L);
|
||||
System.out.println(3037000500L * 3037000500L);
|
||||
System.out.println((-9223372036854775807L-1) / -1);
|
||||
}
|
||||
|
||||
}
|
||||
11
Task/Integer-overflow/Perl/integer-overflow.pl
Normal file
11
Task/Integer-overflow/Perl/integer-overflow.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use integer;
|
||||
use feature 'say';
|
||||
|
||||
say("Testing 64-bit signed overflow:");
|
||||
say(-(-9223372036854775807-1));
|
||||
say(5000000000000000000+5000000000000000000);
|
||||
say(-9223372036854775807 - 9223372036854775807);
|
||||
say(3037000500 * 3037000500);
|
||||
say((-9223372036854775807-1) / -1);
|
||||
18
Task/Integer-overflow/Python/integer-overflow-1.py
Normal file
18
Task/Integer-overflow/Python/integer-overflow-1.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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.
|
||||
>>> for calc in ''' -(-2147483647-1)
|
||||
2000000000 + 2000000000
|
||||
-2147483647 - 2147483647
|
||||
46341 * 46341
|
||||
(-2147483647-1) / -1'''.split('\n'):
|
||||
ans = eval(calc)
|
||||
print('Expression: %r evaluates to %s of type %s'
|
||||
% (calc.strip(), ans, type(ans)))
|
||||
|
||||
|
||||
Expression: '-(-2147483647-1)' evaluates to 2147483648 of type <type 'long'>
|
||||
Expression: '2000000000 + 2000000000' evaluates to 4000000000 of type <type 'long'>
|
||||
Expression: '-2147483647 - 2147483647' evaluates to -4294967294 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'>
|
||||
>>>
|
||||
18
Task/Integer-overflow/Python/integer-overflow-2.py
Normal file
18
Task/Integer-overflow/Python/integer-overflow-2.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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.
|
||||
>>> for calc in ''' -(-2147483647-1)
|
||||
2000000000 + 2000000000
|
||||
-2147483647 - 2147483647
|
||||
46341 * 46341
|
||||
(-2147483647-1) / -1'''.split('\n'):
|
||||
ans = eval(calc)
|
||||
print('Expression: %r evaluates to %s of type %s'
|
||||
% (calc.strip(), ans, type(ans)))
|
||||
|
||||
|
||||
Expression: '-(-2147483647-1)' evaluates to 2147483648 of type <class 'int'>
|
||||
Expression: '2000000000 + 2000000000' evaluates to 4000000000 of type <class 'int'>
|
||||
Expression: '-2147483647 - 2147483647' evaluates to -4294967294 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'>
|
||||
>>>
|
||||
1
Task/Integer-overflow/README
Normal file
1
Task/Integer-overflow/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/Integer_overflow
|
||||
16
Task/Integer-overflow/REXX/integer-overflow.rexx
Normal file
16
Task/Integer-overflow/REXX/integer-overflow.rexx
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/*REXX pgm displays values when integers have an overflow or underflow.*/
|
||||
numeric digits 9 /*default is nine decimal digits.*/
|
||||
call showResult( 999999997 + 1 )
|
||||
call showResult( 999999998 + 1 )
|
||||
call showResult( 999999999 + 1 )
|
||||
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.*/
|
||||
8
Task/Integer-overflow/Ruby/integer-overflow.rb
Normal file
8
Task/Integer-overflow/Ruby/integer-overflow.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
2.1.1 :001 > a = 2**62 -1
|
||||
=> 4611686018427387903
|
||||
2.1.1 :002 > a.class
|
||||
=> Fixnum
|
||||
2.1.1 :003 > (b = a + 1).class
|
||||
=> Bignum
|
||||
2.1.1 :004 > (b-1).class
|
||||
=> Fixnum
|
||||
14
Task/Integer-overflow/Scala/integer-overflow.scala
Normal file
14
Task/Integer-overflow/Scala/integer-overflow.scala
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Math.{addExact => ++, multiplyExact => **, negateExact => ~~, subtractExact => --}
|
||||
|
||||
def requireOverflow(f: => Unit) =
|
||||
try {f; println("Undetected overflow")} catch{case e: Exception => /* caught */}
|
||||
|
||||
println("Testing overflow detection for 32-bit unsigned integers")
|
||||
requireOverflow(~~(--(~~(2147483647), 1))) // -(-2147483647-1)
|
||||
requireOverflow(++(2000000000, 2000000000)) // 2000000000 + 2000000000
|
||||
requireOverflow(--(~~(2147483647), 2147483647)) // -2147483647 + 2147483647
|
||||
requireOverflow(**(46341, 46341)) // 46341 * 46341
|
||||
requireOverflow(**(--(~~(2147483647),1), -1)) // same as (-2147483647-1) / -1
|
||||
|
||||
println("Test - Expect Undetected overflow:")
|
||||
requireOverflow(++(1,1)) // Undetected overflow
|
||||
19
Task/Integer-overflow/Seed7/integer-overflow.seed7
Normal file
19
Task/Integer-overflow/Seed7/integer-overflow.seed7
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: writeResult (ref func integer: expression) is func
|
||||
begin
|
||||
block
|
||||
writeln(expression);
|
||||
exception
|
||||
catch OVERFLOW_ERROR: writeln("OVERFLOW_ERROR");
|
||||
end block;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeResult(-(-9223372036854775807-1));
|
||||
writeResult(5000000000000000000+5000000000000000000);
|
||||
writeResult(-9223372036854775807 - 9223372036854775807);
|
||||
writeResult(3037000500 * 3037000500);
|
||||
writeResult((-9223372036854775807-1) div -1);
|
||||
end func;
|
||||
4
Task/Integer-overflow/Tcl/integer-overflow.tcl
Normal file
4
Task/Integer-overflow/Tcl/integer-overflow.tcl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
proc tcl::mathfunc::clamp32 {x} {
|
||||
expr {$x<0 ? -((-$x) & 0x7fffffff) : $x & 0x7fffffff}
|
||||
}
|
||||
puts [expr { clamp32(2000000000 + 2000000000) }]; # ==> 1852516352
|
||||
Loading…
Add table
Add a link
Reference in a new issue