Number.st
changeset 3 24d81bf47225
parent 2 6526dde5f3ac
child 5 67342904af11
equal deleted inserted replaced
2:6526dde5f3ac 3:24d81bf47225
    22 COPYRIGHT (c) 1988-93 by Claus Gittinger
    22 COPYRIGHT (c) 1988-93 by Claus Gittinger
    23               All Rights Reserved
    23               All Rights Reserved
    24 
    24 
    25 abstract superclass for all kinds of numbers
    25 abstract superclass for all kinds of numbers
    26 
    26 
    27 %W% %E%
    27 $Header: /cvs/stx/stx/libbasic/Number.st,v 1.3 1993-10-13 00:16:45 claus Exp $
    28 '!
    28 '!
    29 
    29 
    30 ! Number methodsFor:'converting' !
    30 ! Number methodsFor:'converting' !
    31 
    31 
    32 @ aNumber
    32 @ aNumber
    43         _qCheckedAlignedNew(newPoint, sizeof(struct point), __context);
    43         _qCheckedAlignedNew(newPoint, sizeof(struct point), __context);
    44         _InstPtr(newPoint)->o_class = Point;
    44         _InstPtr(newPoint)->o_class = Point;
    45         _PointInstPtr(newPoint)->p_x = self;
    45         _PointInstPtr(newPoint)->p_x = self;
    46         _PointInstPtr(newPoint)->p_y = aNumber;
    46         _PointInstPtr(newPoint)->p_y = aNumber;
    47         /*
    47         /*
    48 	 * no store check needed for self: self is a number, new object is definitely in newSpace
    48          * no store check needed for self: self is a number, new object is definitely in newSpace
    49 	 * however, argument may be a context/block
    49          * however, argument may be a context/block
    50 	 */
    50          */
    51 	__STORE(newPoint, aNumber);
    51         __STORE(newPoint, aNumber);
    52         RETURN ( newPoint );
    52         RETURN ( newPoint );
    53     }
    53     }
    54 %}
    54 %}
    55 .
    55 .
    56     ^ Point x:self y:aNumber
    56     ^ Point x:self y:aNumber
    71         _qCheckedAlignedNew(newPoint, sizeof(struct point), __context);
    71         _qCheckedAlignedNew(newPoint, sizeof(struct point), __context);
    72         _InstPtr(newPoint)->o_class = Point;
    72         _InstPtr(newPoint)->o_class = Point;
    73         _PointInstPtr(newPoint)->p_x = self;
    73         _PointInstPtr(newPoint)->p_x = self;
    74         _PointInstPtr(newPoint)->p_y = self;
    74         _PointInstPtr(newPoint)->p_y = self;
    75         /* 
    75         /* 
    76 	 * no store check needed - its definitely in newSpace
    76          * no store check needed - its definitely in newSpace
    77 	 * and self is a number
    77          * and self is a number
    78          */
    78          */
    79         RETURN ( newPoint );
    79         RETURN ( newPoint );
    80     }
    80     }
    81 %}
    81 %}
    82 .
    82 .
   166         count := count - 1
   166         count := count - 1
   167     ]
   167     ]
   168 !
   168 !
   169 
   169 
   170 to:stop do:aBlock
   170 to:stop do:aBlock
   171     "create an interval from the receiver up to the argument, incrementing by 1.
   171     "For each element of the interval from the receiver up to the argument stop,
   172      For each element of the interval, evaluate aBlock"
   172      evaluate aBlock, passing the number as argument."
   173 
   173 
   174     |tmp|
   174     |tmp|
   175 
   175 
   176     tmp := self.
   176     tmp := self.
   177     [tmp <= stop] whileTrue:[
   177     [tmp <= stop] whileTrue:[
   179         tmp := tmp+1
   179         tmp := tmp+1
   180     ]
   180     ]
   181 !
   181 !
   182 
   182 
   183 to:stop by:incr do:aBlock
   183 to:stop by:incr do:aBlock
   184     "create an interval from the receiver up to the argument stop, incrementing
   184     "For each element of the interval from the receiver up to the argument stop, incrementing
   185      by step. For each element of the interval, evaluate aBlock"
   185      by step, evaluate aBlock passing the element as argument."
   186 
   186 
   187     |tmp|
   187     |tmp|
   188 
   188 
   189     tmp := self.
   189     tmp := self.
   190     (incr > 0) ifTrue:[
   190     (incr > 0) ifTrue:[
   227      skipping all whitespace first; return nil if no number"
   227      skipping all whitespace first; return nil if no number"
   228 
   228 
   229     |nextChar radix value negative signExp|
   229     |nextChar radix value negative signExp|
   230 
   230 
   231     nextChar := aStream skipSeparators.
   231     nextChar := aStream skipSeparators.
       
   232 
       
   233     nextChar isNil ifTrue:[
       
   234         "what should be returned at end-of-input ?"
       
   235         ^ nil
       
   236     ].
       
   237 
   232     (nextChar == $-) ifTrue:[
   238     (nextChar == $-) ifTrue:[
   233         negative := true.
   239         negative := true.
   234         nextChar := aStream nextPeek
   240         nextChar := aStream nextPeek
   235     ] ifFalse:[
   241     ] ifFalse:[
   236         negative := false
   242         negative := false
   278     negative ifTrue:[
   284     negative ifTrue:[
   279         ^ value negated
   285         ^ value negated
   280     ].
   286     ].
   281     ^ value
   287     ^ value
   282 
   288 
   283     "Number readFromString:'54.32e-01'"
   289     "Number readFrom:(ReadStream on:'54.32e-01')"
   284     "Number readFromString:'12345678901234567890'"
   290     "Number readFrom:(ReadStream on:'12345678901234567890')"
   285     "Number readFromString:'16rAAAAFFFFAAAAFFFF'"
   291     "Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF')"
   286 ! !
   292     "Number readFrom:(ReadStream on:'(1/10)')"
       
   293 ! !