Hello,
I just want to point out, that there are programming languages, such as
Seed7, that check for integer overflow.
In the discussion "arithmetic overflow revisited" there was an example
with exponentiation. In this example two numbers (a, b) are read and
then the result of a**b is printed. The following Seed7 program does
just this:
$include "seed7_05.s7i";
const proc: main is func
local
var integer: a is 0;
var integer: b is 0;
begin
write("a? "); readln(a);
write("b? "); readln(b);
writeln(a ** b);
end func;
When I start this program (ovf1.sd7) I get:
prompt>s7 ovf1
SEED7 INTERPRETER Version 5.0.9474 Copyright (c) 1990-2018 Thomas Mertes
a? 10
b? 20
*** Uncaught EXCEPTION OVERFLOW_ERROR raised with
{integer: <SYMBOLOBJECT> *NULL_ENTITY_OBJECT* ** integer: <SYMBOLOBJECT> *NULL_ENTITY_OBJECT* }
Stack:
in (val integer: base) ** (val integer: exponent) at /seed7/lib/integer.s7i(172)
in main at ovf1.sd7(10)
-----------------------------
As you can see there is a special EXCEPTION just for integer overflow.
Details about this exception can be found here:
http://seed7.sourceforge.net/manual/errors.htm#OVERFLOW_ERROR
I decided for this solution and against an implicit change to big integers
(with unlimited precision until there is no more memory), because a
solution with an overflow exception can provide more performance.
Reasoning:
OVERFLOW EXCEPTION SWITCH TO BIG INTEGER
Use small (e.g. 64-bit) integer Use a combination of small
representation. (e.g. 64-bit) integer and big
integer representation.
Every time a value is needed:
Check which representation
should be used.
Mixed operations (e.g. small
integer times big integer) can
occur.
Check operations, if they trigger Check small integer operations
integer overflow. In case of if they trigger integer overflow.
overflow throw the exception. In case of overflow switch to
the big integer representation.
When big integer values become
small: Are they switched back to
small integer representation?
CPU instructions can be used to CPU instructions can be used to
do arithmetic operations. do arithmetic operations on the
small integers. The decisision
beween small and big integers
costs extra.
Big integer arithmetic is not Big integer aritmetic is much
used. The programmer needs to more costly than small integer
chose big integer computations operations.
explicit.
The overflow checks can be When mixed cases (plain - big)
implemented as a branch that are all moved to big the cost
is usually not taken. of the overflow checks is the
same as for overflow exceptions.
A compiler can offer the Ommiting the overflow check
possibility to omit all overflow changes the logic of the
checks. Programs that have programming language. Programs
triggered an overflow exception that provided the correct
would return wrong results. The big integer result would return
programmer must be sure that wrong results.
overflow errors will not happen.
Without overflow checks all
arithmetic operations can use
the CPU instructions directly.
An alternate possibility to implement the "SWITCH TO BIG INTEGER"
aproach would be: Use big integers all the time.
In this case you have the (costly) big integer arithmetic all the
time.
In Seed7 you can use the type bigInteger to have big integer
arithmetic (without overflows) all the time.
In Seed7 "small" integers are 64-bit so most programs will not trigger
an overflow.
Regards,
Thomas Mertes
--
Seed7 Homepage:
http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.