Infinity.st
changeset 265 f014922e3b71
child 303 3b550c73b036
equal deleted inserted replaced
264:75289d9aae94 265:f014922e3b71
       
     1 "       NAME            infinity
       
     2         AUTHOR          manchester
       
     3         FUNCTION        Provides a class of infinities
       
     4         ST-VERSION      2.2
       
     5         PREREQUISITES   
       
     6         CONFLICTS
       
     7         DISTRIBUTION    world
       
     8         VERSION         1
       
     9         DATE    22 Jan 1989
       
    10 SUMMARY
       
    11 This is a set of changes that implements infinity in the Number hierarchy.  
       
    12 I obtained the original changes from the author of an article in comp.lang.smalltalk.
       
    13 I have just installed it in my image and I have found two small omissions
       
    14 which are corrected in what is below; there might be others.  Arithmetic
       
    15 between infinities is not defined but magnitude comparisons are implemented.
       
    16 
       
    17 Claus: fixed some minor bugs (args to errorUndefinedResult:) and some
       
    18        wrong comments.
       
    19        Changed retry:coercing: to match ST/X
       
    20 "!
       
    21 
       
    22 !Point methodsFor: 'testing'!
       
    23 
       
    24 isFinite
       
    25         ^x isFinite and: [y isFinite]!
       
    26 
       
    27 isInfinite
       
    28         ^x isInfinite or: [y isInfinite]! !
       
    29 
       
    30 !Number methodsFor: 'testing'!
       
    31 
       
    32 isFinite
       
    33         ^true!
       
    34 
       
    35 isInfinite
       
    36         ^false! !
       
    37 
       
    38 !Number methodsFor: 'coercing'!
       
    39 
       
    40 retry: aSymbol coercing: aNumber
       
    41     "Arithmetic represented by the symbol, aSymbol,
       
    42     could not be performed with the receiver and the argument,
       
    43     aNumber, because of the differences in representation.  Coerce either
       
    44     the receiver or the argument, depending on which has higher generality, and
       
    45     try again.  If the symbol is the equals sign, answer false if the argument
       
    46     is not a Number.  If the generalities are the same, create an error message."
       
    47 
       
    48     |myGenerality otherGenerality|
       
    49 
       
    50     (aSymbol == #=) ifTrue:[
       
    51         (aNumber respondsTo:#generality) ifFalse:[^ false]
       
    52     ] ifFalse:[
       
    53         (aNumber respondsTo:#generality) ifFalse:[
       
    54             self error:'retry:coercing: argument is not a number'.
       
    55             ^ self
       
    56         ]
       
    57     ].
       
    58     myGenerality := self generality.
       
    59     otherGenerality := aNumber generality.
       
    60     (myGenerality > otherGenerality) ifTrue:[
       
    61         ^ self perform:aSymbol with:(self coerce:aNumber)
       
    62     ].
       
    63     (myGenerality < otherGenerality) ifTrue:[
       
    64         aNumber isInfinite ifTrue: [
       
    65             ^ aNumber retryReverseOf:aSymbol with:self
       
    66         ].
       
    67         ^ (aNumber coerce:self) perform:aSymbol with:aNumber
       
    68     ].
       
    69     self error:'retry:coercing: oops - same generality'
       
    70 ! !
       
    71 
       
    72 Number subclass: #Infinity
       
    73         instanceVariableNames: 'positive '
       
    74         classVariableNames: 'NegativeInfinity PositiveInfinity '
       
    75         poolDictionaries: ''
       
    76         category: 'Magnitude-Numbers'!
       
    77 
       
    78 Infinity comment:
       
    79 'I have two instances representing positive and negative infinity.
       
    80 
       
    81 Instance Variables :-
       
    82         positive <Boolean>      :       if true the instance represents positive
       
    83                                         infinity. if false, negative infinity'
       
    84 !
       
    85 
       
    86 !Infinity methodsFor: 'arithmetic'!
       
    87 
       
    88 * aNumber
       
    89         "Multiply the receiver by the argument and answer with the result."
       
    90 
       
    91         aNumber isInfinite ifTrue: [
       
    92             self errorUndefinedResult: #*
       
    93         ].
       
    94         ^self
       
    95 !
       
    96 
       
    97 + aNumber
       
    98         "Add the receiver by the argument and answer with the result."
       
    99 
       
   100         (aNumber isInfinite and: [aNumber ~~ self]) ifTrue: [
       
   101             self errorUndefinedResult: #+
       
   102         ].
       
   103         ^self
       
   104 !
       
   105 
       
   106 - aNumber
       
   107         "subtracet aNumber from the receiver answer with the result."
       
   108 
       
   109         (aNumber isInfinite) ifTrue: [
       
   110             self errorUndefinedResult: #-
       
   111         ].
       
   112         ^self
       
   113 !
       
   114 
       
   115 / aNumber
       
   116         "Divide the receiver by the argument and answer with the result."
       
   117 
       
   118         (aNumber isInfinite or: [aNumber = 0]) ifTrue: [
       
   119             self errorUndefinedResult: #/
       
   120         ].
       
   121         ^self
       
   122 ! !
       
   123 
       
   124 !Infinity methodsFor: 'comparing'!
       
   125 
       
   126 < aNumber
       
   127         "Positive infinity is greater than any number than positive infinity.
       
   128          Analogously, negative infinity is less than any other number other
       
   129          than negative infinity"
       
   130 
       
   131         aNumber == self ifTrue: [^false].
       
   132         ^ positive not!
       
   133 
       
   134 = aNumber
       
   135         ^aNumber == self
       
   136 ! !
       
   137 
       
   138 !Infinity methodsFor: 'testing'!
       
   139 
       
   140 isFinite
       
   141         ^false
       
   142 !
       
   143 
       
   144 isInfinite
       
   145         ^true
       
   146 ! !
       
   147 
       
   148 !Infinity methodsFor: 'coercing'!
       
   149 
       
   150 generality
       
   151         "Infinities are more general than scalars, but not more general than
       
   152          vectors (e.g. Points)"
       
   153         ^85
       
   154 !
       
   155 
       
   156 retryReverseOf: aSymbol with: aNumber
       
   157         (aSymbol == #* or: [aSymbol == #+]) ifTrue: [
       
   158             ^self perform: aSymbol with: aNumber
       
   159         ].
       
   160         (aSymbol == #/ and: [aNumber isFinite]) ifTrue: [^0].
       
   161         (aSymbol == #< and: [aNumber isFinite]) ifTrue: [^positive].
       
   162         (aSymbol == #> and: [ aNumber isFinite ]) ifTrue: [^positive not].
       
   163         (aSymbol == #= and: [ aNumber isFinite ])ifTrue: [ ^false ]. 
       
   164         self errorUndefinedResult: aSymbol
       
   165 ! !
       
   166 
       
   167 !Infinity methodsFor: 'printing'!
       
   168 
       
   169 printOn: aStream
       
   170         aStream nextPutAll: self class name.
       
   171         aStream nextPutAll:(positive
       
   172                                 ifTrue: [' positive']
       
   173                                 ifFalse: [' negative'])
       
   174 ! !
       
   175 
       
   176 !Infinity methodsFor: 'errors'!
       
   177 
       
   178 errorUndefinedResult: messageName
       
   179         self error: 'Undefined result in an Infinity ', messageName
       
   180 ! !
       
   181 
       
   182 !Infinity methodsFor: 'private'!
       
   183 
       
   184 setPositive: aBoolean
       
   185         positive _ aBoolean
       
   186 ! !
       
   187 
       
   188 !Infinity class methodsFor: 'class initialization'!
       
   189 
       
   190 initialize
       
   191         "Infinity initialize"
       
   192 
       
   193         PositiveInfinity _ self basicNew setPositive: true.
       
   194         NegativeInfinity _ self basicNew setPositive: false
       
   195 ! !
       
   196 
       
   197 !Infinity class methodsFor: 'instance creation'!
       
   198 
       
   199 negative
       
   200         "Return the unique instance of negative infinity"
       
   201 
       
   202         ^NegativeInfinity
       
   203 !
       
   204 
       
   205 new
       
   206         self shouldNotImplement
       
   207 !
       
   208 
       
   209 positive
       
   210         "Return the unique instance of positive infinity"
       
   211 
       
   212         ^PositiveInfinity
       
   213 ! !
       
   214 
       
   215 Infinity initialize!
       
   216 
       
   217 
       
   218 "COPYRIGHT.
       
   219  The above file is a Manchester Goodie protected by copyright.
       
   220  These conditions are imposed on the whole Goodie, and on any significant
       
   221  part of it which is separately transmitted or stored:
       
   222         * You must ensure that every copy includes this notice, and that
       
   223           source and author(s) of the material are acknowledged.
       
   224         * These conditions must be imposed on anyone who receives a copy.
       
   225         * The material shall not be used for commercial gain without the prior
       
   226           written consent of the author(s).
       
   227  Further information on the copyright conditions may be obtained by
       
   228  sending electronic mail:
       
   229         To: goodies-lib@cs.man.ac.uk
       
   230         Subject: copyright
       
   231  or by writing to The Smalltalk Goodies Library Manager, Dept of
       
   232  Computer Science, The University, Manchester M13 9PL, UK
       
   233 
       
   234  (C) Copyright 1992 University of Manchester
       
   235  For more information about the Manchester Goodies Library (from which 
       
   236  this file was distributed) send e-mail:
       
   237         To: goodies-lib@cs.man.ac.uk
       
   238         Subject: help 
       
   239 "!