home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / smalltk / src / mag.st < prev    next >
Text File  |  1991-10-12  |  17KB  |  756 lines

  1. *
  2. * Little Smalltalk, version 3
  3. * Written by Tim Budd, Oregon State University, July 1988
  4. *
  5. * Classes dealing with objects having Magnitude
  6. *
  7. Class Magnitude Object
  8. Class    Char Magnitude value
  9. Class    Number Magnitude
  10. Class       Integer Number
  11. Class           LongInteger Integer negative digits
  12. Class       Fraction Number top bottom
  13. Class       Float Number
  14. Class Random Object
  15. *
  16. Methods Object 'magnitude'
  17.     isNumber
  18.         ^ false
  19. |
  20.     isFloat
  21.         ^ false
  22. |
  23.     isFraction
  24.         ^ false
  25. |
  26.     isInteger
  27.         ^ false
  28. |
  29.     isLongInteger
  30.         ^ false
  31. |
  32.     isShortInteger
  33.         ^ false
  34. ]
  35. Methods Char 'all'
  36.     < aValue
  37.         " can only compare characters to characters "
  38.         ^ aValue isChar
  39.             ifTrue: [ value < aValue asInteger ]
  40.             ifFalse: [ smalltalk error: 'char compared to nonchar']
  41. |
  42.     == aValue
  43.         ^ aValue isChar
  44.             ifTrue: [ value = aValue asInteger ]
  45.             ifFalse: [ false ]
  46. |
  47.     asInteger
  48.         ^ value
  49. |
  50.     asString
  51.         " make ourselves into a string "
  52.         ^ ' ' copy; at: 1 put: self
  53. |
  54.     digitValue
  55.         " return an integer representing our value "
  56.         self isDigit ifTrue: [ ^ value - $0 asInteger ].
  57.         self isUppercase ifTrue: [ ^ value - $A asInteger + 10 ].
  58.         ^ smalltalk error: 'illegal conversion, char to digit'
  59. |
  60.     isAlphabetic
  61.         ^ (self isLowercase) or: [ self isUppercase ]
  62. |
  63.     isAlphaNumeric
  64.         ^ (self isAlphabetic) or: [ self isDigit ]
  65. |
  66.     isBlank
  67.         ^ value = $   " blank char "
  68. |
  69.     isChar
  70.         ^ true
  71. |
  72.     isDigit
  73.         ^ value between: $0 asInteger and: $9 asInteger
  74. |
  75.     isLowercase
  76.         ^ value between: $a asInteger and: $z asInteger
  77. |
  78.     isUppercase
  79.         ^ value between: $A asInteger and: $Z asInteger
  80. |
  81.     value: aValue        " private - used for initialization "
  82.         value <- aValue
  83. |
  84.     printString
  85.         ^ '$', self asString
  86. ]
  87. Methods Fraction 'all'
  88.     = f
  89.         f isFraction
  90.             ifTrue: [ ^ (top = f top) and: [ bottom = f bottom ] ]
  91.             ifFalse: [ ^ super = f ]
  92. |
  93.     < f
  94.         f isFraction
  95.             ifTrue: [ ^ (top * f bottom) < (bottom * f top) ]
  96.             ifFalse:[ ^ super < f ]
  97. |
  98.     + f
  99.         f isFraction
  100.             ifTrue: [ ^ ((top * f bottom) + (bottom * f top)) /
  101.                     (bottom * f bottom) ]
  102.             ifFalse:[ ^ super + f ]
  103. |
  104.     - f
  105.         f isFraction
  106.             ifTrue: [ ^ ((top * f bottom) - (bottom * f top)) /
  107.                     (bottom * f bottom) ]
  108.             ifFalse:[ ^ super - f ]
  109. |
  110.     * f
  111.         f isFraction
  112.             ifTrue: [ ^ (top * f top) / (bottom * f bottom) ]
  113.             ifFalse: [ ^ super * f ]
  114. |
  115.     / f
  116.         ^ self * f reciprocal
  117. |
  118.     abs
  119.         ^ top abs / bottom
  120. |
  121.     asFloat
  122.         " convert to a floating point number "
  123.  
  124.         ^ top asFloat / bottom asFloat
  125. |
  126.     truncated
  127.         " convert to an integer rounded towards zero "
  128.         ^ top quo: bottom
  129. |
  130.     bottom
  131.         ^ bottom
  132. |
  133.     coerce: x
  134.         " coerce a value into being a fraction "
  135.  
  136.         ^ x asFraction
  137. |
  138.     generality
  139.         " generality value - used in mixed type arithmetic "
  140.         ^ 5
  141. |
  142.     isFraction
  143.         ^ true
  144. |
  145.     ln
  146.         ^ (top ln) - (bottom ln)
  147. |
  148.     raisedTo: x
  149.         ^ (top raisedTo: x) / (bottom raisedTo: x)
  150. |
  151.     reciprocal
  152.         ^ bottom / top
  153. |
  154.     top
  155.         ^ top
  156. |
  157.     with: t over: b
  158.         " initialization "
  159.  
  160.         top <- t.
  161.         bottom <- b
  162. |
  163.     printString
  164.         ^ top printString, '/', bottom printString
  165. ]
  166. Methods Float 'all'
  167.     + value
  168.         ^ value isFloat
  169.             ifTrue: [ <110 self value> " floating add " ]
  170.             ifFalse: [ super + value ]
  171. |
  172.     - value
  173.         ^ value isFloat
  174.             ifTrue: [ <111 self value> " floating subtract " ]
  175.             ifFalse: [ super - value ]
  176. |
  177.     < value
  178.         ^ value isFloat
  179.             ifTrue: [ <112 self value> " floating comparison " ]
  180.             ifFalse: [ super < value ]
  181. |
  182.     = value
  183.         ^ value isFloat
  184.             ifTrue: [ <116 self value> ]
  185.             ifFalse: [ super = value ]
  186. |
  187.     * value
  188.         ^ value isFloat
  189.             ifTrue: [ <118 self value> ]
  190.             ifFalse: [ super * value ]
  191. |
  192.     / value    
  193.         ^ value isFloat
  194.             ifTrue: [ (value = 0.0)
  195.                     ifTrue: [ smalltalk error:
  196.                         'float division by zero' ]
  197.                     ifFalse: [ <119 self value> ]]
  198.             ifFalse: [ super / value ]
  199. |
  200.     isFloat
  201.         ^ true
  202. |
  203.     coerce: value
  204.         " convert the value into a floating point number "
  205.         ^ value asFloat
  206. |
  207.     exp
  208.         " return e raised to self "
  209.         ^ <103 self>
  210. |
  211.     generality
  212.         " our numerical generality - used for mixed mode arithmetic"
  213.         ^ 7
  214. |
  215.     integerPart    | i j |
  216.         i <- <106 self>. j <- i basicAt: 2. i <- i basicAt: 1.
  217.         j < 0 ifTrue: [ ^ 0 ] ifFalse: [ ^ i * (2 raisedTo: j)]
  218. |
  219.     ln
  220.         " natural log of self "
  221.         ^ <102 self>
  222. |
  223.     new
  224.         ^ smalltalk error: 'cannot create floats with new'
  225. |
  226.     printString
  227.         ^ <101 self>
  228. |
  229.     quo: value
  230.         ^ (self / value) truncated
  231. |
  232.     rounded
  233.         ^ (self + 0.5) floor
  234. |
  235.     truncated    | result f i |
  236.         " truncate to an integer rounded towards zero"
  237.         f <- self. result <- 0.
  238.         [ i <- f integerPart. i > 0] whileTrue:
  239.             [ result <- result + i. f <- f - i ].
  240.         ^ result
  241. ]
  242. Methods Integer 'all'
  243.     + value        | r |
  244.         ^ (self isShortInteger and: [value isShortInteger])
  245.             ifTrue: [ r <- <60 self value>.
  246.                   "primitive will return nil on overflow"
  247.                   r notNil ifTrue: [ r ]
  248.                 ifFalse: [ self asLongInteger + value asLongInteger ]]
  249.             ifFalse: [ super + value ]
  250. |
  251.     - value        | r |
  252.         ^ (self isShortInteger and: [value isShortInteger])
  253.             ifTrue: [ r <- <61 self value>.
  254.                   "primitive will return nil on overflow"
  255.                 r notNil ifTrue: [ r ]
  256.                 ifFalse: [ self asLongInteger - value asLongInteger ]]
  257.             ifFalse: [ super - value ]
  258. |
  259.     < value
  260.         ^ (self isShortInteger and: [value isShortInteger])
  261.             ifTrue: [ <62 self value> ]
  262.             ifFalse: [ super < value ]
  263. |
  264.     > value
  265.         ^ (self isShortInteger and: [value isShortInteger])
  266.             ifTrue: [ <63 self value> ]
  267.             ifFalse: [ super > value ]
  268. |
  269.     = value
  270.         ^ (self isShortInteger and: [value isShortInteger])
  271.             ifTrue: [ self == value ]
  272.             ifFalse: [ super = value ]
  273. |
  274.     * value        | r |
  275.         ^ (self isShortInteger and: [value isShortInteger])
  276.             ifTrue: [ r <- <68 self value>.
  277.                   "primitive will return nil on overflow"
  278.                   r notNil ifTrue: [ r ]
  279.                   ifFalse: [ self asLongInteger * value asLongInteger ]]
  280.             ifFalse: [ super * value ]
  281. |
  282.     / value        | t b |
  283.         value = 0 ifTrue: [ ^ smalltalk error: 'division by zero'].
  284.  
  285.         value isInteger
  286.             ifTrue: [ b <- self gcd: value .
  287.                   t <- self quo: b.
  288.                   b <- value quo: b.
  289.                   b negative
  290.                     ifTrue: [ t <- t negated. 
  291.                           b <- b negated ].
  292.                   (b = 1) ifTrue: [ ^ t ].
  293.                   ^ Fraction new; with: t over: b ]
  294.             ifFalse: [ ^ super / value ]
  295. |
  296.     , value
  297.         " used to make long integer constants "
  298.         ^ self * 1000 + value
  299. |
  300.     allMask: value
  301.         " see if all bits in argument are on"
  302.         ^ value = (self bitAnd: value)
  303. |
  304.     anyMask: value
  305.         " see if any bits in argument are on"
  306.         ^ 0 ~= (self bitAnd: value)
  307. |
  308.     asCharacter
  309.         ^ Char new; value: self
  310. |
  311.     asDigit
  312.         " return as character digit "
  313.         (self >= 0)
  314.             ifTrue: [ (self <= 9) ifTrue: 
  315.                     [ ^ (self + $0 asInteger) asCharacter ].
  316.                   (self < 36) ifTrue:
  317.                     [ ^ (self + $A asInteger - 10) asCharacter ] ].
  318.         ^ smalltalk error: 'illegal conversion, integer to digit'
  319. |
  320.     asFloat
  321.         " should be redefined by any subclasses "
  322.         self isShortInteger ifTrue: [ ^ <51 self> ]
  323. |
  324.     asFraction
  325.         ^ Fraction new ; with: self over: 1
  326. |
  327.     asLongInteger    | newList i |
  328.         newList <- List new.
  329.         i = 0 ifTrue: [ newList add: 0 ]
  330.             ifFalse: [ i <- self abs.
  331.                    [ i ~= 0 ] whileTrue: 
  332.                     [ newList addLast: (i rem: 100).
  333.                     i <- i quo: 100 ] ].
  334.         ^ LongInteger new; sign: i negative digits: newList asArray
  335. |
  336.     asString
  337.         ^ self radix: 10
  338. |
  339.     bitAnd: value
  340.         ^ (self isShortInteger and: [value isShortInteger])
  341.             ifTrue: [ <71 self value > ]
  342.             ifFalse: [ smalltalk error: 
  343.                 'arguments to bit operation must be short integer']
  344. |
  345.     bitAt: value
  346.         ^ (self bitShift: 1 - value) bitAnd: 1
  347. |
  348.     bitInvert
  349.         "invert all bits in self"
  350.         ^ self bitXor: -1
  351. |
  352.     bitOr: value
  353.         ^ (self bitXor: value) bitXor: (self bitAnd: value)
  354. |
  355.     bitXor: value
  356.         ^ (self isShortInteger and: [value isShortInteger])
  357.             ifTrue: [ <72 self value > ]
  358.             ifFalse: [ smalltalk error: 
  359.                 'argument to bit operation must be integer']
  360. |
  361.     bitShift: value
  362.         ^ (self isShortInteger and: [value isShortInteger])
  363.             ifTrue: [ <79 self value > ]
  364.             ifFalse: [ smalltalk error: 
  365.                 'argument to bit operation must be integer']
  366. |
  367.     even
  368.         ^ (self rem: 2) = 0
  369. |
  370.     factorial
  371.         ^ (2 to: self) inject: 1 into: [:x :y | x * y ]
  372. |
  373.     gcd: value
  374.         (value = 0) ifTrue: [ ^ self ].
  375.         (self negative) ifTrue: [ ^ self negated gcd: value ].
  376.         (value negative) ifTrue: [ ^ self gcd: value negated ].
  377.         (value > self) ifTrue: [ ^ value gcd: self ].
  378.         ^ value gcd: (self rem: value)
  379. |
  380.     generality
  381.         " generality value - used in mixed class arithmetic "
  382.         ^ 2
  383. |
  384.     isShortInteger
  385.         ^ true
  386. |
  387.     lcm: value
  388.         ^ (self quo: (self gcd: value)) * value
  389. |
  390.     new
  391.         ^ smalltalk error: 'cannot create integers with new'
  392. |
  393.     odd
  394.         ^ (self rem: 2) ~= 0
  395. |
  396.     quo: value    | r |
  397.         ^ (self isShortInteger and: [value isShortInteger])
  398.             ifTrue: [ r <- <69 self value>.
  399.                 (r isNil)
  400.                     ifTrue: [ smalltalk error:
  401.                         'quo: or rem: with argument 0']
  402.                     ifFalse: [ r ]]
  403.             ifFalse: [ ^ super quo: value ]
  404. |
  405.     radix: base     | sa text |
  406.         " return a printed representation of self in given base"
  407.         sa <- self abs.
  408.         text <- (sa \\ base) asDigit asString.
  409.         ^ (sa < base)
  410.             ifTrue: [ (self negative)
  411.                     ifTrue: [ '-' , text ]
  412.                     ifFalse: [ text ]]
  413.             ifFalse: [ ((self quo: base) radix: base), text ]
  414. |
  415.     truncated
  416.         ^ self
  417. |
  418.     printString
  419.         ^ self asString
  420. |
  421.     timesRepeat: aBlock    | i |
  422.         " use while, which is optimized, not to:, which is not"
  423.         i <- 0.
  424.         [ i < self ] whileTrue:
  425.             [ aBlock value. i <- i + 1]
  426. ]
  427. Methods LongInteger 'all'
  428.     < n        | result |
  429.         n isLongInteger
  430.             ifFalse: [ ^ super < n ].
  431.         (negative == n negative) ifFalse: [ ^ negative ].
  432.         " now either both positive or both negative "
  433.         result <- false.
  434.         self with: n bitDo: 
  435.             [:x :y | (x ~= y) ifTrue: [ result <- x < y]].
  436.         negative ifTrue: [ result <- result not ].
  437.         ^ result
  438. |
  439.     = n
  440.         n isLongInteger
  441.             ifFalse: [ ^ super = n ].
  442.         (negative == n negative) ifFalse: [ ^ false ].
  443.         ^ digits = n digits
  444. |
  445.     + n        | newDigits z carry |
  446.         n isLongInteger
  447.             ifFalse: [ ^ super + n ].
  448.         negative ifTrue: [ ^ n - self negated ].
  449.         n negative ifTrue: [ ^ self - n negated ].
  450.         " reduced to positive + positive case "
  451.         newDigits <- List new.  carry <- 0.
  452.         self with: n bitDo:
  453.             [:x :y | z <- x + y + carry.
  454.                 (z >= 100) ifTrue: [ carry <- 1. z <- z - 100]
  455.                      ifFalse: [ carry <- 0 ].
  456.                 newDigits addLast: z ].
  457.         carry > 0 ifTrue: [ newDigits addLast: carry ].
  458.         ^ LongInteger new; sign: false digits: newDigits asArray
  459. |
  460.     - n        | result newDigits z borrow |
  461.         n isLongInteger
  462.             ifFalse: [ ^ super - n ].
  463.         negative ifTrue: [ ^ (self negated + n) negated ].
  464.         n negative ifTrue: [ ^ self + n negated ].
  465.         (self < n) ifTrue: [ ^ (n - self) negated ].
  466.         " reduced to positive - smaller positive "
  467.         newDigits <- List new. borrow <- 0.
  468.         self with: n bitDo:
  469.             [:x :y | z <- (x - borrow) - y.
  470.                 (z >= 0) ifTrue: [ borrow <- 0]
  471.                 ifFalse: [ z <- z + 100. borrow <- 1].
  472.                 newDigits addLast: z ].
  473.         result <- 0. "now normalize result by multiplication "
  474.         newDigits reverseDo: [:x | result <- result * 100 + x ].
  475.         ^ result
  476. |
  477.     * n        | result |
  478.         n isShortInteger ifTrue: [ ^ self timesShort: n ].
  479.         n isLongInteger  ifFalse: [ ^ super * n ].
  480.         result <- 0 asLongInteger.
  481.         digits reverseDo: 
  482.             [:x | result <- (result timesShort: 100) +
  483.                 (n timesShort: x)].
  484.         negative ifTrue: [ result <- result negated ].
  485.         ^ result
  486. |
  487.     abs
  488.         negative ifTrue: [ ^ self negated] 
  489. |
  490.     asFloat        | r |
  491.         r <- 0.0 .
  492.         digits reverseDo: [ :x | r <- r * 100.0 + x asFloat].
  493.         negative ifTrue: [ r <- r negated ].
  494.         ^ r.
  495. |
  496.     bitShift: n
  497.         (n >= 0)
  498.             ifTrue: [ ^ self * (2 raisedTo: n) ]
  499.             ifFalse: [ ^ self quo: (2 raisedTo: n negated)]
  500. |
  501.     coerce: n
  502.         ^ n asLongInteger
  503. |
  504.     digits
  505.         ^ digits
  506. |
  507.     generality
  508.         ^ 4 "generality value - used in mixed type arithmetic "
  509. |
  510.     isLongInteger
  511.         ^ true
  512. |
  513.     isShortInteger
  514.         " override method in class Integer "
  515.         ^ false
  516. |
  517.     negated
  518.         ^ LongInteger new; sign: negative not digits: digits
  519. |
  520.     negative
  521.         ^ negative
  522. |
  523.     new
  524.         "override restriction from class Integer"
  525.         ^ self
  526. |
  527.     quo: value    | a b quo result |
  528.         result <- 0.
  529.         a <- self abs. b <- value abs.
  530.         [a > b] whileTrue:
  531.             [ quo <- (a asFloat quo: b). result <- result + quo.
  532.                 a <- a - (b * quo) ].
  533.         ^ result
  534. |
  535.     sign: s digits: d
  536.         negative <- s.
  537.         digits <- d.
  538. |
  539.     printString    | str |
  540.         str <- negative ifTrue: [ '-' ] ifFalse: [ '' ].
  541.         digits reverseDo: [:x | str <- str , 
  542.             (x quo: 10) printString , (x rem: 10) printString ].
  543.         ^ str
  544. |
  545.     timesShort: value    | y z carry newDigits |
  546.         y <- value abs.
  547.         carry <- 0.
  548.         newDigits <- digits collect:
  549.             [:x | z <- x * y + carry. 
  550.                 carry <- z quo: 100. 
  551.                 z - (carry * 100)].
  552.         (carry > 0) ifTrue: [ newDigits <- newDigits grow: carry ].
  553.         ^ LongInteger new; sign: (negative xor: value negative) 
  554.                     digits: newDigits
  555. |
  556.     with: n bitDo: aBlock    | d di dj |
  557.         " run down two digits lists in parallel doing block "
  558.         di <- digits size.
  559.         d <- n digits.
  560.         dj <- d size.
  561.         (1 to: (di max: dj)) do: [:i |
  562.             aBlock value: 
  563.                ((i <= di) ifTrue: [ digits at: i] ifFalse: [0])
  564.                 value:
  565.                ((i <= dj) ifTrue: [ d at: i] ifFalse: [0]) ]
  566. ]
  567. Methods Magnitude 'all'
  568.     <= value
  569.         ^ (self < value) or: [ self = value ]
  570. |
  571.     < value
  572.         ^ (self <= value) and: [ self ~= value ]
  573. |
  574.     >= value
  575.         ^ value <= self
  576. |
  577.     > value
  578.         ^ (value < self)
  579. |
  580.     = value
  581.         ^ (self == value)
  582. |
  583.     ~= value
  584.         ^ (self = value) not
  585. |
  586.     between: low and: high
  587.         ^ (low <= self) and: [ self <= high ]
  588. |
  589.     isChar
  590.         ^ false
  591. |
  592.     max: value
  593.         ^ (self < value)
  594.             ifTrue: [ value ]
  595.             ifFalse: [ self ]
  596. |
  597.     min: value
  598.         ^ (self < value)
  599.             ifTrue: [ self ]
  600.             ifFalse: [ value ]
  601. ]
  602. Methods Number 'all'
  603.     isNumber
  604.         ^ true
  605. |
  606.     maxgen: value
  607.         (self isNumber and: [ value isNumber ])
  608.             ifFalse: [ ^ smalltalk error: 
  609.                 'arithmetic on non-numbers' ].
  610.         ^ (self generality > value generality)
  611.             ifTrue: [ self ]
  612.             ifFalse: [ value coerce: self ]
  613. |
  614.     + value
  615.         ^ (self maxgen: value) + (value maxgen: self)
  616. |
  617.     - value
  618.         ^ (self maxgen: value) - (value maxgen: self)
  619. |
  620.     < value
  621.         ^ (self maxgen: value) < (value maxgen: self)
  622. |
  623.     = value
  624.         ^ value isNumber
  625.             ifTrue: [ (self maxgen: value) = (value maxgen: self) ]
  626.             ifFalse: [ false ]
  627. |
  628.     * value
  629.         ^ (self maxgen: value) * (value maxgen: self)
  630. |
  631.     / value
  632.         ^ (self maxgen: value) / (value maxgen: self)
  633. |
  634.     // value
  635.         " integer division, truncate towards negative infinity"
  636.         " see quo: "
  637.         ^ (self / value) floor
  638. |
  639.     \\ value
  640.         " remainder after integer division "
  641.         ^ self - (self // value * value)
  642. |
  643.     abs
  644.         ^ (self < 0)
  645.             ifTrue: [ 0 - self ]
  646.             ifFalse: [ self ]
  647. |
  648.     ceiling        | i |
  649.         i <- self truncated.
  650.         ^ ((self positive) and: [ self ~= i ])
  651.             ifTrue: [ i + 1 ]
  652.             ifFalse: [ i ]
  653. |
  654.     copy
  655.         ^ self
  656. |
  657.     exp
  658.         ^ self asFloat exp
  659. |
  660.     floor        | i |
  661.         i <- self truncated.
  662.         ^ ((self negative) and: [ self ~= i ])
  663.             ifTrue: [ i - 1 ]
  664.             ifFalse: [ i ]
  665. |
  666.     fractionalPart
  667.         ^ self - self truncated
  668. |
  669.     isInteger
  670.         ^ self isLongInteger or: [ self isShortInteger ]
  671. |
  672.     ln
  673.         ^ self asFloat ln
  674. |
  675.     log: value
  676.         ^ self ln / value ln
  677. |
  678.     negated
  679.         ^ 0 - self
  680. |
  681.     negative
  682.         ^ self < 0
  683. |
  684.     positive
  685.         ^ self >= 0
  686. |
  687.     quo: value
  688.         ^ (self maxgen: value) quo: (value maxgen: self)
  689. |
  690.     raisedTo: x    | y |
  691.         x negative 
  692.             ifTrue: [ ^ 1 / (self raisedTo: x negated) ].
  693.         x isShortInteger 
  694.             ifTrue: [ (x = 0) ifTrue: [ ^ 1 ].
  695.                   y <- (self raisedTo: (x quo: 2)) squared.
  696.                   x odd ifTrue: [ y <- y * self ].
  697.                   ^ y ]
  698.                 "use logrithms to do exponeneation"
  699.             ifFalse: [ ^ ( x * self ln ) exp ]
  700. |
  701.     reciprocal
  702.         ^ 1 / self
  703. |
  704.     rem: value
  705.         ^ self - ((self quo: value) * value)
  706. |
  707.     roundTo: value
  708.         ^ (self / value ) rounded * value
  709. |
  710.     sign
  711.         ^ (self = 0) ifTrue: [ 0 ]
  712.             ifFalse: [ self / self abs ]
  713. |
  714.     sqrt
  715.         ^ (self negative)
  716.             ifTrue: [ smalltalk error: 'sqrt of negative']
  717.             ifFalse: [ self raisedTo: 0.5 ]
  718. |
  719.     squared
  720.         ^ self * self
  721. |
  722.     strictlyPositive
  723.         ^ self > 0
  724. |
  725.     to: value
  726.         ^ Interval new; lower: self; upper: value; step: 1
  727. |
  728.     to: value by: step
  729.         ^ Interval new; lower: self; upper: value; step: step
  730. |
  731.     trucateTo: value
  732.         ^ (self / value) trucated * value
  733. ]
  734. Methods Random 'all'
  735.     between: low and: high
  736.         " return random number in given range "
  737.         ^ (self next * (high - low)) + low
  738. |
  739.     next
  740.         " convert rand integer into float between 0 and 1 "
  741.         ^ (<3> rem: 1000) / 1000
  742. |
  743.     next: value    | list |
  744.         " return a list of random numbers of given size "
  745.         list <- List new.
  746.         value timesRepeat: [ list add: self next ].
  747.         ^ list
  748. |
  749.     randInteger: value
  750.         ^ 1 + (<3> rem: value)
  751. |
  752.     set: value
  753.         " set seed for random number generator "
  754.         <55 value>
  755. ]
  756.