  links to this page:    Squeak in a Nutshell Swiki Page List         Math
Last updated at 4:31 pm UTC on 11 November 2006
Squeak is very well capable of doing advanced math, dynamic typing is essential for making easy coercions between different nummeric type, the biggest flaw is in speed where it cannot compete with machine-code-compiled languages as pascal, c, c++ and fortran - but for far the most purposes squeak on a modern machine will do the trick.
When you're doing math in squeak, then remember that the precedence rules you know from your math-lessons isn't obeyed by squeak (binary messages are send from left to right): 3+2 * 5 returns 25, NOT 15 - so put in parantheses whenever in doubt.
This is a quick review of what is possible, browse the classes for further methods and information, theres plenty.

### Different nummeric classes

Squeak comes with support for:
• SmallInteger - machine-integers (31 bits, that is in the range -2^30 - 2^30-1) examples:
5 -42 2r100101 16rFA -36RZ
• LargePositiveInteger LargeNegativeInteger - this is numbers numerically too large for SmallInteger (only limited by memory) examples are:
SmallInteger maxVal + 1. 1000 factorial negated.
• Float - IEEE-754 floating-point double-precision numbers. They have about 16 digits of accuracy and their range is between plus and minus 10^307. Some valid examples are:
8.0. 13.3. 0.3. 2.5e6. 1.27e-30. 1.27e-31. -12.987654e12. (Float pi) (Float infinity)
• Fraction - dealing with proportions between integers, makes possible to have excact representations of eg. 1/3.
• Complex number support has been added with 3.8, a complex number is created from a non-complex by the method 'i', or with the constructors real:imaginary: and/or abs:arg: examples:
3 + 5 i. Complex abs: 5 arg: (Float pi / 4)

You can also do some arithmetic on points (as 2d- and 3d-vectors), they are created like this -2@5. 4.23e6@44.
(also see literals)

### Mathematical functions

All instances of number supports the following messages:

## Comparison

Apart from complex numbers (which can only be compared for equality), all numbers can be compared with:
< >= = =

## Arithmetic:

• + - * / abs negated reciprocal squared
Integers also support gcd: factorial and lcd:

## Trig:

• sin cos tan - Std. geometric functions works in radians.
• degreeSin degreeCos degreeTan - The same in degrees.
• sinh cosh - hyperbole versions for Complex numbers.
• arcSin arcCos arcTan - the inverse trig funtions

## Powers and Logarithms

• raisedTo: - the receiver raised to the argument
• raisedToInteger: - a lot faster.
• sqrt returns the squareroot of the receiver
• exp - e raised to the receiver power
• ln - base e logarithm
• log - base 10 logarithm
• log: - logarithm to any base

## Random numbers

The random-number-generator is a Park-Miller generator, it is implemented in the class Random.
• 5 atRandom - returns a Integer between 1 (inclusive) and 5 inclusive
• #(1 4 2) atRandom - returns a random element from the Array (or any other Collection)
• #(1 2 3) shuffle - permutates the OrderedCollection