diff -r 8a5bcf88a3f0 -r b1103ad3581f ArithmeticValue.st --- a/ArithmeticValue.st Fri Jan 12 22:11:43 2007 +0100 +++ b/ArithmeticValue.st Sat Jan 13 00:18:42 2007 +0100 @@ -46,29 +46,29 @@ variables and signal accessors remain here for backward compatibility. [class variables:] - ArithmeticSignal parent of all arithmetic signals - (never raised itself) - New: now a reference to ArithmeticError + ArithmeticSignal parent of all arithmetic signals + (never raised itself) + New: now a reference to ArithmeticError - DomainErrorSignal raised upon float errors - (for example range in trigonometric) - New: now a reference to DomainError + DomainErrorSignal raised upon float errors + (for example range in trigonometric) + New: now a reference to DomainError - DivisionByZeroSignal raised when division by 0 is attempted - New: now a reference to ZeroDivide + DivisionByZeroSignal raised when division by 0 is attempted + New: now a reference to ZeroDivide - OverflowSignal raised on overflow/underflow conditions - UnderflowSignal in float arithmetic. - Notice: some OperatingSystems do not - provide enough information for ST/X to - extract the real reason for the floatException - thus raising DomainErrorSignal in these cases. + OverflowSignal raised on overflow/underflow conditions + UnderflowSignal in float arithmetic. + Notice: some OperatingSystems do not + provide enough information for ST/X to + extract the real reason for the floatException + thus raising DomainErrorSignal in these cases. [author:] - Claus Gittinger + Claus Gittinger [See also:] - Number + Number " ! ! @@ -88,10 +88,10 @@ " ConversionError handle:[:ex | - Transcript flash + Transcript flash ] do:[ - (0.0 uncheckedDivide:0.0) asFraction + (0.0 uncheckedDivide:0.0) asFraction ] " ! @@ -143,7 +143,7 @@ ! unorderedSignal - "return the signal which is raised when numbers are compared, + "return the signal which is raised when numbers are compared, for which no ordering is defined (for example: complex numbers)" ^ UnorderedNumbersError @@ -255,7 +255,7 @@ "return the receiver modulo something. The remainder has the same sign as something. The following is always true: - (receiver // something) * something + (receiver \\ something) = receiver + (receiver // something) * something + (receiver \\ something) = receiver " ^ self - ((self // something) * something) @@ -277,15 +277,15 @@ ^ self ! -dist:arg +dist:arg "return the distance between arg and the receiver." ^ (arg - self) abs " - (1%1) dist:(0%0) - (1@1) dist:(0@0) - (1) dist:(0) + (1%1) dist:(0%0) + (1@1) dist:(0@0) + (1) dist:(0) " ! @@ -299,7 +299,7 @@ "Return the integer quotient of dividing the receiver by the argument with truncation towards zero. The following is always true: - (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver + (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver " ^ (self / something) truncated @@ -316,7 +316,7 @@ with truncation towards zero. The remainder has the same sign as the receiver. The following is always true: - (receiver quo: something) * something + (receiver rem: something) = receiver + (receiver quo: something) * something + (receiver rem: something) = receiver " ^ self - ((self quo:something) * something) @@ -330,9 +330,9 @@ Its only defined if the arguments type is the same as the receivers." aNumber isZero ifTrue:[ - self isZero ifTrue:[^ self class NaN]. - self negative ifTrue:[^ self class negativeInfinity]. - ^ self class infinity. + self isZero ifTrue:[^ self class NaN]. + self negative ifTrue:[^ self class negativeInfinity]. + ^ self class infinity. ]. ^ self / aNumber ! ! @@ -347,15 +347,15 @@ generality "return a number giving the receivers generality, that number is - used to convert one of the arguments in a mixed expression. + used to convert one of the arguments in a mixed expression. The generality has to be defined in subclasses, - such that gen(a) > gen(b) iff, conversion of b into a's class + such that gen(a) > gen(b) iff, conversion of b into a's class does not cut precision. For example, Integer has 40, Float has 80, meaning that if we convert a Float to an Integer, some precision may be lost. The generality is used by ArithmeticValue>>retry:coercing:, which converts the lower-precision number to the higher precision numbers class, when mixed-type arithmetic is performed." - + ^ self subclassResponsibility "Modified: / 5.11.1996 / 15:05:30 / cg" @@ -364,12 +364,12 @@ retry:aSymbol coercing:aNumber "arithmetic represented by the binary operator, aSymbol, - could not be performed with the receiver and the argument, aNumber, - because of the differences in representation. - Coerce either the receiver or the argument, depending on which has higher - generality, and try again. + could not be performed with the receiver and the argument, aNumber, + because of the differences in representation. + Coerce either the receiver or the argument, depending on which has higher + generality, and try again. If the operation is compare for same value (=), return false if - the argument is not a Number. + the argument is not a Number. If the generalities are the same, create an error message, since this means that a subclass has not been fully implemented." @@ -377,24 +377,23 @@ hasGenerality := aNumber respondsTo:#generality. hasGenerality ifFalse:[ - (aSymbol == #=) ifTrue:[ - ^ false - ]. - (aSymbol == #~=) ifTrue:[ - ^ true - ]. - ^ self error:'retry:coercing: argument is not a number'. + (aSymbol == #=) ifTrue:[ + ^ false + ]. + (aSymbol == #~=) ifTrue:[ + ^ true + ]. + ^ self error:'retry:coercing: argument is not a number'. ]. myGenerality := self generality. otherGenerality := aNumber generality. (myGenerality > otherGenerality) ifTrue:[ - ^ self perform:aSymbol with:(self coerce:aNumber) + ^ self perform:aSymbol with:(self coerce:aNumber) ]. (myGenerality < otherGenerality) ifTrue:[ - ^ (aNumber coerce:self) perform:aSymbol with:aNumber + ^ (aNumber coerce:self) perform:aSymbol with:aNumber ]. -ObjectMemory printStackBacktrace. self error:'retry:coercing: oops - same generality; retry should not happen' "Modified: 5.11.1996 / 15:03:38 / cg" @@ -420,9 +419,9 @@ " 0.3 asFixedPoint 0.5 asFixedPoint - (1/5) asFloat asFixedPoint - (1/3) asFloat asFixedPoint - (2/3) asFloat asFixedPoint + (1/5) asFloat asFixedPoint + (1/3) asFloat asFixedPoint + (2/3) asFloat asFixedPoint (1/8) asFloat asFixedPoint 3.14159 asFixedPoint 0.0000001 asFraction @@ -439,19 +438,19 @@ ^ self asFraction asFixedPoint:scale " - 0.3 asFixedPoint:4 - 0.3 asFixedPoint:3 - 0.3 asFixedPoint:2 - 0.3 asFixedPoint:1 + 0.3 asFixedPoint:4 + 0.3 asFixedPoint:3 + 0.3 asFixedPoint:2 + 0.3 asFixedPoint:1 0.3 asFixedPoint:0 - 0.5 asFixedPoint:3 - (1/5) asFloat asFixedPoint:1 - (1/8) asFloat asFixedPoint:1 - 1.0 asFixedPoint:2 - 3.14159 asFixedPoint:2 - 3.14159 asFixedPoint:3 - (3.14159 asFixedPoint:2) asFixedPoint:5 + 0.5 asFixedPoint:3 + (1/5) asFloat asFixedPoint:1 + (1/8) asFloat asFixedPoint:1 + 1.0 asFixedPoint:2 + 3.14159 asFixedPoint:2 + 3.14159 asFixedPoint:3 + (3.14159 asFixedPoint:2) asFixedPoint:5 " ! @@ -556,7 +555,7 @@ !ArithmeticValue methodsFor:'destructive arithmethic'! *= aNumber - "Return the product of self multiplied by aNumber. + "Return the product of self multiplied by aNumber. The receiver MAY, but NEED NOT be changed to contain the product. So this method must be used as: 'a := a *= 5'. This method can be redefined for constructed datatypes to do optimisations" @@ -568,7 +567,7 @@ ! += aNumber - "Return the sum of self and aNumber. + "Return the sum of self and aNumber. The receiver MAY, but NEED NOT be changed to contain the sum. So this method must be used as: 'a := a += 5'. This method can be redefined for constructed datatypes to do optimisations" @@ -580,7 +579,7 @@ ! -= aNumber - "Return the difference of self and aNumber. + "Return the difference of self and aNumber. The receiver MAY, but NEED NOT be changed to contain the difference. So this method must be used as: 'a := a -= 5'. This method can be redefined for constructed datatypes to do optimisations" @@ -592,7 +591,7 @@ ! /= aNumber - "Return the quotient of self and aNumber. + "Return the quotient of self and aNumber. The receiver MAY, but NEED NOT be changed to contain the quotient. So this method must be used as: 'a := a /= 5'. This method can be redefined for constructed datatypes to do optimisations" @@ -604,7 +603,7 @@ ! div2 - "Return the quotient of self divided by 2. + "Return the quotient of self divided by 2. The receiver MAY, but NEED NOT be changed to contain the result. So this method must be used as: 'a := a div2. This method can be redefined for constructed datatypes to do optimisations" @@ -616,7 +615,7 @@ ! mul2 - "Return the product of self multiplied by 2. + "Return the product of self multiplied by 2. The receiver MAY, but NEED NOT be changed to contain the result. So this method must be used as: 'a := a mul2. This method can be redefined for constructed datatypes to do optimisations" @@ -996,7 +995,7 @@ !ArithmeticValue methodsFor:'mathematical functions'! -** aNumber +** aNumber "Answer the receiver raised to the power of the argument, aNumber." "same as Number>>raisedTo:" @@ -1008,12 +1007,12 @@ raisedTo: aNumber aNumber isInteger ifTrue:[ - ^ self raisedToInteger:aNumber + ^ self raisedToInteger:aNumber ]. ^ self subclassResponsibility ! -raisedToInteger:exp +raisedToInteger:exp "return the receiver raised to exp" |result e t| @@ -1024,22 +1023,22 @@ result := 1. t := self. exp < 0 ifTrue:[ - e := exp negated. + e := exp negated. ] ifFalse:[ - e := exp. + e := exp. ]. [e ~~ 0] whileTrue:[ - [(e bitAnd:1) == 0] whileTrue:[ - e := e bitShift:-1. - t := t * t. - ]. - e := e - 1. - result := result * t. + [(e bitAnd:1) == 0] whileTrue:[ + e := e bitShift:-1. + t := t * t. + ]. + e := e - 1. + result := result * t. ]. (exp < 0) ifTrue:[ - ^ 1 / result + ^ 1 / result ]. ^ result @@ -1047,17 +1046,17 @@ " (2 raisedToInteger:216) - (2 raisedTo:216) --> 105312291668557186697918027683670432318895095400549111254310977536 + (2 raisedTo:216) +-> 105312291668557186697918027683670432318895095400549111254310977536 - (2 raisedToInteger:216) asFloat - (2 raisedTo:216) asFloat + (2 raisedToInteger:216) asFloat + (2 raisedTo:216) asFloat -> 1.05312E+65 (2 raisedToInteger:500) - (2 raisedTo:500) + (2 raisedTo:500) -> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 - 2 raisedToInteger:10 + 2 raisedToInteger:10 -> 1024 -2 raisedToInteger:10 -> 1024 @@ -1065,23 +1064,23 @@ -> -512 10 raisedToInteger:-10 -> (1/10000000000) - 2 raisedToInteger:0 + 2 raisedToInteger:0 -> 1 - 2 raisedToInteger:-1 + 2 raisedToInteger:-1 -> (1/2) Time millisecondsToRun:[ - 10000 timesRepeat:[ - (2 raisedToInteger:500) - ] - ] + 10000 timesRepeat:[ + (2 raisedToInteger:500) + ] + ] Time millisecondsToRun:[ - |bigNum| - bigNum := 2 raisedToInteger:500. - 10 timesRepeat:[ - (bigNum raisedToInteger:500) - ] + |bigNum| + bigNum := 2 raisedToInteger:500. + 10 timesRepeat:[ + (bigNum raisedToInteger:500) + ] ] " @@ -1127,7 +1126,7 @@ ! isFinite - "return true, if the receiver is finite + "return true, if the receiver is finite i.e. it can be represented as a rational number." ^ true @@ -1234,7 +1233,7 @@ ^ (self / aNumber) rounded * aNumber " - 0 roundTo:4 + 0 roundTo:4 1 roundTo:4 2 roundTo:4 3 roundTo:4 @@ -1251,15 +1250,15 @@ ^ (self / aNumber) ceiling * aNumber " - 0 roundUpTo:4 - 1 roundUpTo:4 - 2 roundUpTo:4 - 3 roundUpTo:4 - 4 roundUpTo:4 - 5 roundUpTo:4 - 6 roundUpTo:4 - 7 roundUpTo:4 - 8 roundUpTo:4 + 0 roundUpTo:4 + 1 roundUpTo:4 + 2 roundUpTo:4 + 3 roundUpTo:4 + 4 roundUpTo:4 + 5 roundUpTo:4 + 6 roundUpTo:4 + 7 roundUpTo:4 + 8 roundUpTo:4 " ! @@ -1267,7 +1266,7 @@ "return the integer nearest the receiver" self negative ifTrue:[ - ^ (self - 0.5) ceiling + ^ (self - 0.5) ceiling ]. ^ (self + 0.5) floor @@ -1292,7 +1291,7 @@ !ArithmeticValue class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.75 2007-01-12 21:11:28 cg Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.76 2007-01-12 23:18:42 cg Exp $' ! ! ArithmeticValue initialize!