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:
5 -42 2r100101 16rFA -36RZ
- SmallInteger - machine-integers (31 bits, that is in the range -2^30 - 2^30-1) examples:
SmallInteger maxVal + 1. 1000 factorial negated.
- LargePositiveInteger LargeNegativeInteger - this is numbers numerically too large for SmallInteger (only limited by memory) examples are:
8.0. 13.3. 0.3. 2.5e6. 1.27e-30. 1.27e-31. -12.987654e12. (Float pi) (Float infinity)
- 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:
3 + 5 i. Complex abs: 5 arg: (Float pi / 4)
- 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:
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)
All instances of number supports the following messages:
Apart from complex numbers (which can only be compared for equality), all numbers can be compared with:
< >= = =
Integers also support gcd: factorial and lcd:
- + - * / abs negated reciprocal squared
- 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
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
More advanced functionality
Squeak has - compared to many other languages a very rich array of mathematical functions, also some that are so non-trivial in their implementation that it could be asked if they're too specific - but it's a joy for doing homework-sort of math. There's some more advanced packages for math you can download from SqueakMap, including:
- Cassowary - An incremental constraint solving toolkit that efficiently solves systems of linear equalities and inequalities..
- Graph, Graphs, KGraph - Graph packages
- Logic Expressions Expressions - A model of first order logic expressions.