Search code examples
integerinteger-overfloweiffel

What is the size of an integer in Eiffel? How does it handle overflow?


In Eiffel, what is the size of an INTEGER type?

I was not able to find it, except here, where it is claimed that the size is 32 bits.

In this case, how does Eiffel handle overflow? Is it undefined behavior like in C? Or is there a mechanism to prevent overflow?

If there is any resource where I can find it, then please direct me to it.


Solution

  • According to ECMA standard INTEGER is defined as an alias for one of the sized variants of integer classes with the recommended size of 64 bits. However, because there is a lot of existing code, current implementations still use 32-bit integers for INTEGER. The size can be set by a compiler option (e.g., EiffelStudio uses an Eiffel Configuration File (ECF) to map INTEGER to INTEGER_32, this is done in the core library Base). In principle, a library or an application can specify a different size for INTEGER type using type mapping. The mechanism is not part of Eiffel language specification, therefore usually sized variants are used directly when a specific size is required.

    Eiffel standard does not define precise behavior of basic types, i.e. they are treated like any other types from the language perspective. All implementations, I know of, do not check for integer overflow or underflow and silently wrap the result modulo their size.

    There are several library-based solutions that provide arbitrary precision integer arithmetic:

    • factorial (1994, tested with TowerEiffel 1.3.1) - FACTORIAL represents big integer values
    • big_numbers (1998, tested with SmallEiffel -0.80) - BIG_INTEGER with a set of other numeric classes
    • eapml (Eiffel Arbitrary Precision Mathematics Library) (contemporary, included in standard EiffelStudio distribution under contrib) - INTEGER_X - an arbitrary precision integer
    • dcm (contemporary, included in standard EiffelStudio distribution under contrib) and gobo (contemporary, part of Gobo framework, included in standard EiffelStudio distribution under contrib) - DECIMAL and MA_DECIMAL respectively - decimal numbers following the General Decimal Arithmetic Specification