Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,6 @@
begin
integer n;
n := 16383; % maximum integer value %
write(n+2);
write(n*2);
end

View 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 of type Crazy to overflow:");
loop -- endless loop
Put(" " & Crazy'Image(A) & "+1");
A := A + 1; -- line 49 -- this will later raise a CONSTRAINT_ERROR
end loop;
end Overflow;

View 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.

View file

@ -0,0 +1,78 @@
identification division.
program-id. overflowing.
data division.
working-storage section.
01 bit8-sized usage binary-char. *> standard
01 bit16-sized usage binary-short. *> standard
01 bit32-sized usage binary-long. *> standard
01 bit64-sized usage binary-double. *> standard
01 bit8-unsigned usage binary-char unsigned. *> standard
01 nebulous-size usage binary-c-long. *> extension
01 picture-size picture s999. *> standard
*> ***************************************************************
procedure division.
*> 32 bit signed integer
subtract 2147483647 from zero giving bit32-sized
display bit32-sized
subtract 1 from bit32-sized giving bit32-sized
ON SIZE ERROR display "32bit signed SIZE ERROR"
end-subtract
*> value was unchanged due to size error trap and trigger
display bit32-sized
display space
*> 8 bit unsigned, size tested, invalid results discarded
add -257 to zero giving bit8-unsigned
ON SIZE ERROR display "bit8-unsigned SIZE ERROR"
end-add
display bit8-unsigned
*> programmers can ignore the safety features
compute bit8-unsigned = -257
display "you asked for it: " bit8-unsigned
display space
*> fixed size
move 999 to picture-size
add 1 to picture-size
ON SIZE ERROR display "picture-sized SIZE ERROR"
end-add
display picture-size
*> programmers doing the following, inadvertently,
*> do not stay employed at banks for long
move 999 to picture-size
add 1 to picture-size
*> intermediate goes to 1000, left end truncated on storage
display "you asked for it: " picture-size
add 1 to picture-size
display "really? you want to keep doing this?: " picture-size
display space
*> C values are undefined by spec, only minimums givens
display "How many bytes in a C long? "
length of nebulous-size
", varies by platform"
display "Regardless, ON SIZE ERROR will catch any invalid result"
*> on a 64bit machine, C long of 8 bytes
add 1 to h'ffffffffffffffff' giving nebulous-size
ON SIZE ERROR display "binary-c-long SIZE ERROR"
end-add
display nebulous-size
*> value will still be in initial state, GnuCOBOL initializes to 0
*> value now goes to 1, no size error, that ship has sailed
add 1 to nebulous-size
ON SIZE ERROR display "binary-c-long size error"
end-add
display "error state is not persistent: ", nebulous-size
goback.
end program overflowing.

View file

@ -0,0 +1,38 @@
macro try (expr)
printf "%-*s # => ", padding, {{expr.stringify}}
begin
puts {{expr}}
rescue ex
puts "Exception: #{ex.message}"
end
end
padding = 25
puts "Result does not fit into a 32-bit signed integer:"
try -(-2147483647-1)
try 2000000000 + 2000000000
try -2147483647 - 2147483647
try 46341 * 46341
try (-2147483647-1) // -1
padding = 52
puts "\nResult does not fit into a 64-bit signed integer:"
try -(-9223372036854775807 - 1)
try 5000000000000000000 + 5000000000000000000
try -9223372036854775807 - 9223372036854775807
try 3037000500 * 3037000500
try (-9223372036854775807 - 1) // -1
padding = 32
puts "\nResult does not fit into a 32-bit unsigned integer:"
try -4294967295.to_u32
try 3000000000_u32 + 3000000000_u32
try 2147483647_u32 - 4294967295_u32
try 65537_u32 * 65537_u32
padding = 52
puts "\nResult that does not fit into a 64-bit unsigned integer:"
try -18446744073709551615_i128.to_u64
try 10000000000000000000_u64 + 10000000000000000000_u64
try 9223372036854775807_u64 - 18446744073709551615_u64
try 4294967296_u64 * 4294967296_64

View file

@ -1,6 +1,6 @@
var IS32: integer; {Signed 32-bit integer}
var IS64: Int64; {Signed 64-bit integer}
var IU32: cardinal; {Unsigned 32-bit integer}
var IS32: integer; {Signed 32-bit integer}
var IS64: Int64; {Signed 64-bit integer}
var IU32: cardinal; {Unsigned 32-bit integer}
{============ Signed 32 bit tests ===================================}

View file

@ -3,106 +3,106 @@ 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
// 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)
//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)
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("\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("\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)
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)
}

View file

@ -1,16 +1,16 @@
public final class IntegerOverflow {
public static void main(String[] args) {
// The following examples show that Java allows integer overflow without warning
// and calculates an incorrect result.
// From version 8, Java introduced methods which throw an ArithmeticException when overflow occurs,
public static void main(String[] args) {
// The following examples show that Java allows integer overflow without warning
// and calculates an incorrect result.
// From version 8, Java introduced methods which throw an ArithmeticException when overflow occurs,
// which prevents the calculation of an incorrect result. It also allows the programmer to replace an "int"
// with a "long" and to replace a "long" with a BigInteger.
// Uncomment the lines below to see the use of the new methods:
// addExact(), subtractExact(), multiplyExact() and negateExact().
System.out.println("Signed 32-bit:");
// Uncomment the lines below to see the use of the new methods:
// addExact(), subtractExact(), multiplyExact() and negateExact().
System.out.println("Signed 32-bit:");
System.out.println(-(-2_147_483_647 - 1));
// System.out.println(Math.negateExact(-2_147_483_647 - 1));
@ -42,6 +42,6 @@ public final class IntegerOverflow {
System.out.println((-9_223_372_036_854_775_807L - 1) / -1);
// System.out.println(Math.negateExact(Math.subtractExact(-9_223_372_036_854_775_807L, 1) / 1));
}
}
}

View file

@ -10,16 +10,16 @@ def compare:
[ -(-2147483647-1),"2147483648"],
[2000000000 + 2000000000, "4000000000"],
[-2147483647 - 2147483647, "-4294967294"],
[46341 * 46341, "2147488281"],
[(-2147483647-1) / -1, "2147483648"],
[-2147483647 - 2147483647, "-4294967294"],
[46341 * 46341, "2147488281"],
[(-2147483647-1) / -1, "2147483648"],
"For 64-bit signed integers:",
[-(-9223372036854775807-1), "9223372036854775808"],
[5000000000000000000+5000000000000000000, "10000000000000000000"],
[-9223372036854775807 - 9223372036854775807, "-18446744073709551614"],
[3037000500 * 3037000500, "9223372037000250000"],
[-(-9223372036854775807-1), "9223372036854775808"],
[5000000000000000000+5000000000000000000, "10000000000000000000"],
[-9223372036854775807 - 9223372036854775807, "-18446744073709551614"],
[3037000500 * 3037000500, "9223372037000250000"],
[(-9223372036854775807-1) / -1, "9223372036854775808"],
"For 32-bit unsigned integers:",

View file

@ -2,7 +2,7 @@
# Integer overflow
# # Variables:
# # Variables:
#
typeset -si SHORT_INT
typeset -i INTEGER

View file

@ -0,0 +1,6 @@
overflow_demo: proc options (main);
dcl n fixed bin;
n = 32767;
put skip list (n+2);
put skip list (n*2);
end overflow_demo;

View file

@ -0,0 +1,7 @@
local expressions = {
-(-9223372036854775807 - 1),
5000000000000000000 + 5000000000000000000,
-9223372036854775807 - 9223372036854775807
}
for expressions as expr do print(expr) end

View file

@ -0,0 +1,30 @@
try {
# All of these raise an exception, which is caught below.
# The try block is aborted after the first exception,
# so the subsequent lines are never executed.
[int32] (-(-2147483647-1))
[int32] (2000000000 + 2000000000)
[int32] (-2147483647 - 2147483647)
[int32] (46341 * 46341)
[int32] ((-2147483647-1) / -1)
[int64] (-(-9223372036854775807-1))
[int64] (5000000000000000000+5000000000000000000)
[int64] (-9223372036854775807 - 9223372036854775807)
[int64] (3037000500 * 3037000500)
[int64] ((-9223372036854775807-1) / -1)
[uint32] (-4294967295)
[uint32] (3000000000 + 3000000000)
[uint32] (2147483647 - 4294967295)
[uint32] (65537 * 65537)
[uint64] (-18446744073709551615)
[uint64] (10000000000000000000 + 10000000000000000000)
[uint64] (9223372036854775807 - 18446744073709551615)
[uint64] (4294967296 * 4294967296)
}
catch {
$Error.Exception
}

View file

@ -5,11 +5,11 @@ Type "copyright", "credits" or "license()" for more information.
-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)))
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'>

View file

@ -5,11 +5,11 @@ Type "copyright", "credits" or "license()" for more information.
-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)))
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'>

View file

@ -0,0 +1,2 @@
46341*46341L
46341L*46341L

View file

@ -0,0 +1,33 @@
Rebol [
title: "Rosetta code: Integer overflow"
file: %Integer_overflow.r3
url: https://rosettacode.org/wiki/Integer_overflow
]
foreach expr [
;; For 32-bit signed integers:
[negate (-2147483647 - 1) ]
[2000000000 + 2000000000 ]
[-2147483647 - 2147483647 ]
[46341 * 46341 ]
[(-2147483647 - 1) / -1 ]
;; For 64-bit signed integers:
[negate (-9223372036854775807 - 1) ]
[5000000000000000000 + 5000000000000000000 ]
[-9223372036854775807 - 9223372036854775807]
[3037000500 * 3037000500 ]
[(-9223372036854775807 - 1) / -1 ]
;; For 32-bit unsigned integers:
[-4294967295 ]
[3000000000 + 3000000000 ]
[2147483647 - 4294967295 ]
[65537 * 65537 ]
;; For 64-bit unsigned integers:
[transcode "-18446744073709551615" ]
[transcode " 10000000000000000000" ]
[transcode " 18446744073709551615" ]
[4294967296 * 4294967296 ]
][
prin [pad mold/only expr 44 ";== "]
print either error? res: try expr [as-red reform [res/type res/id]][res]
]

View file

@ -0,0 +1,5 @@
var n = integer
n = 32767 rem - maximum integer value
print n+2
print n*2
end

View file

@ -0,0 +1,14 @@
'Binary Integer overflow - vbs
i=(-2147483647-1)/-1
wscript.echo i
i0=32767 '=32767 Integer (Fixed) type=2
i1=2147483647 '=2147483647 Long (Fixed) type=3
i2=-(-2147483647-1) '=2147483648 Double (Float) type=5
wscript.echo Cstr(i0) & " : " & typename(i0) & " , " & vartype(i0) & vbcrlf _
& Cstr(i1) & " : " & typename(i1) & " , " & vartype(i1) & vbcrlf _
& Cstr(i2) & " : " & typename(i2) & " , " & vartype(i2)
ii=2147483648-2147483647
if vartype(ii)<>3 or vartype(ii)<>2 then wscript.echo "Integer overflow type=" & typename(ii)
i1=1000000000000000-1 '1E+15-1
i2=i1+1 '1E+15
wscript.echo Cstr(i1) & " , " & Cstr(i2)

View file

@ -0,0 +1,14 @@
!YS-v0
tests =: |
-9223372036854775807.-- * -1
5000000000000000000 + 5000000000000000000
-9223372036854775807 - 9223372036854775807
3037000500 * 3037000500
each test tests:lines:
say: test
try:
eval: test
catch e:
say: "$e\n"