|
|
6.104 Number
- Defined in namespace Smalltalk
- Category: Language-Data types
- I am an abstract class that provides operations on numbers, both floating
point and integer. I provide some generic predicates, and supply the
implicit type coercing code for binary operations.
6.104.1 Number class: converting
- coerce: aNumber
- Answer aNumber - whatever class it belongs to, it is good
- readFrom: aStream
- Answer the number read from the rest of aStream, converted to an instance of the receiver. If the receiver is number, the class of the result is undefined -- but the result is good.
6.104.2 Number class: testing
- isImmediate
- Answer whether, if x is an instance of the receiver, x copy == x
6.104.3 Number: arithmetic
- * aNumber
- Subtract the receiver and aNumber, answer the result
- + aNumber
- Sum the receiver and aNumber, answer the result
- - aNumber
- Subtract aNumber from the receiver, answer the result
- / aNumber
- Divide the receiver by aNumber, answer the result (no loss of precision)
- // aNumber
- Divide the receiver by aNumber, answer the result truncated towards -infinity
- \\ aNumber
- Divide the receiver by aNumber truncating towards -infinity, answer the remainder
- quo: aNumber
- Return the integer quotient of dividing the receiver by aNumber with truncation towards zero.
- reciprocal
- Return the reciprocal of the receiver
- rem: aNumber
- Return the remainder of dividing the receiver by aNumber with truncation towards zero.
6.104.4 Number: converting
- asFloat
- This method's functionality should be implemented by subclasses of Number
- asFloatD
- This is mandated by the ANSI standard; since GNU Smalltalk currently does not support different floating-point classes, simply convert the receiver to a Float.
- asFloatE
- This is mandated by the ANSI standard; since GNU Smalltalk currently does not support different floating-point classes, simply convert the receiver to a Float.
- asFloatQ
- This is mandated by the ANSI standard; since GNU Smalltalk currently does not support different floating-point classes, simply convert the receiver to a Float.
- asRectangle
- Answer an empty rectangle whose origin is (self asPoint)
- asScaledDecimal: n
- Answer the receiver, converted to a ScaledDecimal object.
- coerce: aNumber
- Answer aNumber - whatever class it belongs to, it is good
- degreesToRadians
- Convert the receiver to radians
- generality
- Answer the receiver's generality
- radiansToDegrees
- Convert the receiver from radians to degrees
- unity
- Coerce 1 to the receiver's class. The default implementation works, but is inefficient
- zero
- Coerce 0 to the receiver's class. The default implementation works, but is inefficient
6.104.5 Number: copying
- deepCopy
- Return the receiver - it's an immediate (immutable) object
- shallowCopy
- Return the receiver - it's an immediate (immutable) object
6.104.6 Number: error raising
- arithmeticError: msg
- Raise an ArithmeticError exception having msg as its message text.
- zeroDivide
- Raise a division-by-zero (ZeroDivide) exception whose dividend is the receiver.
6.104.7 Number: Intervals & iterators
- to: stop
- Return an interval going from the receiver to stop by 1
- to: stop by: step
- Return an interval going from the receiver to stop with the given step
- to: stop by: step do: aBlock
- Evaluate aBlock for each value in the interval going from the receiver to stop with the given step. Compiled in-line for integer literal steps, and for one-argument aBlocks without temporaries, and therefore not overridable.
- to: stop do: aBlock
- Evaluate aBlock for each value in the interval going from the receiver to stop by 1. Compiled in-line for one-argument aBlocks without temporaries, and therefore not overridable.
6.104.8 Number: misc math
- abs
- Answer the absolute value of the receiver
- arcCos
- return the arc cosine of the receiver
- arcSin
- return the arc sine of the receiver
- arcTan
- return the arc tangent of the receiver
- cos
- return the cosine of the receiver
- estimatedLog
- Answer an estimate of (self abs floorLog: 10). This method should be overridden by subclasses, but Number's implementation does not raise errors - simply, it gives a correct result, so it is slow.
- exp
- return e raised to the receiver
- floorLog: radix
- return (self log: radix) floor. Optimized to answer an integer.
- ln
- return log base e of the receiver
- log
- return log base aNumber of the receiver
- log: aNumber
- return log base aNumber of the receiver
- negated
- Answer the negated of the receiver
- raisedTo: aNumber
- Return self raised to aNumber power
- raisedToInteger: anInteger
- Return self raised to the anInteger-th power
- sin
- return the sine of the receiver
- sqrt
- return the square root of the receiver
- squared
- Answer the square of the receiver
- tan
- return the tangent of the receiver
6.104.9 Number: point creation
- @ y
- Answer a new point whose x is the receiver and whose y is y
- asPoint
- Answer a new point, self @ self
6.104.10 Number: retrying
- retry: aSymbol coercing: aNumber
- Coerce to the other number's class the one number between the receiver and aNumber which has the lowest, and retry calling aSymbol. aSymbol is supposed not to be #= or #~= (since those don't fail if aNumber is not a Number).
- retryDifferenceCoercing: aNumber
- Coerce to the other number's class the one number between the receiver and aNumber which has the lowest, and retry calling #-.
- retryDivisionCoercing: aNumber
- Coerce to the other number's class the one number between the receiver and aNumber which has the lowest, and retry calling #/.
- retryEqualityCoercing: aNumber
- Coerce to the other number's class the one number between the receiver and aNumber which has the lowest, and retry calling #=.
- retryError
- Raise an error--a retrying method was called with two arguments having the same generality.
- retryInequalityCoercing: aNumber
- Coerce to the other number's class the one number between the receiver and aNumber which has the lowest, and retry calling #~=.
- retryMultiplicationCoercing: aNumber
- Coerce to the other number's class the one number between the receiver and aNumber which has the lowest, and retry calling #*.
- retryRelationalOp: aSymbol coercing: aNumber
- Coerce to the other number's class the one number between the receiver and aNumber which has the lowest, and retry calling aSymbol (<, <=, >, >=).
- retrySumCoercing: aNumber
- Coerce to the other number's class the one number between the receiver and aNumber which has the lowest, and retry calling #+.
6.104.11 Number: testing
- closeTo: num
- Answer whether the receiver can be considered sufficiently close to num (this is done by checking equality if num is not a number, and by checking with 0.01% tolerance if num is a number).
- even
- Returns true if self is divisible by 2
- isNumber
- Answer `true'.
- isRational
- Answer whether the receiver is rational - false by default
- negative
- Answer whether the receiver is < 0
- odd
- Returns true if self is not divisible by 2
- positive
- Answer whether the receiver is >= 0
- sign
- Returns the sign of the receiver.
- strictlyPositive
- Answer whether the receiver is > 0
6.104.12 Number: truncation and round off
- asInteger
- Answer the receiver, rounded to the nearest integer
- floor
- Return the integer nearest the receiver toward negative infinity.
- fractionPart
- Answer a number which, summed to the #integerPart of the receiver, gives the receiver itself.
- integerPart
- Answer the receiver, truncated towards zero
- rounded
- Returns the integer nearest the receiver
- roundTo: aNumber
- Answer the receiver, truncated to the nearest multiple of aNumber
- truncated
- Answer the receiver, truncated towards zero
- truncateTo: aNumber
- Answer the receiver, truncated towards zero to a multiple of aNumber
|