[prev] [up] [next]

Number classes

The number hierarchy provides a common protocol for many numeric types. Smalltalk automatically converts between internal representations and provides unlimited precision integer arithmetic.

The most useful numeric classes are:

Use the systemBrowser to have a more detailed look into the implementation.

Number

Number is the abstract superclass of all number-like objects. It provides methods which are independent of the actual numeric representation.

Smalltalks numeric classes make good use of polymorphism in the language: instances of all numeric classes may be used interchangable in most operations (however, for some, it does not make sense).
Also, results of arithmetic (and other) operations are converted as appropriate. For example, executing:

	1 / 3
(send the message 'divide by 3' to the integer '1') will return a fractional result, represented by an instance of Fraction.

Protocol common to all numbers is:

More details are found in the "Number class documentation".

Integer

Integer is the abstract superclass of all integral number objects. There are no instances of Integer itself, but instead of one of its subclasses, SmallInteger or LargeInteger. Conversion between smallIntegers and largeIntegers is transparent; if some value cannot be represented as smallInteger, the system automatically creates a largeInteger object for it.

More details are found in "Integer's class documentation", in "SmallInteger's class documentation" and in "LargeInteger's class documentation".

Fraction

Fractions are rational numbers, typically resulting from a division of two integers which does not give an integral result. In contrast to floatingPoint arithmetic, fractional arithmetic is exact. Therefore:
    (1 / 3) * 3 = 1 
while the corresponding float operation:
    (1 / 33) asFloat * 33 
may return 0.999999... on some systems due to rounding errors.

Fractional results from arithmetic operations are automatically reduced; therefore,

    (1 / 3) * (1 / 3) * 3 
gives a result of 1/3 (not 3/9).

Beside memory limitations, the precision of fractional numbers is unlimited.

More details are found in the "Fraction class documentation".

Float

Floats are limited precision real numbers. The representation depends on the underlying machines and the C-compilers double implementation.
On most modern systems, the IEEC representation is used (be careful on OpenVMS systems ...).

Typically, the precision of floats is 64bit (i.e. some 15 digits) - but you should not depend on this being true for all implementations. See the C-compilers documentation of your actual system for more information.
A companion class called ShortFloat is also available, which corresponds to the C-compilers float implementation. Typically, this gives you a precision of 32bit (i.e. roughly 6 digits).

Due to historic reasons, the existing smalltalk systems provide different precision and/or names for their float number classes;
The original ST-80 provided a single Float class, which offered 32bit floats.
ObjectWorks added a Double class, providing 64bit of precision.
ST/V's and VisualAge provide 64bit of precision in their Float class.

For portability, ST/X's Float class has 64bit precision, and it provides Double as an alias. Since having more precision does usually no harm to the program, this choice should ease porting of smalltalk code from any system. However, be aware of the fact that a float in ST/X takes up more memory than a float in ST-80.

More details are found in "Float's class documentation" and "ShortFloat's class documentation".

Others

You can extent the Number hierarchy, by adding new classes which implement a certain minimum protocol (they should know how to perform some of the basic arithmetic operations, and how to be converted into other representations).
As an example, see the implementation of a complex class in the file "goodies/Numeric-Complex.st".

Notes:

(*)
One bit is lost for implementation reasons. Therefore, the number of bits in a smallInteger is the machines native integer size minus one; usually 31 (63 on DEC-alpha/osf1).
( for details, see object representation)

Copyright © 1996 Claus Gittinger Development & Consulting

<info@exept.de>

Doc $Revision: 1.22 $ $Date: 2001/08/23 12:12:36 $