--- a/ArithmeticValue.st Thu Dec 07 22:32:49 1995 +0100
+++ b/ArithmeticValue.st Thu Dec 07 22:38:49 1995 +0100
@@ -11,16 +11,15 @@
"
Magnitude subclass:#ArithmeticValue
- instanceVariableNames:''
- classVariableNames:'DivisionByZeroSignal DomainErrorSignal
- OverflowSignal UnderflowSignal
- ArithmeticSignal AnyArithmeticSignal
- UnorderedSignal'
- poolDictionaries:''
- category:'Magnitude-Numbers'
+ instanceVariableNames:''
+ classVariableNames:'DivisionByZeroSignal DomainErrorSignal OverflowSignal
+ UnderflowSignal ArithmeticSignal AnyArithmeticSignal
+ UnorderedSignal'
+ poolDictionaries:''
+ category:'Magnitude-Numbers'
!
-!ArithmeticValue class methodsFor:'documentation' !
+!ArithmeticValue class methodsFor:'documentation'!
copyright
"
@@ -36,10 +35,6 @@
"
!
-version
- ^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.16 1995-11-11 14:26:28 cg Exp $'
-!
-
documentation
"
ArithmeticValue is an abstract superclass for all things responding to
@@ -64,7 +59,7 @@
"
! !
-!ArithmeticValue class methodsFor:'initialization' !
+!ArithmeticValue class methodsFor:'initialization'!
initialize
"setup the signals"
@@ -102,7 +97,27 @@
]
! !
-!ArithmeticValue class methodsFor:'Signal constants' !
+!ArithmeticValue class methodsFor:'Signal constants'!
+
+anyArithmeticSignal
+ "return a signalSet with all possible arithmetic signals.
+ OBSOLETE: this will vanish, since starting with 2.10.3, signal parents
+ have been added."
+
+ ^ AnyArithmeticSignal
+!
+
+arithmeticSignal
+ "return the parent of all arithmetic signals"
+
+ ^ ArithmeticSignal
+!
+
+divisionByZeroSignal
+ "return the signal which is raised on division by zero"
+
+ ^ DivisionByZeroSignal
+!
domainErrorSignal
"return the signal which is raised on math errors
@@ -111,12 +126,6 @@
^ DomainErrorSignal
!
-divisionByZeroSignal
- "return the signal which is raised on division by zero"
-
- ^ DivisionByZeroSignal
-!
-
overflowSignal
"return the signal which is raised on overflow conditions (in floats)"
@@ -134,42 +143,122 @@
for which no ordering is defined (for example: complex numbers)"
^ UnderflowSignal
-!
-
-arithmeticSignal
- "return the parent of all arithmetic signals"
-
- ^ ArithmeticSignal
-!
-
-anyArithmeticSignal
- "return a signalSet with all possible arithmetic signals.
- OBSOLETE: this will vanish, since starting with 2.10.3, signal parents
- have been added."
-
- ^ AnyArithmeticSignal
! !
-!ArithmeticValue methodsFor:'converting' !
+!ArithmeticValue methodsFor:'arithmetic'!
+
+* something
+ "return the product of the receiver and the argument"
+
+ ^ self subclassResponsibility
+!
+
++ something
+ "return the sum of the receiver and the argument"
+
+ ^ self subclassResponsibility
+!
+
+- something
+ "return the difference of the receiver and the argument"
+
+ ^ self subclassResponsibility
+!
+
+/ something
+ "return the quotient of the receiver and the argument"
-degreesToRadians
- "interpreting the receiver as radians, return the degrees"
+ ^ self subclassResponsibility
+!
+
+// something
+ "return the integer quotient of the receiver and the argument"
+
+ ^ (self / something) floor
+!
+
+\\ something
+ "return the integer modulu of the receiver and the argument"
- ^ self asFloat degreesToRadians
+ ^ self - ((self // something) * something)
+!
+
+abs
+ "return the absolute value of the receiver"
+
+ (self negative) ifTrue:[^ self negated].
+ ^ self
+!
+
+negated
+ "return the receiver negated"
+
+ ^ self class zero - self
!
-radiansToDegrees
- "interpreting the receiver as degrees, return the radians"
+quo:something
+ "Return the integer quotient of dividing the receiver by the argument
+ with truncation towards zero."
+
+ ^ (self / something) truncated
+!
+
+reciprocal
+ "return the receivers reciprocal"
+
+ ^ self class unity / self
+!
- ^ self asFloat radiansToDegrees
+rem:something
+ "Return the integer remainder of dividing the receiver by the argument
+ with truncation towards zero.
+ The remainder has the same sign as the receiver."
+
+ ^ self - ((self quo:something) * something)
+! !
+
+!ArithmeticValue methodsFor:'comparing'!
+
+< something
+ "return true, if the argument is greater than the receiver"
+
+ ^ self subclassResponsibility
!
-asInteger
- "return an integer with same value - might truncate"
+<= something
+ "return true, if the argument is greater or equal than the receiver"
+
+ ^ (something < self) not
+!
+
+> something
+ "return true, if the argument is less than the receiver"
+
+ ^ something < self
+!
+
+>= something
+ "return true, if the argument is less or equal than the receiver"
- ^ self truncated
+ ^ (self < something) not
!
+compare:arg ifLess:lessBlock ifEqual:equalBlock ifGreater:greaterBlock
+ "three-way compare - thanks to Self for this idea.
+ Can be redefined in subclasses to do it with a single comparison if
+ comparison is expensive."
+
+ self < arg ifTrue:[
+ ^ lessBlock value
+ ].
+ self = arg ifTrue:[
+ ^ equalBlock value
+ ].
+ ^ greaterBlock value
+! !
+
+!ArithmeticValue methodsFor:'converting'!
+
asFloat
"return a float with same value"
@@ -182,12 +271,24 @@
^ self subclassResponsibility
!
+asInteger
+ "return an integer with same value - might truncate"
+
+ ^ self truncated
+!
+
coerce:aNumber
"convert aNumber into an instance of the receivers class and return it."
^ self subclassResponsibility
!
+degreesToRadians
+ "interpreting the receiver as radians, return the degrees"
+
+ ^ self asFloat degreesToRadians
+!
+
generality
"return a number giving the receivers generality, that number is
used to convert one of the arguments in a mixed expression.
@@ -200,6 +301,12 @@
^ self subclassResponsibility
!
+radiansToDegrees
+ "interpreting the receiver as degrees, return the radians"
+
+ ^ self asFloat radiansToDegrees
+!
+
retry:aSymbol coercing:aNumber
"arithmetic represented by the binary operator, aSymbol,
could not be performed with the receiver and the argument, aNumber,
@@ -232,178 +339,90 @@
self error:'retry:coercing: oops - same generality'
! !
-!ArithmeticValue methodsFor:'queries' !
-
-respondsToArithmetic
- "return true, if the receiver responds to arithmetic messages"
+!ArithmeticValue methodsFor:'double dispatching'!
- ^ true
-! !
-
-!ArithmeticValue methodsFor:'arithmetic' !
+differenceFromFloat:aFloat
+ "the receiver does not know how to subtract from a float -
+ retry the operation by coercing to higher generality"
-+ something
- "return the sum of the receiver and the argument"
-
- ^ self subclassResponsibility
+ ^ aFloat retry:#- coercing:self
!
-- something
- "return the difference of the receiver and the argument"
-
- ^ self subclassResponsibility
-!
+differenceFromFraction:aFraction
+ "the receiver does not know how to subtract from a fraction -
+ retry the operation by coercing to higher generality"
-* something
- "return the product of the receiver and the argument"
-
- ^ self subclassResponsibility
-!
-
-/ something
- "return the quotient of the receiver and the argument"
-
- ^ self subclassResponsibility
+ ^ aFraction retry:#- coercing:self
!
-// something
- "return the integer quotient of the receiver and the argument"
-
- ^ (self / something) floor
-!
-
-\\ something
- "return the integer modulu of the receiver and the argument"
+differenceFromInteger:anInteger
+ "the receiver does not know how to subtract from an integer -
+ retry the operation by coercing to higher generality"
- ^ self - ((self // something) * something)
-!
-
-quo:something
- "Return the integer quotient of dividing the receiver by the argument
- with truncation towards zero."
-
- ^ (self / something) truncated
+ ^ anInteger retry:#- coercing:self
!
-rem:something
- "Return the integer remainder of dividing the receiver by the argument
- with truncation towards zero.
- The remainder has the same sign as the receiver."
+lessFromFloat:aFloat
+ "the receiver does not know how to compare to a float -
+ retry the operation by coercing to higher generality"
- ^ self - ((self quo:something) * something)
+ ^ aFloat retry:#< coercing:self
!
-abs
- "return the absolute value of the receiver"
+lessFromFraction:aFraction
+ "the receiver does not know how to compare to a fraction -
+ retry the operation by coercing to higher generality"
- (self negative) ifTrue:[^ self negated].
- ^ self
-!
-
-negated
- "return the receiver negated"
-
- ^ self class zero - self
+ ^ aFraction retry:#< coercing:self
!
-reciprocal
- "return the receivers reciprocal"
-
- ^ self class unity / self
-! !
+lessFromInteger:anInteger
+ "the receiver does not know how to compare to an integer -
+ retry the operation by coercing to higher generality"
-!ArithmeticValue methodsFor:'comparing'!
-
->= something
- "return true, if the argument is less or equal than the receiver"
-
- ^ (self < something) not
+ ^ anInteger retry:#< coercing:self
!
-> something
- "return true, if the argument is less than the receiver"
+productFromFloat:aFloat
+ "the receiver does not know how to multiply a float -
+ retry the operation by coercing to higher generality"
- ^ something < self
+ ^ aFloat retry:#* coercing:self
!
-<= something
- "return true, if the argument is greater or equal than the receiver"
+productFromFraction:aFraction
+ "the receiver does not know how to multiply a fraction -
+ retry the operation by coercing to higher generality"
- ^ (something < self) not
-!
-
-< something
- "return true, if the argument is greater than the receiver"
-
- ^ self subclassResponsibility
+ ^ aFraction retry:#* coercing:self
!
-compare:arg ifLess:lessBlock ifEqual:equalBlock ifGreater:greaterBlock
- "three-way compare - thanks to Self for this idea.
- Can be redefined in subclasses to do it with a single comparison if
- comparison is expensive."
-
- self < arg ifTrue:[
- ^ lessBlock value
- ].
- self = arg ifTrue:[
- ^ equalBlock value
- ].
- ^ greaterBlock value
-! !
+productFromInteger:anInteger
+ "the receiver does not know how to multiply an integer -
+ retry the operation by coercing to higher generality"
-!ArithmeticValue methodsFor:'truncation & rounding'!
-
-ceiling
- "return the integer nearest the receiver towards positive infinity."
-
- |anInteger|
-
- anInteger := self // 1. "truncates towards negative infinity"
- anInteger = self ifTrue:[^ anInteger].
- ^ anInteger + 1
-!
-
-floor
- "return the receiver truncated towards negative infinity"
-
- ^ self // 1
+ ^ anInteger retry:#* coercing:self
!
-truncated
- "return the receiver truncated towards zero"
+quotientFromFloat:aFloat
+ "the receiver does not know how to divide a float -
+ retry the operation by coercing to higher generality"
- self negative ifTrue:[
- ^ self ceiling
- ].
- ^ self floor
-!
-
-truncateTo:aNumber
- "return the receiver truncated to multiples of aNumber"
-
- ^ ((self / aNumber) floor * aNumber) asInteger
+ ^ aFloat retry:#/ coercing:self
!
-rounded
- "return the integer nearest the receiver"
+quotientFromFraction:aFraction
+ "the receiver does not know how to divide a fraction -
+ retry the operation by coercing to higher generality"
- ^ (self + 0.5) floor
+ ^ aFraction retry:#/ coercing:self
!
-roundTo:aNumber
- "return the receiver rounded to multiples of aNumber"
-
- ^ (self / aNumber) rounded * aNumber
-! !
-
-!ArithmeticValue methodsFor:'double dispatching'!
-
-sumFromInteger:anInteger
- "the receiver does not know how to add an integer -
+quotientFromInteger:anInteger
+ "the receiver does not know how to divide an integer -
retry the operation by coercing to higher generality"
- ^ anInteger retry:#+ coercing:self
+ ^ anInteger retry:#/ coercing:self
!
sumFromFloat:aFloat
@@ -420,104 +439,27 @@
^ aFraction retry:#+ coercing:self
!
-differenceFromInteger:anInteger
- "the receiver does not know how to subtract from an integer -
- retry the operation by coercing to higher generality"
-
- ^ anInteger retry:#- coercing:self
-!
-
-differenceFromFloat:aFloat
- "the receiver does not know how to subtract from a float -
- retry the operation by coercing to higher generality"
-
- ^ aFloat retry:#- coercing:self
-!
-
-differenceFromFraction:aFraction
- "the receiver does not know how to subtract from a fraction -
- retry the operation by coercing to higher generality"
-
- ^ aFraction retry:#- coercing:self
-!
-
-productFromInteger:anInteger
- "the receiver does not know how to multiply an integer -
- retry the operation by coercing to higher generality"
-
- ^ anInteger retry:#* coercing:self
-!
-
-productFromFloat:aFloat
- "the receiver does not know how to multiply a float -
- retry the operation by coercing to higher generality"
-
- ^ aFloat retry:#* coercing:self
-!
-
-productFromFraction:aFraction
- "the receiver does not know how to multiply a fraction -
+sumFromInteger:anInteger
+ "the receiver does not know how to add an integer -
retry the operation by coercing to higher generality"
- ^ aFraction retry:#* coercing:self
-!
-
-quotientFromInteger:anInteger
- "the receiver does not know how to divide an integer -
- retry the operation by coercing to higher generality"
-
- ^ anInteger retry:#/ coercing:self
-!
-
-quotientFromFloat:aFloat
- "the receiver does not know how to divide a float -
- retry the operation by coercing to higher generality"
-
- ^ aFloat retry:#/ coercing:self
-!
-
-quotientFromFraction:aFraction
- "the receiver does not know how to divide a fraction -
- retry the operation by coercing to higher generality"
-
- ^ aFraction retry:#/ coercing:self
-!
-
-lessFromInteger:anInteger
- "the receiver does not know how to compare to an integer -
- retry the operation by coercing to higher generality"
-
- ^ anInteger retry:#< coercing:self
-!
-
-lessFromFloat:aFloat
- "the receiver does not know how to compare to a float -
- retry the operation by coercing to higher generality"
-
- ^ aFloat retry:#< coercing:self
-!
-
-lessFromFraction:aFraction
- "the receiver does not know how to compare to a fraction -
- retry the operation by coercing to higher generality"
-
- ^ aFraction retry:#< coercing:self
+ ^ anInteger retry:#+ coercing:self
! !
!ArithmeticValue methodsFor:'misc math'!
-squared
- "return receiver * receiver"
-
- ^ self * self
-!
-
exp
"return e ^ receiver"
^ self asFloat exp
!
+floorLog:radix
+ "return the logarithm truncated as an integer"
+
+ ^ (self log:radix) floor
+!
+
ln
"return the natural logarithm of the receiver"
@@ -536,18 +478,6 @@
^ self ln / aNumber ln
!
-sqrt
- "return the square root of the receiver"
-
- ^ self asFloat sqrt
-!
-
-floorLog:radix
- "return the logarithm truncated as an integer"
-
- ^ (self log:radix) floor
-!
-
raisedTo:aNumber
"return the receiver raised to aNumber"
@@ -571,28 +501,85 @@
^ 1 / result
].
^ result
+!
+
+sqrt
+ "return the square root of the receiver"
+
+ ^ self asFloat sqrt
+!
+
+squared
+ "return receiver * receiver"
+
+ ^ self * self
+! !
+
+!ArithmeticValue methodsFor:'queries'!
+
+respondsToArithmetic
+ "return true, if the receiver responds to arithmetic messages"
+
+ ^ true
+! !
+
+!ArithmeticValue methodsFor:'testing'!
+
+denominator
+ "return the denominator of the receiver"
+
+ ^ 1
+!
+
+even
+ "return true if the receiver is divisible by 2"
+
+ ^ self truncated asInteger even
+!
+
+negative
+ "return true, if the receiver is < 0"
+
+ " this would lead to infinite recursion ...
+ ^ (self < 0)
+ "
+ ^ self subclassResponsibility
+!
+
+numerator
+ "return the numerator of the receiver."
+
+ ^ self
+!
+
+odd
+ "return true if the receiver is not divisible by 2"
+
+ ^ self even not
+!
+
+positive
+ "return true, if the receiver is >= 0"
+
+ ^ self negative not
+!
+
+sign
+ "return the sign of the receiver"
+
+ (self < 0) ifTrue:[^ -1].
+ (self > 0) ifTrue:[^ 1].
+ ^ 0
+!
+
+strictlyPositive
+ "return true, if the receiver is > 0"
+
+ ^ (self > 0)
! !
!ArithmeticValue methodsFor:'trigonometric'!
-sin
- "return the sine of the receiver (interpreted as radians)"
-
- ^ self asFloat sin
-!
-
-cos
- "return the cosine of the receiver (interpreted as radians)"
-
- ^ self asFloat cos
-!
-
-tan
- "return the tangens of the receiver (interpreted as radians)"
-
- ^ self asFloat tan
-!
-
arcCos
"return the arccosine of the receiver (in radians)"
@@ -609,59 +596,74 @@
"return the arctangens of the receiver (in radians)"
^ self asFloat arcTan
+!
+
+cos
+ "return the cosine of the receiver (interpreted as radians)"
+
+ ^ self asFloat cos
+!
+
+sin
+ "return the sine of the receiver (interpreted as radians)"
+
+ ^ self asFloat sin
+!
+
+tan
+ "return the tangens of the receiver (interpreted as radians)"
+
+ ^ self asFloat tan
! !
-!ArithmeticValue methodsFor:'testing'!
-
-negative
- "return true, if the receiver is < 0"
+!ArithmeticValue methodsFor:'truncation & rounding'!
- " this would lead to infinite recursion ...
- ^ (self < 0)
- "
- ^ self subclassResponsibility
-!
+ceiling
+ "return the integer nearest the receiver towards positive infinity."
-positive
- "return true, if the receiver is >= 0"
+ |anInteger|
- ^ self negative not
-!
-
-strictlyPositive
- "return true, if the receiver is > 0"
-
- ^ (self > 0)
+ anInteger := self // 1. "truncates towards negative infinity"
+ anInteger = self ifTrue:[^ anInteger].
+ ^ anInteger + 1
!
-sign
- "return the sign of the receiver"
+floor
+ "return the receiver truncated towards negative infinity"
- (self < 0) ifTrue:[^ -1].
- (self > 0) ifTrue:[^ 1].
- ^ 0
+ ^ self // 1
!
-even
- "return true if the receiver is divisible by 2"
+roundTo:aNumber
+ "return the receiver rounded to multiples of aNumber"
- ^ self truncated asInteger even
+ ^ (self / aNumber) rounded * aNumber
+!
+
+rounded
+ "return the integer nearest the receiver"
+
+ ^ (self + 0.5) floor
!
-odd
- "return true if the receiver is not divisible by 2"
+truncateTo:aNumber
+ "return the receiver truncated to multiples of aNumber"
- ^ self even not
+ ^ ((self / aNumber) floor * aNumber) asInteger
!
-denominator
- "return the denominator of the receiver"
-
- ^ 1
-!
+truncated
+ "return the receiver truncated towards zero"
-numerator
- "return the numerator of the receiver."
+ self negative ifTrue:[
+ ^ self ceiling
+ ].
+ ^ self floor
+! !
- ^ self
+!ArithmeticValue class methodsFor:'documentation'!
+
+version
+ ^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.17 1995-12-07 21:34:10 cg Exp $'
! !
+ArithmeticValue initialize!