Previous | Table of Contents | Next |
A fraction object is an exact representation of a rational number. It is characterized by its numerator and denominator. Fraction objects implement the protocol <fraction>:
Protocol <fraction> | Conforms to: <number> |
Messages: | |
denominator ^ <integer> | Returns the fractions denominator |
numerator ^ <integer> | Returns the fractions numerator |
The arithmetic message, when used with fractions or with an integer and a fraction, produces a fraction as a result unless the denominator of the result would be 1. In this case, the result is an integer. Unlike floating-point numbers, calculation using fractions is always exact. Heres an example:
(1/3)+(1/3) (2/3) (1.0/3.0)+(1.0/3.0) 0.666667
A floating-point object is a limited-precision approximation of a real number. Floating-point objects may have limited precision, and they may also have a performance advantage because they are typically implemented using hardware floating-point operations. A standard Smalltalk implementation may provide classes that implement several different precisions for floating-point numbers. All floating-point classes conform to the protocol <float>. <float> defines messages that support the common mathematical functions:
Protocol <float> | Conforms to: <realNumber> |
Messages: | |
arcCos ^ <float> | Inverse cosine of receiver |
arcSin ^ <float> | Inverse sine of receiver |
arcTan ^ <float> | Inverse tangent of receiver |
cos ^ <float> | Cosine in radian of receiver |
degreesToRadians ^ <float> | Convert from degrees to radians |
exp ^ <float> | Exponential function of receiver |
fractionPart ^ <float> | Fractional part of the receiver |
floorLog: <number> ^ <integer> | Largest integer log(receiver) to the argument base |
integerPart ^ <float> | Integer part of the receiver |
ln ^ <float> | Natural logarithm of receiver |
log: <number> ^ <float> | Logarithm to the argument base of the receiver |
radiansToDegrees ^ <float> | Converts from radians to degrees |
raisedTo: <number> ^ <float> | Receiver raised to the argument power |
sin ^ <float> | Sine in radian of receiver |
tan ^ <float> | Tangent of receiver in radians |
Standard Smalltalk allows for an implementation to have up to three different precisions of floating-point number objects. The precision of a floating-point literal is specified by the exponent character in the literal. Valid exponent characters are e, d, and q. Heres an example:
1.234e3 3.1415926535898d0 6.9314718055994530941723212145817657q-1
Standard Smalltalk does not specify the actual precision of floating-point objects, nor does it require that an implementation support multiple floating-point precisions. However, it does specify that if an implementation supports multiple precisions, then the following rule must be followed:
precision(e)<precision(d)<precision(q)
Scaled decimal numbers have arbitrary precision to the left of the decimal point and limited precision to the right of the decimal point. The number of decimal digits to the right of the decimal point is called the scale of the number. Scaled decimal numbers are particularly useful for representing currency amounts:
Protocol <scaledDecimal> | Conforms to: <number> |
Messages: | |
fractionPart ^ <scaledDecimal> | Fractional part of the receiver |
integerPart ^ <scaledDecimal> | Integer part of the receiver |
scale ^ <integer> | Number of digits to the right of the decimal, including trailing 0s |
significantDigits ^ <integer> | Total digits to right and left of decimal point |
Scaled decimal literals are decimal numbers followed by the letter s. The s may be followed by decimal digits that specify an explicit scale for the number. Heres an example:
12s scale is 0 12.34s scale is 2 12.34s4 12.3400 scale is 4
Almost all programs have the need to organize data into aggregates or data structures. Smalltalk provides a rich set of classes, called the collection classes, that support the organization of data. The core collection classes are Array, Bag, ByteArray, Dictionary, Interval, OrderedCollection, Set, SortedCollection, and String. Each of these classes has specific distinguishing characteristics:
Previous | Table of Contents | Next |