Last updated at 9:03 am UTC on 28 October 2018
Class Number holds the most general methods for dealing with numbers.
Subclasses Float, Fraction, and Integer, and their subclasses, provide concrete representations of a numeric quantity.
Some common methods
- arithmetic selectors:
- + - / *
- // (integer quotient) \\ (modulo)
- abs, negated, quo:, reciprocal, rem: See quo vs. //
- mathematical selectors
- trigonometric selectors
- sin, cos, tan
- arcSin, arcCos, arcTan
- Truncation and rounding
- ceiling, floor
- roundTo:, roundUpTo:, rounded,
- truncateTo:, truncated
- odd, even, positive, negative, sign
- isNumber, isNaN, isZero, isDivisibleBy:
- asNumber, asPoint,
- degreesToRadians, radiansToDegrees
- @ (special primative for points 1 @ 1)
- Intervals (see Interval class)
- to:, to: by:, to: by: do:, to: do:
- printOn:, printStringBase:
All of Number's subclasses participate in a simple type coercion mechanism that supports mixed-mode arithmetic and comparisons. It works as follows: If
self op: arg
fails because of incompatible types, then it is retried in the following guise:
(arg adaptTypeA: self) op: arg adaptToTypeA.
This gives the arg of typeB an opportunity to resolve the incompatibility, knowing exactly what two types are involved. If self is more general, then arg will be converted, and viceVersa. This mechanism is extensible to any new number classes that one might wish to add to Squeak. The only requirement is that every subclass of Number must support a pair of conversion methods specific to each of the other subclasses of Number.
Prime numbers demo