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