Numbers Stphane Ducasse Stephane Ducasseunivsavoie fr http www
Numbers Stéphane Ducasse Stephane. Ducasse@univ-savoie. fr http: //www. listic. univ-savoie. fr/~ducasse/ S. Ducasse 1
License: CC-Attribution-Share. Alike 2. 0 http: //creativecommons. org/licenses/by-sa/2. 0/ S. Ducasse 2
Basic • • • S. Ducasse 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, 3. 811 round. To: 0. 01 -> 3. 81 Range • 30 between: 5 and: 40 3
Basic (2) • • • S. Ducasse Tests • 3. 8 is. Integer, 3. 8 even, 3. 8 odd Signs • positive, negative, sign, negated Other • min: , max: , cos, ln, log: arc. Sin, exp, ** 4
Automatic Coercion • • Smalltalk offers: • • • automatic coercion of numbers infinite number exact precision (7/11) + (4 /11) -> 1 • Small. Integer max. Value • • Smallest large number: • S. Ducasse Biggest small integer Small. Integer max. Value + 1: 5
Large Numbers • 1000 factorial (try it) 40238726007709377354370243392300398571937486421071463254379991042993851239862902059204420848696940480047998861 019719605863166687299480855890132382966994459099742450408707375991882362772718873251977950595099527612087 497546249704360141827809464649629105639388743788648733711918104582578364784997701247663288983595573543251 318532395846307555740911426241747434934755342864657661166779739666882029120737914385371958824980812686783 837455973174613608537953452422158659320192809087829730843139284440328123155861103697680135730421616874760 967587134831202547858932076716913244842623613141250878020800026168315102734182797770478463586817016436502 415369139828126481021309276124489635992870511496497541990934222156683257208082133318611681155361583654698 404670897560290095053761647584772842188967964624494516076535340819890138544248798495995331910172335555660 213945039973628075013783761530712776192684903435262520001588853514733161170210396817592151090778801939317 811419454525722386554146106289218796022383897147608850627686296714667469756291123408243920816015378088989 396451826324367161676217916890977991190375403127462228998800519544441428201218736174599264295658174662830 295557029902432415318161721046583203678690611726015878352075151628422554026517048330422614397428693306169 089796848259012545832716822645806652676995865268227280707578139185817888965220816434834482599326604336766 017699961283186078838615027946595513115655203609398818061213855860030143569452722420634463179746059468257 310379008402443243846565724501440282188525247093519062092902313649327349756551395872055965422874977401141 334696271542284586237738753823048386568897646192738381490014076731044664025989949022222176590433990188601 856652648506179970235619389701786004081188972991831102117122984590164192106888438712185564612496079872290 851929681937238864261483965738229112312502418664935314397013742853192664987533721894069428143411852015801 412334482801505139969429015348307764456909907315243327828826986460278986432113908350621709500259738986355 427719674282224875758676575234422020757363056949882508796892816275384886339690995982628095612145099487170 124451646126037902930912088908694202851064018215439945715680594187274899809425474217358240106367740459574 178516082923013535808184009699637252423056085590370062427124341690900415369010593398383577793941097002775 347200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 • 1000 factorial /999 factorial • -> 1000 S. Ducasse 6
In VW Coercion & Generality • • Arithmetic. Value>>coerce: a. Number "Answer a number representing the argument, a. Number, that is the same kind of Number as the receiver. Must be defined by all Number classes. " ^ self subclass. Responsibility • • Arithmic. Value>>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 subclass. Responsibility S. Ducasse 7
In VW Coercion & Generality • • • Integer>>coerce: a. Number "Convert a number to a compatible form a. Number as. Integer • Integer>>generality ^40 • Generality: Small. Int 20, Integer 40, Fraction 60, Fixed. Point 70, Float 80, Double 90 S. Ducasse 8
#retry: coercing: in VW Arithmetic. Value>>sum. From. Integer: an. Integer "The argument an. Integer, known to be a kind of integer, encountered a problem on addition. Retry by coercing either an. Integer or self, whichever is the less general arithmetic value. " ^an. Integer retry: #+ coercing: self Arithmetic. Value>>retry: a. Symbol coercing: a. Number "Arithmetic represented by the symbol, a. Symbol, could not be performed with the receiver and the argument, a. Number, 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 < a. Number generality if. True: [^(a. Number coerce: self) perform: a. Symbol with: a. Number]. self generality > a. Number generality if. True: [^self perform: a. Symbol with: (self coerce: a. Number)]. S. Ducasse self error: 'coercion attempt failed' 9
adapt. To. Integer: . . . in Squeak • • Integer>>+ a. Number "Refer to the comment in Number + " a. Number is. Integer if. True: [self negative == a. Number negative if. True: [^ (self digit. Add: a. Number) normalize] if. False: [^ self digit. Subtract: a. Number]]. ^ a. Number adapt. To. Integer: self and. Send: #+ • • • Number>>adapt. To. Integer: rcvr and. Send: selector "If I am involved in arithmetic with a Integer, convert us and evaluate expr. Block. " ^ self subclass. Responsibility • • • Float>>adapt. To. Integer: rcvr and. Send: selector "If I am involved in arithmetic with an Integer, convert it to a Float. " ^ rcvr as. Float perform: selector with: self • • Fraction>>adapt. To. Integer: rcvr and. Send: selector 10 "If I am involved in arithmetic with an Integer, convert it to a S. Ducasse
as. Float • • Float>>as. Float "Answer the receiver itself. " • ^self • • • Fraction>>as. Float "Answer a Float that closely approximates the value of the receiver. Ideally, answer the Float that most closely approximates the receiver. " • • . . Integer>>as. Float "Answer a Float that represents the value of the receiver. Optimized to process only the significant digits of a Large. Integer” • . . S. Ducasse 11
as. Fraction • • • • S. Ducasse Fraction>>as. Fraction "Answer the receiver itself. " ^self Integer>>as. Fraction "Answer a Fraction that represents value of the receiver. " ^Fraction numerator: self denominator: 1. . Float>>+ a. Number "Primitive. Answer the sum of the receiver and a. Number. Essential. Fail if the argument is not a Float. See Object documentation what. Is. APrimitive. " <primitive: 41> ^ a. Number adapt. To. Float: self and. Send: #+ 12
About Pure OO • • • S. Ducasse Small. Integer 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 13
Summary Automatic coercion Number kind addition S. Ducasse 14
- Slides: 14