home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / number.st < prev    next >
Text File  |  1992-02-15  |  6KB  |  311 lines

  1. "======================================================================
  2. |
  3. |   Number Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbb          9 Nov 90      Put in changes for fractions.
  36. |
  37. | sbyrne     19 Sep 89      Converted to use real category strings.
  38. |
  39. | sbyrne     25 Apr 89      created.
  40. |
  41. "
  42.  
  43. Magnitude subclass: #Number
  44.       instanceVariableNames: ''
  45.       classVariableNames: ''
  46.       poolDictionaries: ''
  47.       category: nil
  48. !
  49.  
  50. Number comment: 
  51. 'I am an abstract class that provides operations on numbers, both floating
  52. point and integer.  I provide some generic predicates, and supply the 
  53. implicit type coercing code for binary operations.' !
  54.  
  55. !Number methodsFor: 'converting'!
  56.  
  57. degreesToRadians
  58.     ^self / 57.295779513082320876846364344191
  59. !
  60.  
  61. radiansToDegrees
  62.     ^self * 57.295779513082320876846364344191
  63. !
  64.  
  65. coerce: aNumber
  66.     self subclassResponsibility
  67. !
  68.  
  69. generality
  70.     self subclassResponsibility
  71. !
  72.  
  73. retry: aSymbol coercing: aNumber
  74.     | selfGen aNumGen |
  75.     (aSymbol == #= or: [ aSymbol == #~= ])
  76.         ifTrue: [ (aNumber isKindOf: Number) 
  77.               "Return false for =, true for ~="
  78.               ifFalse: [ ^aSymbol ~= #= ] ].
  79.     selfGen _ self generality.
  80.     aNumGen _ aNumber generality.
  81.     selfGen > aNumGen
  82.         ifTrue: [ ^self perform: aSymbol with: (self coerce: aNumber) ].
  83.     selfGen < aNumGen
  84.         ifTrue: [ ^(aNumber coerce: self) perform: aSymbol with: aNumber ].
  85.     self error: 'retry:coercing: called with arguments of the same generality' 
  86. !!
  87.  
  88.  
  89.  
  90. !Number methodsFor: 'arithmetic'!
  91.  
  92. + aNumber
  93.     self subclassResponsibility
  94. !
  95.  
  96. - aNumber
  97.     self subclassResponsibility
  98. !
  99.  
  100. * aNumber
  101.     self subclassResponsibility
  102. !
  103.  
  104. / aNumber
  105.     self subclassResponsibility
  106. !
  107.  
  108. // aNumber
  109.     self subclassResponsibility
  110. !
  111.  
  112. \\ aNumber
  113.     self subclassResponsibility
  114. !
  115.  
  116. quo: aNumber
  117.     "Return the integer quotient of dividing the receiver by aNumber with
  118.     truncation towards zero."
  119.     ^(self / aNumber) truncated
  120. !
  121.  
  122. rem: aNumber
  123.     "Return the remainder of dividing the receiver by aNumber with
  124.     truncation towards zero."
  125.     ^self - ((self quo: aNumber) * aNumber)
  126. !!
  127.  
  128.  
  129.  
  130. !Number methodsFor: 'truncation and round off'!
  131.  
  132. truncated
  133.     ^self subclassResponsibility
  134. !
  135.  
  136. truncateTo: aNumber
  137.     ^(self / aNumber) truncated * aNumber
  138. !
  139.  
  140. rounded
  141.     "Returns the integer nearest the receiver"
  142.     ^(self + 0.5) truncated
  143. !
  144.  
  145. roundTo: aNumber
  146.     ^(self / aNumber) rounded * aNumber
  147. !!
  148.  
  149.  
  150.  
  151. !Number methodsFor: 'testing'!
  152. negative
  153.     ^self < 0
  154. !
  155.  
  156. positive
  157.     ^self >= 0
  158. !
  159.  
  160. strictlyPositive
  161.     ^self > 0
  162. !
  163.  
  164. sign
  165.     "Returns the sign of the receiver."
  166.     self < 0 ifTrue: [ ^-1 ].
  167.     self > 0 ifTrue: [ ^1 ].
  168.     ^0
  169. !
  170.  
  171. even
  172.     "Returns true if self is divisible by 2"
  173.     ^self truncated even
  174. !
  175.  
  176. odd
  177.     "Returns true if self is not divisible by 2"
  178.     ^self truncated odd
  179. !!
  180.  
  181.  
  182.  
  183. !Number methodsFor: 'misc math'!
  184.  
  185. squared
  186.     ^self * self
  187. !
  188.  
  189. abs
  190.     self > 0 ifTrue: [ ^self ] ifFalse: [ ^self negated ]
  191. !
  192.  
  193. negated
  194.     ^0 - self
  195. !
  196.  
  197. sin
  198.     ^self asFloat sin
  199. !
  200.  
  201. cos
  202.     ^self asFloat cos
  203. !
  204.  
  205. tan
  206.     ^self asFloat tan
  207. !
  208.  
  209. arcSin
  210.     ^self asFloat arcSin
  211. !
  212.  
  213. arcCos
  214.     ^self asFloat arcCos
  215. !
  216.  
  217. arcTan
  218.     ^self asFloat arcTan
  219. !
  220.  
  221. sqrt
  222.     ^self asFloat sqrt
  223. !
  224.  
  225. exp
  226.     ^self asFloat exp
  227. !
  228.  
  229. ln
  230.     ^self asFloat ln
  231. !
  232.  
  233. log: aNumber
  234.     "return log base aNumber of the receiver"
  235.     ^self ln / aNumber ln
  236. !
  237.  
  238. floorLog: radix
  239.     ^(self log: radix) floor
  240. !
  241.  
  242. raisedToInteger: anInteger
  243.     "Return self raised to anInteger power"
  244.     | result |
  245.     result _ self coerce: 1.
  246.     anInteger timesRepeat: [ result _ result * self ].
  247.     ^result
  248. !!
  249.  
  250.  
  251.  
  252.  
  253. !Number methodsFor: 'truncation and round off'!
  254.  
  255. floor
  256.     "Return the integer nearest the receiver toward negative infinity."
  257.  
  258.     | selfTruncated |
  259.     selfTruncated _ self truncated.
  260.     "If positive, truncation to zero is what we want."
  261.     self >= 0 ifTrue: [^selfTruncated].
  262.     "Must be negative."
  263.     self = selfTruncated
  264.         ifTrue: [^selfTruncated]
  265.         ifFalse: [^selfTruncated - 1]
  266. !!
  267.  
  268. !Number methodsFor: 'arithmetic'!
  269.  
  270. reciprocal
  271.     self = 0
  272.         ifTrue: [self error: 'can not return the reciprocal of zero']
  273.         ifFalse: [^1 / self]
  274. !!
  275.  
  276.  
  277.  
  278. !Number methodsFor: 'Interval iterators'!
  279.  
  280. to: stop
  281.     ^Interval from: self to: stop
  282. !
  283.  
  284. to: stop by: step
  285.     ^Interval from: self to: stop by: step
  286. !
  287.  
  288. to: stop by: step do: aBlock
  289.     | i |
  290.     i _ self.
  291.     step > 0
  292.         ifTrue: [
  293.             [ i <= stop ]
  294.             whileTrue: [ aBlock value: i.
  295.                      i _ i + step ]
  296.         ]
  297.     ifFalse: [
  298.             [ i >= stop ]
  299.             whileTrue: [ aBlock value: i.
  300.                      i _ i + step ]
  301.     ]
  302.     " ??? What's the return value? "
  303. !
  304.  
  305. to: stop do: aBlock
  306.     ^self to: stop by: 1 do: aBlock
  307. !!
  308.  
  309.