Back: NullValueHolder-accessing Up: Base classes Forward: Number class-converting   Top: GNU Smalltalk Library Reference Contents: Table of Contents Index: Class index About: About this document

1.116 Number

Defined in namespace Smalltalk
Superclass: Magnitude
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.

1.116.1 Number class: converting  (class)
1.116.2 Number class: testing  (class)
1.116.3 Number: arithmetic  (instance)
1.116.4 Number: coercion  (instance)
1.116.5 Number: comparing  (instance)
1.116.6 Number: converting  (instance)
1.116.7 Number: copying  (instance)
1.116.8 Number: error raising  (instance)
1.116.9 Number: misc math  (instance)
1.116.10 Number: point creation  (instance)
1.116.11 Number: retrying  (instance)
1.116.12 Number: shortcuts and iterators  (instance)
1.116.13 Number: testing  (instance)
1.116.14 Number: truncation and round off  (instance)


1.116.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.


1.116.2 Number class: testing

isImmediate
Answer whether, if x is an instance of the receiver, x copy == x


1.116.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). Raise a ZeroDivide exception or return a valid (possibly infinite) continuation value if aNumber is zero.

// aNumber
Return the integer quotient of dividing the receiver by aNumber with truncation towards negative infinity. Raise a ZeroDivide exception if aNumber is zero

\\ aNumber
Return the remainder of dividing the receiver by aNumber with truncation towards negative infinity. Raise a ZeroDivide exception if aNumber is zero

quo: aNumber
Return the integer quotient of dividing the receiver by aNumber with truncation towards zero. Raise a ZeroDivide exception if aNumber is zero

reciprocal
Return the reciprocal of the receiver

rem: aNumber
Return the remainder of dividing the receiver by aNumber with truncation towards zero. Raise a ZeroDivide exception if aNumber is zero


1.116.4 Number: coercion

asCNumber
Convert the receiver to a kind of number that is understood by the C call-out mechanism.


1.116.5 Number: comparing

max: aNumber
Answer the maximum between the receiver and aNumber. Redefine in subclasses if necessary to ensure that if either self or aNumber is a NaN, it is always answered.

min: aNumber
Answer the minimum between the receiver and aNumber. Redefine in subclasses if necessary to ensure that if either self or aNumber is a NaN, it is always answered.


1.116.6 Number: converting

asFloat
Convert the receiver to an arbitrary subclass of Float

asFloatD
This method's functionality should be implemented by subclasses of Number

asFloatE
This method's functionality should be implemented by subclasses of Number

asFloatQ
This method's functionality should be implemented by subclasses of Number

asNumber
Answer the receiver, since it is already a number

asRectangle
Answer an empty rectangle whose origin is (self asPoint)

asScaledDecimal: n
Answer the receiver, converted to a ScaledDecimal object.

asScaledDecimal: denDigits radix: base scale: n
Answer the receiver, divided by base^denDigits and converted to a ScaledDecimal object.

coerce: aNumber
Answer aNumber, converted to an integer or floating-point number.

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


1.116.7 Number: copying

deepCopy
Return the receiver - it's an immediate (immutable) object

shallowCopy
Return the receiver - it's an immediate (immutable) object


1.116.8 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.


1.116.9 Number: misc math

abs
Answer the absolute value of the receiver

arcCos
Answer the arc cosine of the receiver

arcCosh
Answer the hyperbolic arc-cosine of the receiver.

arcSin
Answer the arc sine of the receiver

arcSinh
Answer the hyperbolic arc-sine of the receiver.

arcTan
Answer the arc tangent of the receiver

arcTan: x
Answer the angle (measured counterclockwise) between (x, self) and a ray starting in (0, 0) and moving towards (1, 0) - i.e. 3 o'clock

arcTanh
Answer the hyperbolic arc-tangent of the receiver.

ceilingLog: radix
Answer (self log: radix) ceiling. Optimized to answer an integer.

cos
Answer the cosine of the receiver

cosh
Answer the hyperbolic 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
Answer e raised to the receiver

floorLog: radix
Answer (self log: radix) floor. Optimized to answer an integer.

ln
Answer log base e of the receiver

log
Answer log base 10 of the receiver

log: aNumber
Answer log base aNumber of the receiver

negated
Answer the negated of the receiver

positiveDifference: aNumber
Answer the positive difference of the receiver and aNumber, that is self - aNumber if it is positive, 0 otherwise.

raisedTo: aNumber
Return self raised to aNumber power

raisedToInteger: anInteger
Return self raised to the anInteger-th power

sin
Answer the sine of the receiver

sinh
Answer the hyperbolic sine of the receiver.

sqrt
Answer the square root of the receiver

squared
Answer the square of the receiver

tan
Answer the tangent of the receiver

tanh
Answer the hyperbolic tangent of the receiver.

withSignOf: aNumber
Answer the receiver, with its sign possibly changed to match that of aNumber.


1.116.10 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


1.116.11 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 #+.


1.116.12 Number: shortcuts and 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 collect: aBlock
Evaluate aBlock for each value in the interval going from the receiver to stop with the given step. The results are collected in an Array and returned.

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 collect: aBlock
Evaluate aBlock for each value in the interval going from the receiver to stop by 1. The results are collected in an Array and returned.

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.


1.116.13 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

isFinite
Answer whether the receiver represents a finite quantity. Most numeric classes are for finite quantities, so the default is to answer true rather than calling #subclassResponsibility.

isInfinite
Answer whether the receiver represents an infinite quantity. Most numeric classes are for finite quantities, so the default is to answer false rather than calling #subclassResponsibility.

isNaN
Answer whether the receiver is a Not-A-Number. Most numeric classes don't handle nans, so the default is to answer false rather than calling #subclassResponsibility.

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


1.116.14 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

roundTo: aNumber
Answer the receiver, truncated to the nearest multiple of aNumber

rounded
Returns the integer nearest the receiver

truncateTo: aNumber
Answer the receiver, truncated towards zero to a multiple of aNumber

truncated
Answer the receiver, truncated towards zero



Back: Number-testing Up: Number Forward: Object   Top: GNU Smalltalk Library Reference Contents: Table of Contents Index: Class index About: About this document


This document was generated on May, 22 2008 using texi2html