S.Ducasse Stéphane Ducasse 1 Numbers
S.Ducasse License: CC-Attribution-ShareAlike
S.Ducasse 3 Arithmetic 5 + 6, 5 - 6, 5 * 6, (division) 30 / 9, (integer division) 30 // 9, (modulo) 30 \\ 9, (square root) 9 sqrt, (square) 3 squared Rounding 3.8 ceiling -> 4, 3.8 floor -> 3, roundTo: > 3.81 Range 30 between: 5 and: 40 Basic 3
S.Ducasse 4 Basic (2) Tests 3.8 isInteger, 3.8 even, 3.8 odd Signs positive, negative, sign, negated Other min:, max:, cos, ln, log, log: arcSin, exp, **
S.Ducasse 5 Smalltalk offers: automatic coercion of numbers infinite number exact precision (7/11) + (4 /11) -> 1 SmallInteger maxValue Biggest small integer Smallest large number: SmallInteger maxValue + 1: Automatic Coercion
S.Ducasse 6 Large Numbers 1000 factorial (try it) factorial /999 factorial -> 1000
S.Ducasse 7 ArithmeticValue>>coerce: aNumber "Answer a number representing the argument, aNumber, that is the same kind of Number as the receiver. Must be defined by all Number classes." ^ self subclassResponsibility ArithmicValue>>generality "Answer the number representing the ordering of the receiver in the generality hierarchy. A number in this hierarchy coerces to numbers higher in hierarchy (i.e., with larger generality numbers)." ^ self subclassResponsibility In VW Coercion & Generality
S.Ducasse 8 In VW Coercion & Generality Integer>>coerce: aNumber "Convert a number to a compatible form aNumber asInteger Integer>>generality ^40 Generality: SmallInt 20, Integer 40, Fraction 60, FixedPoint 70, Float 80, Double 90
S.Ducasse 9 ArithmeticValue>>sumFromInteger: anInteger "The argument anInteger, known to be a kind of integer, encountered a problem on addition. Retry by coercing either anInteger or self, whichever is the less general arithmetic value." ^anInteger retry: #+ coercing: self ArithmeticValue>>retry: aSymbol coercing: aNumber "Arithmetic represented by the symbol, 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. If the generalities are the same, then this message should not have been sent so an error notification is provided." self generality < aNumber generality ifTrue: [^(aNumber coerce: self) perform: aSymbol with: aNumber]. self generality > aNumber generality ifTrue: [^self perform: aSymbol with: (self coerce: aNumber)]. self error: 'coercion attempt failed' #retry:coercing: in VW
S.Ducasse 10 adaptToInteger:... in Squeak Integer>>+ aNumber "Refer to the comment in Number + " aNumber isInteger ifTrue: [self negative == aNumber negative ifTrue: [^ (self digitAdd: aNumber) normalize] ifFalse: [^ self digitSubtract: aNumber]]. ^ aNumber adaptToInteger: self andSend: #+ Number>>adaptToInteger: rcvr andSend: selector "If I am involved in arithmetic with a Integer, convert us and evaluate exprBlock." ^ self subclassResponsibility Float>>adaptToInteger: rcvr andSend: selector "If I am involved in arithmetic with an Integer, convert it to a Float." ^ rcvr asFloat perform: selector with: self Fraction>>adaptToInteger: rcvr andSend: selector "If I am involved in arithmetic with an Integer, convert it to a Fraction." ^ rcvr asFraction perform: selector with: self
S.Ducasse 11 asFloat Float>>asFloat "Answer the receiver itself." ^self Fraction>>asFloat "Answer a Float that closely approximates the value of the receiver. Ideally, answer the Float that most closely approximates the receiver.".... Integer>>asFloat "Answer a Float that represents the value of the receiver. Optimized to process only the significant digits of a LargeInteger”....
S.Ducasse 12 asFraction Fraction>>asFraction "Answer the receiver itself." ^self Integer>>asFraction "Answer a Fraction that represents value of the the receiver." ^Fraction numerator: self denominator: Float>>+ aNumber "Primitive. Answer the sum of the receiver and aNumber. Essential. Fail if the argument is not a Float. See Object documentation whatIsAPrimitive." ^ aNumber adaptToFloat: self andSend: #+
S.Ducasse 13 SmallInteger are objects in Smalltalk No primitives types But heavily optimized by the virtual machine tagged integer for integers and other primitive types one bit indicate that their value is coded differently than normal objects About Pure OO
S.Ducasse 14 Summary Automatic coercion Number kind addition