Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Integer_overflow

View file

@ -0,0 +1,111 @@
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.
<br>The integers supported by such a type can be &nbsp; ''signed'' &nbsp; or &nbsp; ''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.
;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
!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
|}
;Notes:
:* &nbsp; When the integer overflow does trigger an exception show how the exception is caught.
:* &nbsp; When the integer overflow produces some value, &nbsp; print it.
:* &nbsp; It should be explicitly noted when an integer overflow is not recognized, &nbsp; the program continues with wrong results.
:* &nbsp; This should be done for signed and unsigned integers of various sizes supported by the computer programming language.
:* &nbsp; When a language has no fixed size integer type, &nbsp; or when no integer overflow can occur for other reasons, &nbsp; this should be noted.
:* &nbsp; It is okay to mention, &nbsp; when a language supports unlimited precision integers, &nbsp; but this task is NOT the place to demonstrate the <br>&nbsp; capabilities of unlimited precision integers.
<br><br>

View file

@ -0,0 +1,6 @@
L 2,=F'2147483647' 2**31-1
L 3,=F'1' 1
AR 2,3 add register3 to register2
BO OVERFLOW branch on overflow
....
OVERFLOW EQU *

View file

@ -0,0 +1,6 @@
IPM 1 Insert Program Mask
O 1,BITFPO unmask Fixed Overflow
SPM 1 Set Program Mask
...
DS 0F alignment
BITFPO DC BL1'00001000' bit20=1 [start at 16]

View file

@ -0,0 +1,4 @@
LDA #$7F
CLC
ADC #$01
BVS ErrorHandler ;this branch will always be taken.

View file

@ -0,0 +1,4 @@
LDA #$FF
CLC
ADC #$01
BCS ErrorHandler ;this branch will always be taken.

View file

@ -0,0 +1,3 @@
LDX #$7F
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.

View file

@ -0,0 +1,3 @@
LDA #%01000000
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.

View file

@ -0,0 +1,4 @@
LDX #$FF
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!
BEQ OverflowOccurred ;notice that we used BEQ here and not BCS.

View file

@ -0,0 +1,16 @@
;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.
;add the low bytes
LDA $10 ;low byte of first operand
CLC
ADC $12 ;low byte of second operand
STA $14 ;low byte of sum
;add the high bytes
LDA $11 ;high byte of first operand
ADC $13 ;high byte of second operand
STA $15 ;high byte of result
BVS HandleOverflow ;only check for overflow when adding the most significant bytes.

View file

@ -0,0 +1,4 @@
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
SUB.B #1,D0 ;WILL SET THE OVERFLOW FLAG SINCE AT BYTE LENGTH WE CROSSED FROM 80 TO 7F

View file

@ -0,0 +1,4 @@
BEGIN
print (max int);
print (1+max int)
END

View file

@ -0,0 +1,4 @@
BEGIN
print (long max int);
print (1+ long max int)
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 @@
A% = -(-32767-1)

View file

@ -0,0 +1 @@
A% = 20000 + 20000

View file

@ -0,0 +1 @@
A% = -32767 -32767

View file

@ -0,0 +1 @@
A% = 182 * 182

View file

@ -0,0 +1 @@
A% = -32767 : POKE PEEK(131) + PEEK(132) * 256, 0 : ? A%

View file

@ -0,0 +1,11 @@
big32bit: 2147483646
big64bit: 9223372036854775808
print type big32bit
print type big64bit
print big32bit + 1
print big64bit + 1
print big32bit * 2
print big64bit * 2

View 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

View file

@ -0,0 +1,4 @@
Disp -65535▶Dec,i
Disp 40000+40000▶Dec,i
Disp 32767-65535▶Dec,i
Disp 257*257▶Dec,i

View file

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

View file

@ -0,0 +1,35 @@
#include <iostream>
#include <cstdint>
#include <limits>
int main (int argc, char *argv[])
{
std::cout << std::boolalpha
<< std::numeric_limits<std::int32_t>::is_modulo << '\n'
<< std::numeric_limits<std::uint32_t>::is_modulo << '\n' // always true
<< std::numeric_limits<std::int64_t>::is_modulo << '\n'
<< std::numeric_limits<std::uint64_t>::is_modulo << '\n' // always true
<< "Signed 32-bit:\n"
<< -(-2147483647-1) << '\n'
<< 2000000000 + 2000000000 << '\n'
<< -2147483647 - 2147483647 << '\n'
<< 46341 * 46341 << '\n'
<< (-2147483647-1) / -1 << '\n'
<< "Signed 64-bit:\n"
<< -(-9223372036854775807-1) << '\n'
<< 5000000000000000000+5000000000000000000 << '\n'
<< -9223372036854775807 - 9223372036854775807 << '\n'
<< 3037000500 * 3037000500 << '\n'
<< (-9223372036854775807-1) / -1 << '\n'
<< "Unsigned 32-bit:\n"
<< -4294967295U << '\n'
<< 3000000000U + 3000000000U << '\n'
<< 2147483647U - 4294967295U << '\n'
<< 65537U * 65537U << '\n'
<< "Unsigned 64-bit:\n"
<< -18446744073709551615LU << '\n'
<< 10000000000000000000LU + 10000000000000000000LU << '\n'
<< 9223372036854775807LU - 18446744073709551615LU << '\n'
<< 4294967296LU * 4294967296LU << '\n';
return 0;
}

View 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!");
}
}
}

View 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;
}

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,4 @@
(* -1 (dec -9223372036854775807))
(+ 5000000000000000000 5000000000000000000)
(- -9223372036854775807 9223372036854775807)
(* 3037000500 3037000500)

View 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

View file

@ -0,0 +1,7 @@
LDA ff
ADD one
...
ff: 255
one: 1

View 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);
}

View file

@ -0,0 +1,123 @@
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 ===================================}
procedure TestSigned32_1;
begin
IS32:=-(-2147483647-1);
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestSigned32_2;
begin
IS32:=2000000000 + 2000000000;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestSigned32_3;
begin
IS32:=-2147483647 - 2147483647;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestSigned32_4;
begin
IS32:=46341 * 46341;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestSigned32_5;
begin
IS32:=(-2147483647-1) div -1;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
{============ Signed 64 bit tests ===================================}
procedure TestSigned64_1;
begin
IS64:=-(-9223372036854775807-1);
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestSigned64_2;
begin
IS64:=5000000000000000000+5000000000000000000;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestSigned64_3;
begin
IS64:=-9223372036854775807 - 9223372036854775807;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestSigned64_4;
begin
IS64:=3037000500 * 3037000500;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestSigned64_5;
begin
IS64:=(-9223372036854775807-1) div -1;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
{============ UnSigned 32 bit tests ===================================}
procedure TestUnSigned32_1;
begin
IU32:=-4294967295;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestUnSigned32_2;
begin
IU32:=3000000000 + 3000000000;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestUnSigned32_3;
begin
IU32:=2147483647 - 4294967295;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
procedure TestUnSigned32_4;
begin
IU32:=65537 * 65537;
end;
// Compiler: "Overflow in conversion or arithmetic operation"
//Delphi-6 does not have 64-bit unsigned integers.
//Later version have 64-bit unsigned integers.

View file

@ -0,0 +1 @@
#include <stdio.h>

View file

@ -0,0 +1,48 @@
' FB 1.05.0 Win64
' The suffixes L, LL, UL and ULL are added to the numbers to make it
' clear to the compiler that they are to be treated as:
' signed 4 byte, signed 8 byte, unsigned 4 byte and unsigned 8 byte
' integers, respectively.
' Integer types in FB are freely convertible to each other.
' In general if the result of a computation would otherwise overflow
' it is converted to a higher integer type.
' Consequently, although the calculations are the same as the C example,
' the results for the 32-bit integers are arithmetically correct (and different
' therefore from the C results) because they are converted to 8 byte integers.
' However, as 8 byte integers are the largest integral type, no higher conversions are
' possible and so the results 'wrap round'. The 64-bit results are therefore the
' same as the C examples except the one where the compiler warns that there is an overflow
' which, frankly, I don't understand.
Print "Signed 32-bit:"
Print -(-2147483647L-1L)
Print 2000000000L + 2000000000L
Print -2147483647L - 2147483647L
Print 46341L * 46341L
Print (-2147483647L-1L) \ -1L
Print
Print "Signed 64-bit:"
Print -(-9223372036854775807LL-1LL)
Print 5000000000000000000LL + 5000000000000000000LL
Print -9223372036854775807LL - 9223372036854775807LL
Print 3037000500LL * 3037000500LL
Print (-9223372036854775807LL - 1LL) \ -1LL ' compiler warning : Overflow in constant conversion
Print
Print "Unsigned 32-bit:"
Print -4294967295UL
Print 3000000000UL + 3000000000UL
Print 2147483647UL - 4294967295UL
Print 65537UL * 65537UL
Print
Print "Unsigned 64-bit:"
Print -18446744073709551615ULL ' compiler warning : Implicit conversion
Print 10000000000000000000ULL + 10000000000000000000ULL
Print 9223372036854775807ULL - 18446744073709551615ULL
Print 4294967296ULL * 4294967296ULL
Print
Print "Press any key to quit"
Sleep

View 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)
}

View file

@ -0,0 +1,61 @@
println "\nSigned 32-bit (failed):"
assert -(-2147483647-1) != 2147483648g
println(-(-2147483647-1))
assert 2000000000 + 2000000000 != 4000000000g
println(2000000000 + 2000000000)
assert -2147483647 - 2147483647 != -4294967294g
println(-2147483647 - 2147483647)
assert 46341 * 46341 != 2147488281g
println(46341 * 46341)
//Groovy converts divisor and dividend of "/" to floating point. Use "intdiv" to remain integral
//assert (-2147483647-1) / -1 != 2147483648g
assert (-2147483647-1).intdiv(-1) != 2147483648g
println((-2147483647-1).intdiv(-1))
println "\nSigned 64-bit (passed):"
assert -(-2147483647L-1) == 2147483648g
println(-(-2147483647L-1))
assert 2000000000L + 2000000000L == 4000000000g
println(2000000000L + 2000000000L)
assert -2147483647L - 2147483647L == -4294967294g
println(-2147483647L - 2147483647L)
assert 46341L * 46341L == 2147488281g
println(46341L * 46341L)
assert (-2147483647L-1).intdiv(-1) == 2147483648g
println((-2147483647L-1).intdiv(-1))
println "\nSigned 64-bit (failed):"
assert -(-9223372036854775807L-1) != 9223372036854775808g
println(-(-9223372036854775807L-1))
assert 5000000000000000000L+5000000000000000000L != 10000000000000000000g
println(5000000000000000000L+5000000000000000000L)
assert -9223372036854775807L - 9223372036854775807L != -18446744073709551614g
println(-9223372036854775807L - 9223372036854775807L)
assert 3037000500L * 3037000500L != 9223372037000250000g
println(3037000500L * 3037000500L)
//Groovy converts divisor and dividend of "/" to floating point. Use "intdiv" to remain integral
//assert (-9223372036854775807L-1) / -1 != 9223372036854775808g
assert (-9223372036854775807L-1).intdiv(-1) != 9223372036854775808g
println((-9223372036854775807L-1).intdiv(-1))
println "\nSigned unbounded (passed):"
assert -(-2147483647g-1g) == 2147483648g
println(-(-2147483647g-1g))
assert 2000000000g + 2000000000g == 4000000000g
println(2000000000g + 2000000000g)
assert -2147483647g - 2147483647g == -4294967294g
println(-2147483647g - 2147483647g)
assert 46341g * 46341g == 2147488281g
println(46341g * 46341g)
assert (-2147483647g-1g).intdiv(-1) == 2147483648g
println((-2147483647g-1g).intdiv(-1))
assert -(-9223372036854775807g-1) == 9223372036854775808g
println(-(-9223372036854775807g-1))
assert 5000000000000000000g+5000000000000000000g == 10000000000000000000g
println(5000000000000000000g+5000000000000000000g)
assert -9223372036854775807g - 9223372036854775807g == -18446744073709551614g
println(-9223372036854775807g - 9223372036854775807g)
assert 3037000500g * 3037000500g == 9223372037000250000g
println(3037000500g * 3037000500g)
assert (-9223372036854775807g-1g).intdiv(-1) == 9223372036854775808g
println((-9223372036854775807g-1g).intdiv(-1))

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

View 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

View 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

View 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

View 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

View file

@ -0,0 +1,16 @@
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);
}
}

View file

@ -0,0 +1,39 @@
def compare:
if type == "string" then "\n\(.)\n"
else map(tostring)
| .[1] as $s
| .[0]
| if $s == . then . + ": agrees"
else $s + ": expression evaluates to " + .
end
end;
[ -(-2147483647-1),"2147483648"],
[2000000000 + 2000000000, "4000000000"],
[-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) / -1, "9223372036854775808"],
"For 32-bit unsigned integers:",
[-4294967295, "-4294967295"],
[3000000000 + 3000000000, "6000000000"],
[2147483647 - 4294967295, "-2147483648"],
[65537 * 65537, "4295098369"],
"For 64-bit unsigned integers:",
[-18446744073709551615, "-18446744073709551615"],
[10000000000000000000 + 10000000000000000000, "20000000000000000000"],
[9223372036854775807 - 18446744073709551615, "-9223372036854775808"],
[4294967296 * 4294967296, "18446744073709551616"]
| compare

View file

@ -0,0 +1,9 @@
using Printf
S = subtypes(Signed)
U = subtypes(Unsigned)
println("Integer limits:")
for (s, u) in zip(S, U)
@printf("%8s: [%s, %s]\n", s, typemin(s), typemax(s))
@printf("%8s: [%s, %s]\n", u, typemin(u), typemax(u))
end

View file

@ -0,0 +1,5 @@
println("Add one to typemax:")
for t in S
over = typemax(t) + one(t)
@printf("%8s → %-25s (%s)\n", t, over, typeof(over))
end

View file

@ -0,0 +1,29 @@
// The Kotlin compiler can detect expressions of signed constant integers that will overflow.
// It cannot detect unsigned integer overflow, however.
@Suppress("INTEGER_OVERFLOW")
fun main() {
println("*** Signed 32 bit integers ***\n")
println(-(-2147483647 - 1))
println(2000000000 + 2000000000)
println(-2147483647 - 2147483647)
println(46341 * 46341)
println((-2147483647 - 1) / -1)
println("\n*** Signed 64 bit integers ***\n")
println(-(-9223372036854775807 - 1))
println(5000000000000000000 + 5000000000000000000)
println(-9223372036854775807 - 9223372036854775807)
println(3037000500 * 3037000500)
println((-9223372036854775807 - 1) / -1)
println("\n*** Unsigned 32 bit integers ***\n")
// println(-4294967295U) // this is a compiler error since unsigned integers have no negation operator
// println(0U - 4294967295U) // this works
println((-4294967295).toUInt()) // converting from the signed Int type also produces the overflow; this is intended behavior of toUInt()
println(3000000000U + 3000000000U)
println(2147483647U - 4294967295U)
println(65537U * 65537U)
println("\n*** Unsigned 64 bit integers ***\n")
println(0U - 18446744073709551615U) // we cannot convert from a signed type here (since none big enough exists) and have to use subtraction
println(10000000000000000000U + 10000000000000000000U)
println(9223372036854775807U - 18446744073709551615U)
println(4294967296U * 4294967296U)
}

View file

@ -0,0 +1,22 @@
#!/bin/ksh
# Integer overflow
# # Variables:
#
typeset -si SHORT_INT
typeset -i INTEGER
typeset -li LONG_INT
######
# main #
######
(( SHORT_INT = 2**15 -1 )) ; print "SHORT_INT (2^15 -1) = $SHORT_INT"
(( SHORT_INT = 2**15 )) ; print "SHORT_INT (2^15) : $SHORT_INT"
(( INTEGER = 2**31 -1 )) ; print " INTEGER (2^31 -1) = $INTEGER"
(( INTEGER = 2**31 )) ; print " INTEGER (2^31) : $INTEGER"
(( LONG_INT = 2**63 -1 )) ; print " LONG_INT (2^63 -1) = $LONG_INT"
(( LONG_INT = 2**63 )) ; print " LONG_INT (2^63) : $LONG_INT"

View file

@ -0,0 +1 @@
#include <stdio.h>

View file

@ -0,0 +1,14 @@
put -(-2147483647-1)
-- -2147483648
put 2000000000 + 2000000000
-- -294967296
put -2147483647 - 2147483647
-- 2
put 46341 * 46341
-- -2147479015
put (-2147483647-1) / -1
--> crashes Director (jeez!)

View file

@ -0,0 +1,5 @@
assert(math.type~=nil, "Lua 5.3+ required for this test.")
minint, maxint = math.mininteger, math.maxinteger
print("min, max int64 = " .. minint .. ", " .. 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))

View file

@ -0,0 +1,37 @@
Long A
Try ok {
A=12121221212121
}
If not ok then Print Error$ 'Overflow Long
Def Integer B
Try ok {
B=1212121212
}
If not ok then Print Error$ ' Overflow Integer
Def Currency C
Try ok {
C=121212121232934392898274327927948
}
If not ok then Print Error$ ' return Overflow Long, but is overflow Currency
Def Decimal D
Try ok {
D=121212121232934392898274327927948
}
If not ok then Print Error$ ' return Overflow Long, but is overflow Decimal
\\ No overflow for unsigned numbers in structs
Structure Struct {
\\ union a1, a2| b
{
a1 as integer
a2 as integer
}
b as long
}
\\ structures are type for Memory Block, or other sttructure
\\ we use Clear to erase internal Memory Block
Buffer Clear DataMem as Struct*20
\\ from a1 we get only the low word
Return DataMem, 0!a2:=0xBBBB, 0!a1:=0xFFFFAAAA
Print Hex$(Eval(DataMem, 0!b))="BBBBAAAA"
Print Eval(DataMem, 0!b)=Eval(DataMem, 0!a2)*0x10000+Eval(DataMem, 0!a1)

View file

@ -0,0 +1,2 @@
$MaxNumber +
10^-15.954589770191003298111788092733772206160314 $MaxNumber

View file

@ -0,0 +1,6 @@
try:
var x: int32 = -2147483647
x = -(x - 1) # Raise overflow.
echo x
except OverflowDefect:
echo "Overflow detected"

View file

@ -0,0 +1,8 @@
{.push overflowChecks: off.}
try:
var x: int32 = -2147483647
x = -(x - 1)
echo x # -2147483648 — Wrong result as 2147483648 doesn't fit in an int32.
except OverflowDefect:
echo "Overflow detected" # Not executed.
{.pop.}

View file

@ -0,0 +1,52 @@
echo "For 32 bits signed integers with overflow check suppressed:"
{.push overflowChecks: off.}
var a: int32
a = -(-2147483647i32 - 1'i32)
echo " -(-2147483647-1) gives ", a # -2147483648.
a = 2000000000i32 + 2000000000i32
echo " 2000000000 + 2000000000 gives ", a # -294967296.
a = -2147483647i32 - 2147483647i32
echo " -2147483647 - 2147483647 gives ", a # 2.
a = 46341i32 * 46341i32
echo " 46341 * 46341 gives ", a # -2147479015.
a = (-2147483647i32 - 1i32) div -1i32
echo " (-2147483647-1) / -1 gives ", a # -2147483648.
{.pop.}
echo ""
echo "For 64 bits signed integers with overflow check suppressed:"
{.push overflowChecks: off.}
var b: int64
b = -(-9223372036854775807i64 - 1i64)
echo " -(-9223372036854775807-1) gives ", b # -9223372036854775808.
b = 5000000000000000000i64 + 5000000000000000000i64
echo " 5000000000000000000 + 5000000000000000000 gives ", b # -8446744073709551616.
b = -9223372036854775807i64 - 9223372036854775807i64
echo " -9223372036854775807 - 9223372036854775807 gives ", b # 2.
b = 3037000500i64 * 3037000500i64
echo " 3037000500 * 3037000500 gives ", b # -9223372036709301616.
b = (-9223372036854775807i64 - 1i64) div -1i64
echo " (-9223372036854775807-1) / -1 gives ", b # -9223372036854775808.
{.pop.}
echo ""
echo "For 32 bits unsigned integers:"
var c: uint32
echo " -4294967295 doesnt compile."
c = 3000000000u32 + 3000000000u32
echo " 3000000000 + 3000000000 gives ", c # 1705032704.
c = 2147483647u32 - 4294967295u32
echo " 2147483647 - 4294967295 gives ", c # 2147483648.
c = 65537u32 * 65537u32
echo " 65537 * 65537 gives ", c # 131073.
echo ""
echo "For 64 bits unsigned integers:"
var d: uint64
echo " -18446744073709551615 doesnt compile."
d = 10000000000000000000u64 + 10000000000000000000u64
echo " 10000000000000000000 + 10000000000000000000 gives ", d # 1553255926290448384.
d = 9223372036854775807u64 - 18446744073709551615u64
echo " 9223372036854775807 - 18446744073709551615 gives ", d # 9223372036854775808.
d = 4294967296u64 * 4294967296u64
echo " 4294967296 * 4294967296 gives ", d # 0.

View file

@ -0,0 +1,2 @@
Vecsmall([1])
Vecsmall([2^64])

View file

@ -0,0 +1,43 @@
100H: /* SHOW INTEGER OVERFLOW */
/* CP/M SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
/* CONSOLE I/O ROUTINES */
PRCHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PRSTRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PRNL: PROCEDURE; CALL PRCHAR( 0DH ); CALL PRCHAR( 0AH ); END;
PRNUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
N$STR( W := LAST( N$STR ) ) = '$';
N$STR( W := W - 1 ) = '0' + ( ( V := N ) MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PRSTRING( .N$STR( W ) );
END PRNUMBER;
/* TASK */
/* THE ONLY TYPES SUPPORTED BY THE ORIGINAL PL/M COMPILER ARE */
/* UNSIGED, BYTE IS 8 BITS AND ADDRESS IS 16 BITS */
DECLARE SV BYTE, LV ADDRESS;
SV = 255; /* MAXIMUM BYTE VALUE */
LV = 65535; /* MAXIMUM ADDRESS VALUE */
CALL PRSTRING( .'8-BIT: $' );
CALL PRNUMBER( SV );
CALL PRSTRING( .' INCREMENTS TO: $' );
SV = SV + 1;
CALL PRNUMBER( SV );
CALL PRNL;
CALL PRSTRING( .'16-BIT: $' );
CALL PRNUMBER( LV );
CALL PRSTRING( .' INCREMENTS TO: $' );
LV = LV + 1;
CALL PRNUMBER( LV );
CALL PRNL;
EOF

View file

@ -0,0 +1 @@
#include <stdio.h>

View 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);

View file

@ -0,0 +1,3 @@
-->
<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>
<!--

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

@ -0,0 +1,56 @@
#MAX_BYTE =127
#MAX_ASCII=255 ;=MAX_CHAR Ascii-Mode
CompilerIf #PB_Compiler_Unicode=1
#MAX_CHAR =65535 ;Unicode-Mode
CompilerElse
#MAX_CHAR =255
CompilerEndIf
#MAX_WORD =32767
#MAX_UNIC =65535
#MAX_LONG =2147483647
CompilerIf #PB_Compiler_Processor=#PB_Processor_x86
#MAX_INT =2147483647 ;32-bit CPU
CompilerElseIf #PB_Compiler_Processor=#PB_Processor_x64
#MAX_INT =9223372036854775807 ;64-bit CPU
CompilerEndIf
#MAX_QUAD =9223372036854775807
Macro say(Type,maxv,minv,sz)
PrintN(Type+#TAB$+RSet(Str(minv),30,Chr(32))+#TAB$+RSet(Str(maxv),30,Chr(32))+#TAB$+RSet(Str(sz),6,Chr(32))+" Byte")
EndMacro
OpenConsole()
PrintN("TYPE"+#TAB$+RSet("MIN",30,Chr(32))+#TAB$+RSet("MAX",30,Chr(32))+#TAB$+RSet("SIZE",6,Chr(32)))
Define.b b1=#MAX_BYTE, b2=b1+1
say("Byte",b1,b2,SizeOf(b1))
Define.a a1=#MAX_ASCII, a2=a1+1
say("Ascii",a1,a2,SizeOf(a1))
Define.c c1=#MAX_CHAR, c2=c1+1
say("Char",c1,c2,SizeOf(c1))
Define.w w1=#MAX_WORD, w2=w1+1
say("Word",w1,w2,SizeOf(w1))
Define.u u1=#MAX_UNIC, u2=u1+1
say("Unicode",u1,u2,SizeOf(u1))
Define.l l1=#MAX_LONG, l2=l1+1
say("Long ",l1,l2,SizeOf(l1))
Define.i i1=#MAX_INT, i2=i1+1
say("Int",i1,i2,SizeOf(i1))
Define.q q1=#MAX_QUAD, q2=q1+1
say("Quad",q1,q2,SizeOf(q1))
Input()

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

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

View file

@ -0,0 +1,16 @@
/*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 )
call showResult( -999999998 - 2 )
call showResult( 40000 * 25000 )
call showResult( -50000 * 20000 )
call showResult( 50000 *-30000 )
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. */

View file

@ -0,0 +1,20 @@
#lang racket
(require racket/unsafe/ops)
(fixnum? -1073741824) ;==> #t
(fixnum? (- -1073741824)) ;==> #f
(- -1073741824) ;==> 1073741824
(unsafe-fx- 0 -1073741824) ;==> -1073741824
(+ 1000000000 1000000000) ;==> 2000000000
(unsafe-fx+ 1000000000 1000000000) ;==> -147483648
(- -1073741823 1073741823) ;==> -2147483646
(unsafe-fx- -1073741823 1073741823) ;==> 2
(* 46341 46341) ;==> 2147488281
(unsafe-fx* 46341 46341) ;==> 4633
(/ -1073741824 -1) ;==> 1073741824
(unsafe-fxquotient -1073741824 -1) ;==> -1073741824

View file

@ -0,0 +1,2 @@
my int64 ($a, $b, $c) = 9223372036854775807, 5000000000000000000, 3037000500;
.say for -(-$a - 1), $b + $b, -$a - $a, $c * $c, (-$a - 1)/-1;

View 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

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

View 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

View 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

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

View file

@ -0,0 +1,2 @@
var (a, b, c) = (9223372036854775807, 5000000000000000000, 3037000500);
[-(-a - 1), b + b, -a - a, c * c, (-a - 1)/-1].each { say _ };

View file

@ -0,0 +1,5 @@
2147483647 + 1. -> 2147483648
2147483647 add_32: 1 -> -2147483648
4294967295 + 1. -> 4294967296
16rFFFFFFFF add_32u: 1. -> 0
... simular stuff for sub32/mul32 ...

View file

@ -0,0 +1,10 @@
~(~9223372036854775807-1) ;
poly: : error: Overflow exception raised while converting ~9223372036854775807 to int
Int.maxInt ;
val it = SOME 4611686018427387903: int option
~(~4611686018427387903 - 1);
Exception- Overflow raised
(~4611686018427387903 - 1) div ~1;
Exception- Overflow raised
2147483648 * 2147483648 ;
Exception- Overflow raised

View file

@ -0,0 +1,52 @@
// 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
var int32:Int32
var int64:Int64
var uInt32:UInt32
var uInt64:UInt64
println("signed 32-bit int:")
int32 = -1 &* (-2147483647 - 1)
println(int32)
int32 = 2000000000 &+ 2000000000
println(int32)
int32 = -2147483647 &- 2147483647
println(int32)
int32 = 46341 &* 46341
println(int32)
int32 = (-2147483647-1) &/ -1
println(int32)
println()
println("signed 64-bit int:")
int64 = -1 &* (-9223372036854775807 - 1)
println(int64)
int64 = 5000000000000000000&+5000000000000000000
println(int64)
int64 = -9223372036854775807 &- 9223372036854775807
println(int64)
int64 = 3037000500 &* 3037000500
println(int64)
int64 = (-9223372036854775807-1) &/ -1
println(int64)
println()
println("unsigned 32-bit int:")
println("-4294967295 is caught as a compile time error")
uInt32 = 3000000000 &+ 3000000000
println(uInt32)
uInt32 = 2147483647 &- 4294967295
println(uInt32)
uInt32 = 65537 &* 65537
println(uInt32)
println()
println("unsigned 64-bit int:")
println("-18446744073709551615 is caught as a compile time error")
uInt64 = 10000000000000000000 &+ 10000000000000000000
println(uInt64)
uInt64 = 9223372036854775807 &- 18446744073709551615
println(uInt64)
uInt64 = 4294967296 &* 4294967296
println(uInt64)

View file

@ -0,0 +1,4 @@
proc tcl::mathfunc::clamp32 {x} {
expr {$x<0 ? -((-$x) & 0x7fffffff) : $x & 0x7fffffff}
}
puts [expr { clamp32(2000000000 + 2000000000) }]; # ==> 1852516352

View file

@ -0,0 +1,13 @@
PRINT "Signed 32-bit:"
PRINT -(-2147483647-1) !-2147483648
PRINT 2000000000 + 2000000000 !4000000000
PRINT -2147483647 - 2147483647 !-4294967294
PRINT 46341 * 46341 !2147488281
!PRINT (-2147483647-1) / -1 !error: Illegal expression
WHEN ERROR IN
PRINT maxnum * 2 !Run-time error "Overflow"
USE
PRINT maxnum
!returns the largest number that can be represented in your computer
END WHEN
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 @@
Dim i As Integer '32-bit signed integer

View file

@ -0,0 +1 @@
Dim i As ULong '64-bit unsigned integer

View file

@ -0,0 +1,3 @@
i = -18446744073709551615
i = 10000000000000000000 + 10000000000000000000
i = 9223372036854775807 - 18446744073709551615

View file

@ -0,0 +1 @@
i = 4294967296 * 4294967296

View file

@ -0,0 +1 @@
i = 4294967296 : i = i * i

View file

@ -0,0 +1,7 @@
Dim i As Integer '32-bit signed integer
Try
i = -2147483647 : i = -(i - 1)
Debug.Print(i)
Catch ex As Exception
Debug.Print("Exception raised : " & ex.Message)
End Try

View file

@ -0,0 +1,9 @@
i = -(-2147483647 - 1)
i = 0 - (-2147483647 - 1)
i = -(-2147483647L - 1)
i = -(-2147483647 - 2)
i = 2147483647 + 1
i = 2000000000 + 2000000000
i = -2147483647 - 2147483647
i = 46341 * 46341
i = (-2147483647 - 1) / -1

View file

@ -0,0 +1,2 @@
i = -Int(-2147483647 - 1)
i = -2147483647: i = -(i - 1)

View file

@ -0,0 +1 @@
Dim i As UInteger '32-bit unsigned integer

View file

@ -0,0 +1,4 @@
i = -4294967295
i = 3000000000 + 3000000000
i = 2147483647 - 4294967295
i = 65537 * 65537

View file

@ -0,0 +1 @@
i = 3000000000 : i = i + i

View file

@ -0,0 +1 @@
Dim i As Long '64-bit signed integer

View file

@ -0,0 +1,5 @@
i = -(-9223372036854775807 - 1)
i = 5000000000000000000 + 5000000000000000000
i = -9223372036854775807 - 9223372036854775807
i = 3037000500 * 3037000500
i = (-9223372036854775807 - 1) / -1

View file

@ -0,0 +1 @@
i = -9223372036854775807 : i = -(i - 1)

View file

@ -0,0 +1,12 @@
'Binary Integer overflow - vb6 - 28/02/2017
Dim i As Long '32-bit signed integer
i = -(-2147483647 - 1) '=-2147483648 ?! bug
i = -Int(-2147483647 - 1) '=-2147483648 ?! bug
i = 0 - (-2147483647 - 1) 'Run-time error '6' : Overflow
i = -2147483647: i = -(i - 1) 'Run-time error '6' : Overflow
i = -(-2147483647 - 2) 'Run-time error '6' : Overflow
i = 2147483647 + 1 'Run-time error '6' : Overflow
i = 2000000000 + 2000000000 'Run-time error '6' : Overflow
i = -2147483647 - 2147483647 'Run-time error '6' : Overflow
i = 46341 * 46341 'Run-time error '6' : Overflow
i = (-2147483647 - 1) / -1 'Run-time error '6' : Overflow

View file

@ -0,0 +1,4 @@
i=0
On Error Resume Next
i = 2147483647 + 1
Debug.Print i 'i=0

View file

@ -0,0 +1,6 @@
i=0
On Error GoTo overflow
i = 2147483647 + 1
...
overflow:
Debug.Print "Error: " & Err.Description '-> Error: Overflow

View file

@ -0,0 +1,3 @@
On Error GoTo 0
i = 2147483647 + 1 'Run-time error '6' : Overflow
Debug.Print i

View file

@ -0,0 +1,3 @@
var exprs = [-4294967295, 3000000000 + 3000000000, 2147483647 - 4294967295, 65537 * 65537]
System.print("Unsigned 32-bit:")
for (expr in exprs) System.print(expr >> 0)

View file

@ -0,0 +1,12 @@
int N;
[N:= -(-2147483647-1);
IntOut(0, N); CrLf(0);
N:= 2000000000 + 2000000000;
IntOut(0, N); CrLf(0);
N:= -2147483647 - 2147483647;
IntOut(0, N); CrLf(0);
N:= 46341 * 46341;
IntOut(0, N); CrLf(0);
N:= (-2147483647-1)/-1;
IntOut(0, N); CrLf(0);
]

Some files were not shown because too many files have changed in this diff Show more