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

  1. "======================================================================
  2. |
  3. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  4. | Written by David Duke.
  5. | Slightly modified by Steve Byrne.
  6. |
  7. | This file is part of GNU Smalltalk.
  8. |
  9. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  10. | under the terms of the GNU General Public License as published by the Free
  11. | Software Foundation; either version 1, or (at your option) any later version.
  12. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  13. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  15. | details.
  16. | You should have received a copy of the GNU General Public License along with
  17. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  18. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  19. |
  20.  ======================================================================"
  21.  
  22. "
  23. |     Change Log
  24. | ============================================================================
  25. | Author       Date       Change 
  26. | sbb         16 Feb 92      Created in the 1.1 timeframe
  27. |
  28. "
  29.  
  30. Number subclass: #Fraction
  31.     instanceVariableNames: 'numerator denominator'
  32.     classVariableNames: ''
  33.     poolDictionaries: ''
  34.     category: nil!
  35.  
  36. Fraction comment:
  37. ' I represent rational numbers in the form (p/q) where p and q are integers.
  38. The arithmetic operations  *, +, -, /,  on fractions, all return a reduced 
  39. fraction.'!
  40.  
  41.  
  42. !Integer methodsFor: 'basic math'!
  43.  
  44. / arg             "from changes.st"
  45.     <primitive: 10>
  46.     (arg isMemberOf: Integer) & (self isMemberOf: Integer) 
  47.     ifTrue: [ ^(Fraction numerator: self denominator: arg)]. 
  48.     ^self retry: #/ coercing: arg
  49. ! !
  50.  
  51.  
  52.  
  53. !Integer methodsFor: 'converting'!
  54.  
  55. asFraction
  56.     ^Fraction numerator: self denominator: 1
  57. !!
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. !Fraction methodsFor: 'accessing'!
  65. denominator
  66.     ^denominator!
  67. numerator
  68.     ^numerator 
  69. !!
  70.  
  71.  
  72. !Fraction methodsFor: 'arithmetic'!
  73. * aNumber 
  74.     "I implement * for fractions."
  75.  
  76.     (aNumber isMemberOf: Fraction)
  77.         ifTrue: [^(Fraction numerator: numerator * aNumber numerator denominator: denominator * aNumber denominator) reduced]
  78.         ifFalse: [^self retry: #* coercing: aNumber]!
  79. + aNumber 
  80.     "I implement + for fractions."
  81.  
  82.     (aNumber isMemberOf: Fraction)
  83.         ifTrue: [^(Fraction numerator: (numerator * aNumber denominator) + (aNumber numerator * denominator) denominator: denominator * aNumber denominator) reduced]
  84.         ifFalse: [^self retry: #+ coercing: aNumber]!
  85. - aNumber 
  86.     "I implement - for fractions."
  87.  
  88.     (aNumber isMemberOf: Fraction)
  89.         ifTrue: [^self + (aNumber negated)]
  90.         ifFalse: [^self retry: #- coercing: aNumber]!
  91. / aNumber 
  92.     "I implement / for fractions."
  93.  
  94.     (aNumber isMemberOf: Fraction)
  95.         ifTrue: [^self * (aNumber reciprocal)]
  96.         ifFalse: [^self retry: #/ coercing: aNumber] !
  97. // aNumber
  98.     "Return the integer quotient of dividing the receiver by aNumber with
  99.     truncation towards negative infinity."
  100.     ^(self / aNumber) floor !
  101. \\ aNumber
  102.     "Return the remainder from dividing the receiver by aNumber, (using //)."
  103.     ^(self - (self // aNumber * aNumber)) !
  104. negated
  105.     ^Fraction numerator: numerator negated denominator: denominator !
  106. reciprocal
  107.     denominator < 0
  108.         ifTrue: [^Fraction numerator: denominator negated denominator: numerator negated]
  109.         ifFalse: [^Fraction numerator: denominator denominator: numerator]
  110.  
  111. !!
  112.  
  113.  
  114. !Fraction methodsFor: 'coercing'!
  115. coerce: aNumber 
  116.     ^aNumber asFraction!
  117. generality
  118.     ^3!
  119. truncated
  120.     ^numerator quo: denominator
  121. !!
  122.  
  123. !Fraction methodsFor: 'comparing'!
  124. < arg 
  125.     "I test if the receiver is less than arg."
  126.  
  127.     (arg isMemberOf: Fraction)
  128.         ifTrue: [^arg denominator * numerator < (denominator * arg numerator)]
  129.         ifFalse: [^self retry: #< coercing: arg]!
  130. > arg 
  131.     "I test if the receiver is more than arg."
  132.  
  133.     (arg isMemberOf: Fraction)
  134.         ifTrue: [^arg denominator * numerator > (denominator * arg numerator)]
  135.         ifFalse: [^self retry: #> coercing: arg]!
  136. = arg 
  137.     "I test if the receiver is equals arg."
  138.  
  139.     (arg isMemberOf: Fraction)
  140.         ifTrue: [^arg denominator * numerator = (denominator * arg numerator)]
  141.         ifFalse: [^self retry: #= coercing: arg]!
  142. hash
  143.     ^numerator bitXor: denominator
  144. !!
  145.  
  146. !Fraction methodsFor: 'converting'!
  147. asFloat
  148.     ^numerator asFloat / denominator asFloat!
  149. asFraction
  150.     ^self 
  151. !!
  152.  
  153.  
  154. !Fraction methodsFor: 'printing'!
  155. printOn: aStream
  156.  
  157.     " Fractions print as (numerator/denominator) eg (3/4) ."
  158.  
  159.     aStream nextPut: $(; 
  160.         print: numerator;
  161.         nextPut: $/;
  162.         print: denominator;
  163.         nextPut: $) 
  164. !!
  165.  
  166.  
  167. !Fraction methodsFor: 'private'!
  168. reduced
  169.     | gcd |
  170.     numerator = 1 ifTrue: [^self].
  171.     denominator = 1 ifTrue: [^numerator].
  172.     numerator = 0 ifTrue: [^0].
  173.     numerator = denominator ifTrue: [^1].
  174.     gcd _ numerator gcd: denominator.
  175.     gcd = 1 ifTrue: [^self].
  176.     denominator = gcd ifTrue: [^numerator // gcd].
  177.     ^Fraction numerator: numerator // gcd denominator: denominator // gcd !
  178. setNumerator: numInteger setDenominator: denInteger 
  179.     denInteger = 0
  180.         ifTrue: [^self error: 'The denominator can not be zero'].
  181.     denInteger < 0
  182.         ifTrue: [numerator _ numInteger negated.
  183.             denominator _ denInteger negated]
  184.         ifFalse: [numerator _ numInteger.
  185.             denominator _ denInteger]
  186. !!
  187.  
  188.  
  189. !Fraction class methodsFor: 'instance creation'!
  190.  
  191. numerator: nInteger denominator: dInteger 
  192.     " Answer a new instance of fraction (nInteger/dInteger)"
  193.  
  194.     ^self new setNumerator: nInteger setDenominator: dInteger 
  195. !!
  196.  
  197.