author | Claus Gittinger <cg@exept.de> |
Fri, 16 Nov 2001 14:14:32 +0100 | |
changeset 6194 | e060dbb4f75c |
parent 6155 | 00eaf2fa55bc |
child 6195 | 9e60c38d1d61 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
5322 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
5360 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
ArithmeticValue subclass:#Number |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
16 |
instanceVariableNames:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
17 |
classVariableNames:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
18 |
poolDictionaries:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
19 |
category:'Magnitude-Numbers' |
1 | 20 |
! |
21 |
||
1892 | 22 |
!Number class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
5322 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
abstract superclass for all kinds of numbers |
|
1295 | 41 |
|
42 |
[author:] |
|
5322 | 43 |
Claus Gittinger |
1628 | 44 |
|
45 |
[see also:] |
|
5322 | 46 |
Integer LargeInteger SmallInteger |
47 |
LimitedPrecisionReal Float ShortFloat |
|
48 |
Fraction FixedPoint |
|
88 | 49 |
" |
50 |
! ! |
|
1 | 51 |
|
1892 | 52 |
!Number class methodsFor:'instance creation'! |
1 | 53 |
|
3060 | 54 |
fromString:aString |
55 |
"for compatibility with other smalltalks - same as #readFrom:" |
|
56 |
||
57 |
^ self readFrom:aString |
|
58 |
||
3716 | 59 |
" |
60 |
Number fromString:'12345' |
|
61 |
'12345' asNumber |
|
62 |
" |
|
63 |
||
64 |
"Modified: / 3.8.1998 / 20:05:11 / cg" |
|
3060 | 65 |
! |
66 |
||
67 |
fromString:aString onError:exceptionBlock |
|
68 |
"for compatibility with other smalltalks - same as #readFrom:" |
|
69 |
||
70 |
^ self readFrom:aString onError:exceptionBlock |
|
71 |
||
3716 | 72 |
" |
73 |
Number fromString:'12345' onError:0 |
|
74 |
Number fromString:'fooBarBaz' onError:0 |
|
75 |
" |
|
76 |
||
77 |
"Modified: / 3.8.1998 / 20:05:34 / cg" |
|
3060 | 78 |
! |
79 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
80 |
readFrom:aStringOrStream onError:exceptionBlock |
1 | 81 |
"return the next Number from the (character-)stream aStream; |
202 | 82 |
skipping all whitespace first; return the value of exceptionBlock, |
83 |
if no number can be read." |
|
1 | 84 |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
85 |
|value| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
86 |
|
973 | 87 |
ErrorSignal handle:[:ex | |
5322 | 88 |
^ exceptionBlock value |
339 | 89 |
] do:[ |
5322 | 90 |
|str nextChar radix negative signExp| |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1635
diff
changeset
|
91 |
|
5322 | 92 |
str := aStringOrStream readStream. |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1635
diff
changeset
|
93 |
|
5322 | 94 |
nextChar := str skipSeparators. |
95 |
nextChar isNil ifTrue:[^ exceptionBlock value]. |
|
3 | 96 |
|
5322 | 97 |
(nextChar == $-) ifTrue:[ |
98 |
negative := true. |
|
99 |
str next. |
|
100 |
nextChar := str peekOrNil |
|
101 |
] ifFalse:[ |
|
102 |
negative := false. |
|
103 |
(nextChar == $+) ifTrue:[ |
|
104 |
str next. |
|
105 |
nextChar := str peekOrNil |
|
106 |
] |
|
107 |
]. |
|
108 |
(nextChar isDigit or:[nextChar == $.]) ifFalse:[ |
|
109 |
^ exceptionBlock value. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
110 |
"/ value := super readFrom:str. |
339 | 111 |
"/ negative ifTrue:[value := value negated]. |
112 |
"/ ^ value |
|
5322 | 113 |
]. |
114 |
nextChar == $. ifTrue:[ |
|
115 |
radix := 10. |
|
116 |
value := 0.0. |
|
117 |
] ifFalse:[ |
|
118 |
value := Integer readFrom:str radix:10. |
|
119 |
nextChar := str peekOrNil. |
|
120 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
|
121 |
str next. |
|
122 |
radix := value. |
|
123 |
value := Integer readFrom:str radix:radix. |
|
124 |
] ifFalse:[ |
|
125 |
radix := 10 |
|
126 |
]. |
|
127 |
]. |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
128 |
|
5322 | 129 |
(nextChar == $.) ifTrue:[ |
130 |
str next. |
|
131 |
nextChar := str peekOrNil. |
|
132 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[ |
|
133 |
value := value asFloat |
|
134 |
+ (Number readMantissaFrom:str radix:radix). |
|
135 |
nextChar := str peekOrNil |
|
136 |
] |
|
137 |
]. |
|
138 |
((nextChar == $e) or:[nextChar == $E]) ifTrue:[ |
|
139 |
str next. |
|
140 |
nextChar := str peekOrNil. |
|
141 |
signExp := 1. |
|
142 |
(nextChar == $+) ifTrue:[ |
|
143 |
str next. |
|
144 |
nextChar := str peekOrNil. |
|
145 |
] ifFalse:[ |
|
146 |
(nextChar == $-) ifTrue:[ |
|
147 |
str next. |
|
148 |
nextChar := str peekOrNil. |
|
149 |
signExp := -1 |
|
150 |
] |
|
151 |
]. |
|
152 |
(nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[ |
|
153 |
value := value asFloat |
|
154 |
* (10.0 raisedToInteger: |
|
155 |
((Integer readFrom:str radix:radix) * signExp)) |
|
156 |
] |
|
157 |
]. |
|
158 |
negative ifTrue:[ |
|
159 |
value := value negated |
|
160 |
]. |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
161 |
]. |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
162 |
^ value. |
1 | 163 |
|
329 | 164 |
" |
165 |
Number readFrom:(ReadStream on:'54.32e-01') |
|
166 |
Number readFrom:(ReadStream on:'12345678901234567890') |
|
167 |
Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
168 |
Number readFrom:'16rAAAAFFFFAAAAFFFF' |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
169 |
Number readFrom:'0.000001' |
329 | 170 |
'+00000123.45' asNumber |
171 |
" |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
172 |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
173 |
"Modified: / 14.4.1998 / 19:22:50 / cg" |
296 | 174 |
! |
175 |
||
176 |
readSmalltalkSyntaxFrom:aStream |
|
177 |
"ST-80 compatibility (thanks to a note from alpha testers) |
|
178 |
read and return the next Number in smalltalk syntax from the |
|
329 | 179 |
(character-)stream aStream." |
180 |
||
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
181 |
^ Scanner scanNumberFrom:aStream |
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
182 |
"/ ^ Compiler evaluate:aStream compile:false "/ self readFrom:aStream. |
296 | 183 |
|
329 | 184 |
" |
185 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'54.32e-01') |
|
186 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'12345678901234567890') |
|
187 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
188 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'(1/10)') |
|
189 |
Number readFrom:(ReadStream on:'(1/10)') |
|
190 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'+00000123.45') |
|
191 |
Number readFrom:(ReadStream on:'+00000123.45') |
|
5013 | 192 |
|
193 |
|s| |
|
194 |
s := ReadStream on:'2.'. |
|
195 |
Number readSmalltalkSyntaxFrom:s. |
|
196 |
s next |
|
197 |
||
198 |
|s| |
|
199 |
s := ReadStream on:'2.0.'. |
|
200 |
Number readSmalltalkSyntaxFrom:s. |
|
201 |
s next |
|
329 | 202 |
" |
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
203 |
|
5013 | 204 |
"Modified: / 19.11.1999 / 18:26:47 / cg" |
1 | 205 |
! ! |
701 | 206 |
|
1892 | 207 |
!Number class methodsFor:'error reporting'! |
701 | 208 |
|
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
209 |
raise:aSignalSymbol receiver:someNumber selector:sel arg:arg errorString:text |
3394 | 210 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
211 |
||
4558 | 212 |
<context: #return> |
213 |
||
3394 | 214 |
|msg| |
215 |
||
216 |
msg := MessageSend |
|
5322 | 217 |
receiver:someNumber |
218 |
selector:sel |
|
219 |
arguments:(Array with:arg). |
|
3394 | 220 |
|
221 |
^ (self perform:aSignalSymbol) |
|
5322 | 222 |
raiseRequestWith:msg |
223 |
errorString:text |
|
224 |
in:thisContext sender |
|
3394 | 225 |
|
226 |
" |
|
227 |
Number |
|
5322 | 228 |
raise:#domainErrorSignal |
229 |
receiver:1.0 |
|
230 |
selector:#sin |
|
231 |
arg:nil |
|
232 |
errorString:'foo bar test' |
|
3394 | 233 |
" |
234 |
! |
|
235 |
||
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
236 |
raise:aSignalSymbol receiver:someNumber selector:sel arguments:argArray errorString:text |
701 | 237 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
238 |
||
4558 | 239 |
<context: #return> |
240 |
||
701 | 241 |
|msg| |
242 |
||
243 |
msg := MessageSend |
|
5322 | 244 |
receiver:someNumber |
245 |
selector:sel |
|
246 |
arguments:argArray. |
|
3394 | 247 |
|
701 | 248 |
^ (self perform:aSignalSymbol) |
5322 | 249 |
raiseRequestWith:msg |
250 |
errorString:text |
|
251 |
in:thisContext sender |
|
701 | 252 |
|
253 |
" |
|
3394 | 254 |
Number |
5322 | 255 |
raise:#domainErrorSignal |
256 |
receiver:1.0 |
|
257 |
selector:#sin |
|
258 |
arg:nil |
|
259 |
errorString:'foo bar test' |
|
701 | 260 |
" |
261 |
! |
|
262 |
||
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
263 |
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel errorString:text |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
264 |
"ST-80 compatible signal raising. Provided for PD numeric classes. |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
265 |
aSignalSymbolOrErrorClass is either an Error-subclass, or |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
266 |
the selector which is sent to myself, to retrieve the Exception class / Signal." |
701 | 267 |
|
4558 | 268 |
<context: #return> |
269 |
||
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
270 |
|msg signalOrException| |
701 | 271 |
|
272 |
msg := MessageSend |
|
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
273 |
receiver:someNumber |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
274 |
selector:sel |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
275 |
arguments:#(). |
3394 | 276 |
|
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
277 |
aSignalSymbolOrErrorClass isSymbol ifTrue:[ |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
278 |
signalOrException := self perform:aSignalSymbolOrErrorClass. |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
279 |
] ifFalse:[ |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
280 |
signalOrException := aSignalSymbolOrErrorClass. "/ assume its an Error-Subclass |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
281 |
]. |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
282 |
|
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
283 |
^ signalOrException |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
284 |
raiseRequestWith:msg |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
285 |
errorString:text |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
286 |
in:thisContext sender |
701 | 287 |
|
288 |
" |
|
3394 | 289 |
Number |
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
290 |
raise:#domainErrorSignal |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
291 |
receiver:1.0 |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
292 |
selector:#foo |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
293 |
errorString:'foo bar test' |
701 | 294 |
" |
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
295 |
|
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
296 |
"Modified: / 16.11.2001 / 14:04:15 / cg" |
701 | 297 |
! ! |
298 |
||
1892 | 299 |
!Number class methodsFor:'private'! |
701 | 300 |
|
301 |
readMantissaFrom:aStream radix:radix |
|
302 |
"helper for readFrom: - |
|
303 |
return the mantissa from the (character-)stream aStream; |
|
304 |
no whitespace-skipping; error if no number available" |
|
305 |
||
306 |
|nextChar value factor| |
|
307 |
||
308 |
value := 0.0. |
|
309 |
factor := 1.0 / radix. |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
310 |
nextChar := aStream peekOrNil. |
701 | 311 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
5322 | 312 |
value := value + (nextChar digitValue * factor). |
313 |
factor := factor / radix. |
|
314 |
aStream next. |
|
315 |
nextChar := aStream peekOrNil |
|
701 | 316 |
]. |
317 |
^ value |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
318 |
|
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
319 |
"Modified: / 14.4.1998 / 18:47:47 / cg" |
701 | 320 |
! ! |
321 |
||
4988 | 322 |
!Number methodsFor:'Compatibility - Squeak'! |
323 |
||
324 |
asSmallAngleDegrees |
|
325 |
"Return the receiver normalized to lie within the range (-180, 180)" |
|
326 |
||
327 |
| pos | |
|
328 |
pos _ self \\ 360. |
|
329 |
pos > 180 ifTrue: [pos _ pos - 360]. |
|
330 |
^ pos |
|
331 |
||
6072 | 332 |
"#(-500 -300 -150 -5 0 5 150 300 500 1200) collect: [:n | n asSmallAngleDegrees]" |
333 |
! |
|
4988 | 334 |
|
6143 | 335 |
closeTo:num |
336 |
"are these two numbers close?" |
|
337 |
||
338 |
| ans | |
|
339 |
||
340 |
self = num ifTrue:[^ true]. |
|
341 |
||
342 |
num isLimitedPrecisionReal "isReal" ifTrue: [^ num closeTo: self asFloat]. |
|
343 |
[ans := (self = num)] ifError: [:aString :aReceiver | ^ false]. |
|
344 |
^ ans |
|
345 |
||
346 |
" |
|
347 |
1 closeTo:1.0000000001 |
|
348 |
1 closeTo:1.001 |
|
349 |
" |
|
6155 | 350 |
|
351 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
6143 | 352 |
! |
353 |
||
4988 | 354 |
newTileMorphRepresentative |
355 |
^ TileMorph new addArrows; setLiteral: self; addSuffixIfCan |
|
356 |
! |
|
357 |
||
358 |
stringForReadout |
|
359 |
^ self rounded printString |
|
360 |
! ! |
|
361 |
||
5238 | 362 |
!Number methodsFor:'coercing & converting'! |
701 | 363 |
|
364 |
coerce:aNumber |
|
365 |
"return aNumber converted into receivers type" |
|
366 |
||
367 |
^ self subclassResponsibility |
|
368 |
! |
|
369 |
||
370 |
generality |
|
371 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
372 |
||
373 |
^ 40 |
|
374 |
! ! |
|
375 |
||
376 |
!Number methodsFor:'converting'! |
|
377 |
||
3634 | 378 |
% aNumber |
379 |
"Return a complex number with the receiver as the real part and |
|
380 |
aNumber as the imaginary part" |
|
381 |
||
382 |
^ Complex real:self imaginary:aNumber |
|
383 |
||
384 |
"Modified: / 9.7.1998 / 10:18:12 / cg" |
|
385 |
! |
|
386 |
||
701 | 387 |
@ aNumber |
388 |
"return a Point with the receiver as x-coordinate and the argument |
|
389 |
as y-coordinate" |
|
390 |
||
391 |
%{ /* NOCONTEXT */ |
|
392 |
||
393 |
/* |
|
394 |
* I cannot tell if this special code is worth anything |
|
395 |
*/ |
|
5322 | 396 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
397 |
OBJ newPoint; |
|
398 |
int spc; |
|
701 | 399 |
|
5322 | 400 |
__qCheckedAlignedNew(newPoint, sizeof(struct __Point)); |
401 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
402 |
__PointInstPtr(newPoint)->p_x = self; |
|
403 |
__PointInstPtr(newPoint)->p_y = aNumber; |
|
404 |
if (! __bothSmallInteger(self, aNumber)) { |
|
405 |
spc = __qSpace(newPoint); |
|
406 |
__STORE_SPC(newPoint, aNumber, spc); |
|
407 |
__STORE_SPC(newPoint, self, spc); |
|
408 |
} |
|
409 |
RETURN ( newPoint ); |
|
701 | 410 |
} |
411 |
%} |
|
412 |
. |
|
413 |
^ Point x:self y:aNumber |
|
414 |
! |
|
415 |
||
3634 | 416 |
asComplex |
417 |
"Return a complex number with the receiver as the real part and |
|
418 |
zero as the imaginary part" |
|
419 |
||
420 |
^ Complex fromReal:self |
|
421 |
||
422 |
"Modified: / 9.7.1998 / 10:18:16 / cg" |
|
423 |
! |
|
424 |
||
701 | 425 |
asPoint |
426 |
"return a new Point with the receiver as all coordinates; |
|
427 |
often used to supply the same value in two dimensions, as with |
|
428 |
symmetrical gridding or scaling." |
|
429 |
||
430 |
%{ /* NOCONTEXT */ |
|
431 |
||
5322 | 432 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
433 |
OBJ newPoint; |
|
701 | 434 |
|
5322 | 435 |
__qCheckedAlignedNew(newPoint, sizeof(struct __Point)); |
436 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
437 |
__PointInstPtr(newPoint)->p_x = self; |
|
438 |
__PointInstPtr(newPoint)->p_y = self; |
|
439 |
__STORE(newPoint, self); |
|
440 |
RETURN ( newPoint ); |
|
701 | 441 |
} |
442 |
%}. |
|
443 |
^ Point x:self y:self |
|
444 |
! |
|
445 |
||
2416 | 446 |
decodeAsLiteralArray |
447 |
"given a literalEncoding in the receiver, |
|
448 |
create & return the corresponding object. |
|
449 |
The inverse operation to #literalArrayEncoding." |
|
450 |
||
451 |
^ self |
|
452 |
||
453 |
"Created: 25.2.1997 / 19:17:06 / cg" |
|
454 |
"Modified: 25.2.1997 / 19:17:42 / cg" |
|
455 |
! |
|
456 |
||
701 | 457 |
degreesToRadians |
458 |
"interpreting the receiver as radians, return the degrees" |
|
459 |
||
460 |
^ (self * (Float pi)) / 180.0 |
|
461 |
! |
|
462 |
||
463 |
literalArrayEncoding |
|
1245 | 464 |
"encode myself as an array literal, from which a copy of the receiver |
465 |
can be reconstructed with #decodeAsLiteralArray." |
|
701 | 466 |
|
467 |
^ self |
|
468 |
||
469 |
"Modified: 1.9.1995 / 02:25:26 / claus" |
|
1245 | 470 |
"Modified: 22.4.1996 / 13:00:27 / cg" |
701 | 471 |
! |
472 |
||
473 |
radiansToDegrees |
|
474 |
"interpreting the receiver as degrees, return the radians" |
|
475 |
||
476 |
^ (self * 180.0) / (Float pi) |
|
477 |
! ! |
|
478 |
||
479 |
!Number methodsFor:'intervals'! |
|
480 |
||
6072 | 481 |
downTo:stop |
482 |
"return an interval from receiver down to the argument, incrementing by -1" |
|
483 |
||
484 |
^ self to:stop by:-1 |
|
485 |
||
486 |
" |
|
487 |
(10 downTo:1) do:[:i | Transcript showCR:i]. |
|
488 |
" |
|
489 |
! |
|
490 |
||
701 | 491 |
to:stop |
492 |
"return an interval from receiver up to the argument, incrementing by 1" |
|
493 |
||
494 |
^ Interval from:self to:stop |
|
495 |
! |
|
496 |
||
497 |
to:stop by:step |
|
498 |
"return an interval from receiver up to the argument, incrementing by step" |
|
499 |
||
500 |
^ Interval from:self to:stop by:step |
|
6154 | 501 |
! |
502 |
||
503 |
to:stop byFactor:factor |
|
504 |
"return a geometric series from receiver up to the argument; |
|
505 |
elements have a constant factor in between" |
|
506 |
||
507 |
^ GeometricSeries from:self to:stop byFactor:factor |
|
508 |
||
509 |
" |
|
510 |
(1 to:256 byFactor:2) |
|
511 |
(256 to:1 byFactor:1/2) |
|
512 |
" |
|
701 | 513 |
! ! |
514 |
||
515 |
!Number methodsFor:'iteration'! |
|
516 |
||
517 |
timesRepeat:aBlock |
|
518 |
"evaluate the argument, aBlock self times" |
|
519 |
||
520 |
|count| |
|
521 |
||
522 |
count := self. |
|
523 |
[count > 0] whileTrue:[ |
|
5322 | 524 |
aBlock value. |
525 |
count := count - 1 |
|
701 | 526 |
] |
527 |
! ! |
|
528 |
||
3634 | 529 |
!Number methodsFor:'mathematical functions'! |
530 |
||
531 |
conjugated |
|
532 |
"Return the complex conjugate of this Number." |
|
533 |
||
534 |
^ self |
|
535 |
||
536 |
"Modified: / 9.7.1998 / 10:17:31 / cg" |
|
537 |
! |
|
538 |
||
539 |
imaginary |
|
540 |
"Return the imaginary part of this Number." |
|
541 |
||
542 |
^ 0 |
|
543 |
||
544 |
"Modified: / 9.7.1998 / 10:17:24 / cg" |
|
545 |
! |
|
546 |
||
547 |
real |
|
548 |
"Return the real part of this Number." |
|
549 |
||
550 |
^ self |
|
551 |
||
552 |
"Modified: / 9.7.1998 / 10:17:17 / cg" |
|
553 |
! ! |
|
554 |
||
701 | 555 |
!Number methodsFor:'printing & storing'! |
556 |
||
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
557 |
printOn:aStream paddedWith:padCharacter to:size base:radix |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
558 |
|s| |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
559 |
|
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
560 |
s := self printStringRadix:radix. |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
561 |
s printOn: aStream leftPaddedTo:size with: padCharacter |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
562 |
|
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
563 |
|
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
564 |
! |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
565 |
|
701 | 566 |
storeOn:aStream |
567 |
"append a string for storing the receiver onto the argument, |
|
568 |
aStream - since numbers are literals,they store as they print." |
|
569 |
||
570 |
^ self printOn:aStream |
|
571 |
! |
|
572 |
||
573 |
storeString |
|
574 |
"return a string for storing |
|
575 |
- since numbers are literals, they store as they print." |
|
576 |
||
577 |
^ self printString |
|
578 |
! ! |
|
579 |
||
580 |
!Number methodsFor:'testing'! |
|
581 |
||
5360 | 582 |
isDivisibleBy:aNumber |
583 |
"return true, if the receiver can be divided by the argument, aNumber without a remainder. |
|
584 |
Notice, that the result is only worth trusting, if the receiver is an integer." |
|
585 |
||
586 |
aNumber = 0 ifTrue: [^ false]. |
|
587 |
aNumber isInteger ifFalse: [^ false]. |
|
588 |
^ (self \\ aNumber) = 0 |
|
589 |
||
590 |
" |
|
591 |
3 isDivisibleBy:2 |
|
592 |
4 isDivisibleBy:2 |
|
593 |
4.0 isDivisibleBy:2 |
|
594 |
4.5 isDivisibleBy:4.5 |
|
595 |
4.5 isDivisibleBy:1.0 |
|
596 |
" |
|
597 |
! |
|
598 |
||
6143 | 599 |
isInfinite |
600 |
||
601 |
^ false |
|
6155 | 602 |
|
603 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
6143 | 604 |
! |
605 |
||
606 |
isNaN |
|
607 |
"return true, if the receiver is an invalid float (NaN - not a number)." |
|
608 |
||
609 |
^ false |
|
6155 | 610 |
|
611 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
6143 | 612 |
! |
613 |
||
701 | 614 |
isNumber |
615 |
"return true, if the receiver is a kind of number" |
|
616 |
||
617 |
^ true |
|
618 |
! |
|
619 |
||
6087 | 620 |
isReal |
621 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
622 |
true is returned here - the method is redefined from Object." |
|
623 |
||
624 |
^ true |
|
625 |
! |
|
626 |
||
701 | 627 |
isZero |
1557 | 628 |
"return true, if the receiver is zero" |
629 |
||
701 | 630 |
^ self = 0 |
1557 | 631 |
|
632 |
"Modified: 18.7.1996 / 12:40:49 / cg" |
|
701 | 633 |
! ! |
634 |
||
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
635 |
!Number methodsFor:'tracing'! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
636 |
|
4682 | 637 |
traceInto:aRequestor level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
638 |
"double dispatch into tracer, passing my type implicitely in the selector" |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
639 |
|
4682 | 640 |
^ aRequestor traceNumber:self level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
641 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
642 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
643 |
! ! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
644 |
|
5552 | 645 |
!Number methodsFor:'truncation & rounding'! |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
646 |
|
4895 | 647 |
detentBy: detent atMultiplesOf: grid snap: snap |
648 |
"Map all values that are within detent/2 of any multiple of grid |
|
649 |
to that multiple. |
|
650 |
Otherwise, if snap is true, return self, meaning that the values |
|
651 |
in the dead zone will never be returned. |
|
652 |
If snap is false, then expand the range between dead zones |
|
653 |
so that it covers the range between multiples of the grid, |
|
654 |
and scale the value by that factor." |
|
655 |
||
656 |
| r1 r2 | |
|
657 |
||
658 |
r1 := self roundTo: grid. "Nearest multiple of grid" |
|
659 |
(self roundTo: detent) = r1 ifTrue: [^ r1]. "Snap to that multiple..." |
|
660 |
snap ifTrue: [^ self]. "...or return self" |
|
661 |
||
662 |
r2 := self < r1 "Nearest end of dead zone" |
|
5322 | 663 |
ifTrue: [r1 - (detent asFloat/2)] |
664 |
ifFalse: [r1 + (detent asFloat/2)]. |
|
4895 | 665 |
|
666 |
"Scale values between dead zones to fill range between multiples" |
|
667 |
^ r1 + ((self - r2) * grid asFloat / (grid - detent)) |
|
668 |
||
669 |
" |
|
670 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: true] |
|
671 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: false] |
|
672 |
(3.9 to: 4.1 by: 0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: true] |
|
673 |
(-3.9 to: -4.1 by: -0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: false] |
|
674 |
" |
|
675 |
! |
|
676 |
||
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
677 |
fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
678 |
"return a float with value from digits after the decimal point. |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
679 |
(i.e. the receiver minus its truncated value)" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
680 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
681 |
^ self - self truncated asFloat |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
682 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
683 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
684 |
1234.56789 fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
685 |
1.2345e6 fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
686 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
687 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
688 |
"Modified: / 4.11.1996 / 20:26:54 / cg" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
689 |
"Created: / 28.10.1998 / 17:14:40 / cg" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
690 |
! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
691 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
692 |
integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
693 |
"return a float with value from digits before the decimal point |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
694 |
(i.e. the truncated value)" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
695 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
696 |
^ self truncated asFloat |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
697 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
698 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
699 |
1234.56789 integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
700 |
1.2345e6 integerPart |
6154 | 701 |
12.5 integerPart |
702 |
-12.5 integerPart |
|
703 |
(5/3) integerPart |
|
704 |
(-5/3) integerPart |
|
705 |
(5/3) truncated |
|
706 |
(-5/3) truncated |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
707 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
708 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
709 |
"Created: / 28.10.1998 / 17:14:56 / cg" |
6154 | 710 |
"Modified: / 5.11.2001 / 17:54:22 / cg" |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
711 |
! ! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
712 |
|
1892 | 713 |
!Number class methodsFor:'documentation'! |
701 | 714 |
|
715 |
version |
|
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
716 |
^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.68 2001-11-16 13:14:32 cg Exp $' |
701 | 717 |
! ! |