Vision Class: Number
Overview
Numbers are objects that represent numerical values and respond to messages that compute mathematical results. The literal representation of a number is a sequence of digits that may be preceded by a minus sign and/or followed by a decimal point and another sequence of digits.
The classes Integer, Double, and Float are subclasses of the class Number. The class Number defines the general protocol for all numeric classes. Most numbers are either integers or doubles. The Float class is used for efficiency in certain cases. All arithmetic operations between numbers return instances of the class Double. It is not meaningful to create new instances of the Number classes.
The Number class is a direct subclass of the class Ordinal:
Object | Ordinal | Number | |-- Double | |-- Float | |-- Integer
Computation Messages
Many messages defined for the class Number and its subclasses provide a way to perform standard mathematical computations.
The following messages perform the basic arithmetic operations. These messages can be sent to an instance of any Number subclass and return an instance of the Double class. The parameter can be an instance of any Number subclass.
Message | Definition | Sample |
+ | Add parameter to recipient | 2 + 3 |
- | Subtract parameter from recipient | 3 - 2 |
* | Multiply recipient by parameter | 3 * 2 |
/ | Divide recipient by parameter | 3 / 2 |
toThe: | Raise recipient to the parameter power | 2 toThe: 3 . |
The following messages perform the basic relational operations. These messages can be sent to an instance of any Number subclass and return an instance of the Boolean class. Parameters are an instance of any Number subclass except where explicitly noted.
Message | Definition | Sample |
= | Is recipient equal to parameter? | 2 = 2.0 |
== | Is recipient identical to parameter? | 2 == 2.0 |
!= | Is recipient not equal to parameter? | 2 != 2.0 |
!== | Is recpient not identical to parameter? | 2 !== 2.0 |
< | Is recpient less than parameter? | 1 < 2.0 |
<= | Is recpient less than or equal to parameter? | 1 <= 2.0 |
> | Is recpient greater than parameter? | 1 > 2.0 |
>= | Is recpient greater than or equal to parameter? | 1 >= 2.0 |
between:and: | Is recpient value between two parameter values? | 5 between: 3 and: 7 . |
inRange: | Is recpient value in the range implied by the parameter, a list of two elements representing the start and end of the range inclusively? | 5 inRange: 3,7 . |
inSet: | Is recpient equal to one of the values in the parameter, a list containing one or more elements? | 5.0 inSet: 1,3,5,7,9 . |
notBetween:and: | Is recpient value not between two parameter values? | 5 notBetween: 3 and: 7 . |
within:percentOf: | Is recpient value within parameter1 percent of parameter2? | 100 within: 10 percentOf: 105 . |
The following messages perform other standard computations. These messages can be sent to an instance of any Number subclass.
Message | Definition | Sample |
absoluteValue | Absolute value of recipient | -2 absoluteValue |
asAPowerOf: | Raise parameter to the recipient power | 3 asAPowerOf: 2 . |
exp | Raise the constant e (~ 2.7183) to the recipient power | 3 exp |
log | Compute natural log of recipient | 3 log |
log: | Compute log of recipient using numeric parameter as base | 3 log: 10 . |
max: | Returns the larger of recipient and parameter value | 3 max: 4 . |
min: | Returns the smaller of recipient and parameter value | 3 min: 4 . |
negated | Multiplies recipient by -1 | 3 negated |
pctChangeTo: | Computes percentage change between recipient and parameter relative to parameter | 3 pctChangeTo: 2 . |
round | Round recipient to nearest integer | 3.3 round |
round: | Round recipient to parameter number of decimals places | 3.333 round: 2 . |
sign | Returns -1, 0, or 1 based on sign of recipient | 3 sign |
sqrt | Square root of recipient | 3 sqrt |
The following messages perform other standard computations. These messages are only defined for Integers.
Message | Definition | Sample |
adjustedMod: | Recipient modulo parameter, returning an integer between 1 and parameter | 10 adjustedMod: 5 . |
mod: | Recipient modulo parameter, returning an integer between 0 and parameter - 1 | 10 mod: 5 . |
numberOfDigits | Integer number of digits in recipient | 3 numberOfDigits |
The following messages perform the basic trigonometric operations. These messages can be sent to an instance of any Number subclass and return an instance of the class Double.
Message | Sample |
cosine, sine, tangent | 0 cosine |
arcCosine, arcSine, arcTangent | 1 arcCosine |
hyperbolicCosine, hyperbolicSine, hyperbolicTangent | 0 hyperbolicCosine |
Conversion Messages
Several messages have been defined to convert an instance of Number to an instance of another class.
The following messages convert from one numeric class to another. These messages can be sent to an instance of any Number subclass.
Message | Definition | Sample |
asDouble | Convert recipient to a double | 3 asDouble |
asFloat | Convert recipient to a float | 3 asFloat |
asInteger | Convert recipient to an integer | 3.0 asInteger |
The following messages convert numbers to instances of other classes. These messages can be sent to an instance of any Number subclass.
Message | Definition | Sample |
asDate | Convert recipient in form YYYYMMDD to a Date object | 19960315 asDate |
asString | Convert recipient to a String object | 123 asString |
sequence | Create list of recipient elements from 1 to recipient | 10 sequence |
sequence0 | Create list of recipient elements from 0 to recipient - 1 | 10 sequence0 |