author | sr |
Tue, 17 Dec 2019 14:43:08 +0100 | |
changeset 25147 | c5466c435143 |
parent 25144 | 03ca574d45c2 |
child 25148 | 03ac6dae4ff8 |
permissions | -rw-r--r-- |
25147 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1988 by Claus Gittinger |
20308 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5360 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17469 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
ArithmeticValue subclass:#Number |
18850 | 19 |
instanceVariableNames:'' |
18862 | 20 |
classVariableNames:'DecimalPointCharacterForPrinting DecimalPointCharactersForReading |
21 |
DefaultDisplayRadix' |
|
18850 | 22 |
poolDictionaries:'' |
23 |
category:'Magnitude-Numbers' |
|
1 | 24 |
! |
25 |
||
1892 | 26 |
!Number class methodsFor:'documentation'! |
88 | 27 |
|
28 |
copyright |
|
29 |
" |
|
30 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
20308 | 31 |
All Rights Reserved |
1 | 32 |
|
88 | 33 |
This software is furnished under a license and may be used |
34 |
only in accordance with the terms of that license and with the |
|
35 |
inclusion of the above copyright notice. This software may not |
|
36 |
be provided or otherwise made available to, or used by, any |
|
37 |
other person. No title to or ownership of the software is |
|
38 |
hereby transferred. |
|
39 |
" |
|
40 |
! |
|
1 | 41 |
|
88 | 42 |
documentation |
43 |
" |
|
44 |
abstract superclass for all kinds of numbers |
|
1295 | 45 |
|
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
46 |
[class variables:] |
20308 | 47 |
DecimalPointCharacterForPrinting <Character> used when printing |
48 |
DecimalPointCharactersForReading <Collection of Character> accepted as decimalPointChars when reading |
|
49 |
||
50 |
DefaultDisplayRadix the radix in which integers present their |
|
51 |
displayString (which is used in inspectors) |
|
52 |
If you are to look at many hex numbers, bitmasks |
|
53 |
etc. you may set this to 2 or 16. |
|
54 |
(avoids typing printStringRadix:.. all the time |
|
55 |
- I know - I am lazy ;-). Default is 10. |
|
56 |
||
1295 | 57 |
[author:] |
20308 | 58 |
Claus Gittinger |
1628 | 59 |
|
60 |
[see also:] |
|
20308 | 61 |
Integer LargeInteger SmallInteger |
62 |
LimitedPrecisionReal Float ShortFloat |
|
63 |
Fraction FixedPoint |
|
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
1892 | 67 |
!Number class methodsFor:'instance creation'! |
1 | 68 |
|
21871 | 69 |
coerce:aNumber |
70 |
"convert the argument aNumber into an instance of the receiver (class) and return it." |
|
71 |
||
72 |
self == Number ifTrue:[ |
|
73 |
self assert:(aNumber isNumber). |
|
74 |
^ aNumber |
|
75 |
]. |
|
76 |
^ super coerce:aNumber |
|
77 |
||
78 |
"Created: / 21-06-2017 / 08:58:38 / cg" |
|
79 |
! |
|
80 |
||
11944 | 81 |
fastFromString:aString |
82 |
"return the next Float, Integer or ShortFloat from the string. |
|
83 |
No spaces are skipped. |
|
84 |
||
85 |
This is a specially tuned entry (using a low-level C-call), which |
|
86 |
returns garbage if the argument string is not a valid float number. |
|
87 |
It has been added to allow high speed string decomposition into numbers, |
|
88 |
especially for mass-data." |
|
89 |
||
90 |
^ self fastFromString:aString at:1 |
|
91 |
||
92 |
" |
|
93 |
Float fromString:'12345.0' |
|
94 |
Float fastFromString:'12345.0' |
|
95 |
||
96 |
Integer fromString:'12345' |
|
97 |
Integer fastFromString:'12345' |
|
98 |
||
99 |
should be roughly 10times faster than the general method: |
|
100 |
||
101 |
Time millisecondsToRun:[ |
|
20308 | 102 |
100000 timesRepeat:[ Float fromString:'12345.0' ] |
103 |
]. |
|
11944 | 104 |
Time millisecondsToRun:[ |
20308 | 105 |
100000 timesRepeat:[ Float fastFromString:'12345.0' ] |
106 |
]. |
|
11944 | 107 |
|
108 |
Time millisecondsToRun:[ |
|
20308 | 109 |
100000 timesRepeat:[ Integer fromString:'12345' ] |
110 |
]. |
|
11944 | 111 |
Time millisecondsToRun:[ |
20308 | 112 |
100000 timesRepeat:[ Integer fastFromString:'12345' ] |
113 |
]. |
|
11944 | 114 |
" |
115 |
! |
|
116 |
||
24449 | 117 |
fastFromString:aString at:startIndex |
118 |
"return the next Float, Integer or ShortFloat from the string. |
|
119 |
No spaces are skipped. |
|
120 |
||
121 |
This is a specially tuned entry (using a low-level C-call), which |
|
122 |
returns garbage if the argument string is not a valid float number. |
|
123 |
It has been added to allow high speed string decomposition into numbers, |
|
124 |
especially for mass-data." |
|
125 |
||
126 |
self subclassResponsibility |
|
127 |
||
128 |
" |
|
129 |
Float fromString:'12345.0' |
|
130 |
Float fastFromString:'12345.0' |
|
131 |
||
132 |
Integer fromString:'12345' |
|
133 |
Integer fastFromString:'12345' |
|
134 |
||
135 |
should be roughly 10times faster than the general method: |
|
136 |
||
137 |
Time millisecondsToRun:[ |
|
138 |
100000 timesRepeat:[ Float fromString:'12345.0' ] |
|
139 |
]. |
|
140 |
Time millisecondsToRun:[ |
|
141 |
100000 timesRepeat:[ Float fastFromString:'12345.0' ] |
|
142 |
]. |
|
143 |
||
144 |
Time millisecondsToRun:[ |
|
145 |
100000 timesRepeat:[ Integer fromString:'12345' ] |
|
146 |
]. |
|
147 |
Time millisecondsToRun:[ |
|
148 |
100000 timesRepeat:[ Integer fastFromString:'12345' ] |
|
149 |
]. |
|
150 |
" |
|
151 |
||
152 |
"Created: / 21-07-2019 / 19:17:17 / Claus Gittinger" |
|
153 |
! |
|
154 |
||
21871 | 155 |
fromNumber:aNumber |
156 |
"return aNumber coerced to myself" |
|
157 |
||
158 |
self isAbstract ifTrue:[^ aNumber]. |
|
159 |
^ self coerce:aNumber |
|
160 |
||
161 |
"Created: / 21-06-2017 / 08:57:00 / cg" |
|
162 |
! |
|
163 |
||
3060 | 164 |
fromString:aString |
17056 | 165 |
"return a number by reading from aString. |
17057 | 166 |
In contrast to readFrom:, no garbage is allowed after the number. |
17056 | 167 |
I.e. the string must contain exactly one valid number (with optional separators around)" |
3060 | 168 |
|
9333 | 169 |
^ self |
20308 | 170 |
fromString:aString |
171 |
decimalPointCharacters:(self decimalPointCharactersForReading) |
|
9333 | 172 |
|
173 |
" |
|
174 |
Number fromString:'12345' |
|
175 |
Number fromString:'abc' |
|
17056 | 176 |
Number fromString:'1abc' -> raises an error |
20308 | 177 |
Number readFrom:'1abc' -> reads a 1 |
178 |
Number readFrom:'10/2' -> reads a 10 |
|
179 |
Number fromString:'10/2' -> raises an error |
|
180 |
Number fromString:'(1/2)' -> reads a fraction |
|
181 |
Number readFrom:'(1/2)' -> reads a fraction |
|
182 |
Number readFrom:'(10/2)' -> reads a 5 |
|
9333 | 183 |
'12345' asNumber |
184 |
" |
|
185 |
||
186 |
"Modified: / 3.8.1998 / 20:05:11 / cg" |
|
187 |
! |
|
188 |
||
24448 | 189 |
fromString:aString decimalPointCharacter:decimalPointCharacter |
190 |
"return a number by reading from aString. |
|
191 |
In contrast to readFrom:, no garbage is allowed after the number. |
|
192 |
I.e. the string must contain exactly one valid number (with optional separators around)" |
|
193 |
||
194 |
^ self fromString:aString decimalPointCharacters:(decimalPointCharacter asString) |
|
195 |
||
196 |
" |
|
197 |
Number fromString:'12345.99' decimalPointCharacter:$. |
|
198 |
Number fromString:'12345,99' decimalPointCharacter:$, |
|
199 |
" |
|
200 |
||
201 |
"Created: / 21-07-2019 / 13:09:13 / Claus Gittinger" |
|
202 |
"Modified (comment): / 21-07-2019 / 18:04:30 / Claus Gittinger" |
|
203 |
! |
|
204 |
||
205 |
fromString:aString decimalPointCharacter:decimalPointCharacter onError:exceptionBlock |
|
206 |
"return a number by reading from aString. |
|
207 |
In contrast to readFrom:, no garbage is allowed after the number. |
|
208 |
I.e. the string must contain exactly one valid number (with optional separators around)" |
|
209 |
||
210 |
^ self |
|
211 |
fromString:aString |
|
212 |
decimalPointCharacters:(decimalPointCharacter asString) |
|
213 |
onError:exceptionBlock. |
|
214 |
||
215 |
" |
|
216 |
Number fromString:'12,345' decimalPointCharacter:',' onError:0 |
|
217 |
Number fromString:'12,345' decimalPointCharacter:$, onError:0 |
|
218 |
Number fromString:'12,345,456' decimalPointCharacter:$. thouseandsSeparator:$, onError:0 |
|
219 |
Number fromString:'12,345,45' decimalPointCharacter:$. thouseandsSeparator:$, onError:0 |
|
220 |
" |
|
221 |
||
222 |
"Created: / 21-07-2019 / 13:07:39 / Claus Gittinger" |
|
223 |
"Modified (comment): / 21-07-2019 / 18:50:18 / Claus Gittinger" |
|
224 |
! |
|
225 |
||
226 |
fromString:aString decimalPointCharacter:decimalPointCharacter thousandsSeparator:thousandsSeparator onError:exceptionBlock |
|
227 |
"return a number by reading from aString. |
|
228 |
In contrast to readFrom:, no garbage is allowed after the number. |
|
229 |
I.e. the string must contain exactly one valid number (with optional separators around)" |
|
230 |
||
231 |
^ self |
|
232 |
fromString:aString |
|
233 |
decimalPointCharacters:(decimalPointCharacter asString) |
|
234 |
thousandsSeparator:thousandsSeparator |
|
235 |
onError:exceptionBlock. |
|
236 |
||
237 |
" |
|
238 |
Number fromString:'12,345' decimalPointCharacter:',' onError:0 |
|
239 |
Number fromString:'12,345' decimalPointCharacter:$, onError:0 |
|
240 |
Number fromString:'12345,456' decimalPointCharacter:$. thousandsSeparator:$, onError:0 |
|
241 |
Number fromString:'12,345,456' decimalPointCharacter:$. thousandsSeparator:$, onError:0 |
|
242 |
Number fromString:'12,345,456,789' decimalPointCharacter:$. thousandsSeparator:$, onError:0 |
|
243 |
Number fromString:'12,345,456,789.89' decimalPointCharacter:$. thousandsSeparator:$, onError:0 |
|
244 |
Number fromString:'12.345.456.789,89' decimalPointCharacter:$, thousandsSeparator:$. onError:0 |
|
245 |
||
246 |
these report an error: |
|
247 |
Number fromString:'12,345,45' decimalPointCharacter:$. thousandsSeparator:$, onError:0 |
|
248 |
Number fromString:'12.345.456.789' decimalPointCharacter:$. thousandsSeparator:$, onError:0 |
|
249 |
Number fromString:'12.345.456.78' decimalPointCharacter:$. thousandsSeparator:$, onError:0 |
|
250 |
" |
|
251 |
||
252 |
"Created: / 21-07-2019 / 18:50:57 / Claus Gittinger" |
|
253 |
! |
|
254 |
||
9333 | 255 |
fromString:aString decimalPointCharacters:decimalPointCharacters |
17056 | 256 |
"return a number by reading from aString. |
17057 | 257 |
In contrast to readFrom:, no garbage is allowed after the number. |
17056 | 258 |
I.e. the string must contain exactly one valid number (with optional separators around)" |
9333 | 259 |
|
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
260 |
|s num| |
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
261 |
|
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
262 |
s := aString readStream. |
24448 | 263 |
num := self |
264 |
readFrom:s |
|
265 |
decimalPointCharacters:decimalPointCharacters |
|
266 |
onError:[^ ConversionError raiseRequestErrorString:' - invalid number']. |
|
17056 | 267 |
s atEnd ifFalse:[ |
24448 | 268 |
s skipSeparators. |
269 |
s atEnd ifFalse:[ |
|
270 |
^ ConversionError raiseRequestErrorString:' - garbage at end of number' |
|
271 |
]. |
|
17056 | 272 |
]. |
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
273 |
^ num. |
3060 | 274 |
|
3716 | 275 |
" |
276 |
Number fromString:'12345' |
|
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
277 |
Number fromString:'abc' |
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
278 |
Number fromString:'1abc' |
3716 | 279 |
'12345' asNumber |
280 |
" |
|
281 |
||
24448 | 282 |
"Modified: / 03-08-1998 / 20:05:11 / cg" |
283 |
"Modified (format): / 21-07-2019 / 18:04:58 / Claus Gittinger" |
|
3060 | 284 |
! |
285 |
||
9333 | 286 |
fromString:aString decimalPointCharacters:decimalPointCharacters onError:exceptionBlock |
17057 | 287 |
"return a number by reading from aString. |
288 |
In contrast to readFrom:, no garbage is allowed after the number. |
|
289 |
I.e. the string must contain exactly one valid number (with optional separators around)" |
|
9333 | 290 |
|
24448 | 291 |
^ self |
292 |
fromString:aString |
|
293 |
decimalPointCharacters:decimalPointCharacters |
|
294 |
thousandsSeparator:nil |
|
295 |
onError:exceptionBlock |
|
296 |
||
297 |
" |
|
298 |
Number fromString:'12345' onError:0 |
|
299 |
Number fromString:'12,345' decimalPointCharacters:',' onError:0 |
|
300 |
Number fromString:'12,345' decimalPointCharacters:',' onError:0 |
|
301 |
Number fromString:'fooBarBaz' onError:0 |
|
302 |
Number fromString:'123fooBarBaz' onError:0 |
|
303 |
Number fromString:'123,fooBarBaz' decimalPointCharacters:',' onError:0 |
|
304 |
" |
|
305 |
||
306 |
"Modified: / 03-08-1998 / 20:05:34 / cg" |
|
307 |
"Modified: / 21-07-2019 / 18:02:55 / Claus Gittinger" |
|
308 |
! |
|
309 |
||
310 |
fromString:aString decimalPointCharacters:decimalPointCharacters thousandsSeparator:thousandsSeparator onError:exceptionBlock |
|
311 |
"return a number by reading from aString. |
|
312 |
In contrast to readFrom:, no garbage is allowed after the number. |
|
313 |
I.e. the string must contain exactly one valid number (with optional separators around)" |
|
314 |
||
21142 | 315 |
|s num| |
316 |
||
317 |
s := aString readStream. |
|
24448 | 318 |
num := self readFrom:s |
319 |
decimalPointCharacters:decimalPointCharacters |
|
320 |
thousandsSeparator:thousandsSeparator |
|
321 |
onError:[^ exceptionBlock value]. |
|
21142 | 322 |
s atEnd ifFalse:[ |
323 |
s skipSeparators. |
|
324 |
s atEnd ifFalse:[ |
|
325 |
^ exceptionBlock value "/ - garbage at end of number' |
|
326 |
]. |
|
327 |
]. |
|
328 |
^ num. |
|
9333 | 329 |
|
330 |
" |
|
331 |
Number fromString:'12345' onError:0 |
|
21142 | 332 |
Number fromString:'12,345' decimalPointCharacters:',' onError:0 |
333 |
Number fromString:'12,345' decimalPointCharacters:',' onError:0 |
|
9333 | 334 |
Number fromString:'fooBarBaz' onError:0 |
21142 | 335 |
Number fromString:'123fooBarBaz' onError:0 |
336 |
Number fromString:'123,fooBarBaz' decimalPointCharacters:',' onError:0 |
|
9333 | 337 |
" |
338 |
||
24448 | 339 |
"Created: / 21-07-2019 / 18:02:32 / Claus Gittinger" |
9333 | 340 |
! |
341 |
||
3060 | 342 |
fromString:aString onError:exceptionBlock |
17057 | 343 |
"return a number by reading from aString. |
344 |
In contrast to readFrom:, no garbage is allowed after the number. |
|
345 |
I.e. the string must contain exactly one valid number (with optional separators around)" |
|
3060 | 346 |
|
21142 | 347 |
^ self fromString:aString decimalPointCharacters:(self decimalPointCharactersForReading) onError:exceptionBlock |
3060 | 348 |
|
3716 | 349 |
" |
350 |
Number fromString:'12345' onError:0 |
|
351 |
Number fromString:'fooBarBaz' onError:0 |
|
21142 | 352 |
Number fromString:'123fooBarBaz' onError:0 |
3716 | 353 |
" |
354 |
||
355 |
"Modified: / 3.8.1998 / 20:05:34 / cg" |
|
3060 | 356 |
! |
357 |
||
24448 | 358 |
readFrom:aStringOrStream decimalPointCharacter:decimalPointCharacter |
359 |
"return the next Number from the (character-)stream aStream; |
|
360 |
skipping all whitespace first. |
|
361 |
Return the value of exceptionBlock, if no number can be read. |
|
362 |
This method is less strict than the smalltalk number reader; it |
|
363 |
allows for prefixed + and also allows missing fractional part after eE. |
|
364 |
It also allows garbage after the number - i.e. it reads what it can. |
|
365 |
See #fromString: , which is more strict and does not allow garbage at the end." |
|
366 |
||
367 |
^ self |
|
368 |
readFrom:aStringOrStream |
|
369 |
decimalPointCharacters:(decimalPointCharacter asString) |
|
370 |
||
371 |
" |
|
25147 | 372 |
Number readFrom:'0' decimalPointCharacter:$. |
24448 | 373 |
Number readFrom:'123.456' decimalPointCharacter:$. |
374 |
Number readFrom:'123,456' decimalPointCharacter:$, |
|
375 |
" |
|
376 |
||
377 |
"Created: / 21-07-2019 / 13:09:52 / Claus Gittinger" |
|
25147 | 378 |
"Modified (comment): / 17-12-2019 / 14:42:47 / Stefan Reise" |
24448 | 379 |
! |
380 |
||
381 |
readFrom:aStringOrStream decimalPointCharacter:decimalPointCharacter onError:exceptionBlock |
|
382 |
"return the next Number from the (character-)stream aStream; |
|
383 |
skipping all whitespace first. |
|
384 |
Return the value of exceptionBlock, if no number can be read. |
|
385 |
This method is less strict than the Smalltalk number reader; |
|
386 |
it allows for prefixed + and also allows missing fractional part after eE. |
|
387 |
It supports the regular Smalltalk radix prefix xr. |
|
388 |
It also allows garbage after the number - i.e. it reads what it can. |
|
389 |
See #fromString: , which is more strict and does not allow garbage at the end. |
|
390 |
||
391 |
Notice (see examples below): |
|
392 |
if sent to Number, it will decide which type of number to return (depending on the exponent character); |
|
393 |
if sent to a concrete number-class, an instance of that class will be returned (independent of the exponent character) |
|
394 |
" |
|
395 |
||
396 |
^ self readFrom:aStringOrStream decimalPointCharacters:(decimalPointCharacter asString) onError:exceptionBlock |
|
397 |
||
398 |
" |
|
399 |
Number readFrom:(ReadStream on:'54.32e-01') decimalPointCharacter:$. onError:[self halt]. |
|
400 |
Number readFrom:(ReadStream on:'54,32e-01') decimalPointCharacter:$, onError:[self halt]. |
|
401 |
" |
|
402 |
||
403 |
"Created: / 21-07-2019 / 13:11:12 / Claus Gittinger" |
|
404 |
! |
|
405 |
||
20308 | 406 |
readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters |
9333 | 407 |
"return the next Number from the (character-)stream aStream; |
17057 | 408 |
skipping all whitespace first. |
409 |
Return the value of exceptionBlock, if no number can be read. |
|
9333 | 410 |
This method is less strict than the smalltalk number reader; it |
17057 | 411 |
allows for prefixed + and also allows missing fractional part after eE. |
412 |
It also allows garbage after the number - i.e. it reads what it can. |
|
413 |
See #fromString: , which is more strict and does not allow garbage at the end." |
|
9333 | 414 |
|
415 |
^ self |
|
20308 | 416 |
readFrom:aStringOrStream |
417 |
decimalPointCharacters:decimalPointCharacters |
|
418 |
onError:[self error:'conversion error for: ' , self name] |
|
9333 | 419 |
|
420 |
" |
|
20308 | 421 |
Number readFrom:'123.456' decimalPointCharacters:'.' |
422 |
Number readFrom:'123,456' decimalPointCharacters:'.,' |
|
423 |
Number readFrom:'123,456' decimalPointCharacters:'.' |
|
9333 | 424 |
" |
425 |
! |
|
426 |
||
23481 | 427 |
readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters allowCStyle:allowCStyle onError:exceptionBlock |
1 | 428 |
"return the next Number from the (character-)stream aStream; |
17057 | 429 |
skipping all whitespace first. |
430 |
Return the value of exceptionBlock, if no number can be read. |
|
23480 | 431 |
This method is less strict than the Smalltalk number reader; |
432 |
it allows for prefixed + and also allows missing fractional part after eE. |
|
433 |
It supports 0x, 0o and 0b prefixes (hex, octal and binary) |
|
434 |
and the regular Smalltalk radix prefix xr. |
|
25147 | 435 |
If also allows for strings like '1.0×1015' to be read (as 1E+15). |
23615 | 436 |
|
17057 | 437 |
It also allows garbage after the number - i.e. it reads what it can. |
21815 | 438 |
See #fromString: , which is more strict and does not allow garbage at the end. |
439 |
||
440 |
Notice (see examples below): |
|
441 |
if sent to Number, it will decide which type of number to return (depending on the exponent character); |
|
442 |
if sent to a concrete number-class, an instance of that class will be returned (independent of the exponent character) |
|
443 |
" |
|
1 | 444 |
|
24448 | 445 |
^ self |
446 |
readFrom:aStringOrStream |
|
447 |
decimalPointCharacters:decimalPointCharacters |
|
448 |
thousandsSeparator:nil |
|
449 |
allowCStyle:allowCStyle onError:exceptionBlock |
|
450 |
||
451 |
" |
|
452 |
Number readFrom:(ReadStream on:'1.234.567,99') |
|
453 |
decimalPointCharacter:$, |
|
454 |
thousandsSeparator:$. |
|
455 |
onError:[self halt]. |
|
456 |
" |
|
457 |
||
458 |
"Created: / 27-10-2018 / 09:21:11 / Claus Gittinger" |
|
459 |
"Modified: / 21-07-2019 / 13:06:54 / Claus Gittinger" |
|
460 |
! |
|
461 |
||
462 |
readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters onError:exceptionBlock |
|
463 |
"return the next Number from the (character-)stream aStream; |
|
464 |
skipping all whitespace first. |
|
465 |
Return the value of exceptionBlock, if no number can be read. |
|
466 |
This method is less strict than the Smalltalk number reader; |
|
467 |
it allows for prefixed + and also allows missing fractional part after eE. |
|
468 |
It supports the regular Smalltalk radix prefix xr. |
|
469 |
It also allows garbage after the number - i.e. it reads what it can. |
|
470 |
See #fromString: , which is more strict and does not allow garbage at the end. |
|
471 |
||
472 |
Notice (see examples below): |
|
473 |
if sent to Number, it will decide which type of number to return (depending on the exponent character); |
|
474 |
if sent to a concrete number-class, an instance of that class will be returned (independent of the exponent character) |
|
475 |
" |
|
476 |
||
477 |
^ self |
|
478 |
readFrom:aStringOrStream |
|
479 |
decimalPointCharacters:decimalPointCharacters |
|
480 |
thousandsSeparator:nil |
|
25144 | 481 |
allowCStyle:true |
24448 | 482 |
onError:exceptionBlock |
483 |
||
484 |
" |
|
485 |
Number readFrom:(ReadStream on:'54.32e-01') decimalPointCharacters:'.' onError:[self halt]. |
|
486 |
||
487 |
Number readFrom:(ReadStream on:'12345') decimalPointCharacters:'.' onError:[self halt]. |
|
488 |
Number readFrom:(ReadStream on:'12345.0') decimalPointCharacters:'.' onError:[self halt]. |
|
489 |
||
490 |
Number readFrom:(ReadStream on:'12345.0f') decimalPointCharacters:'.' onError:[self halt]. |
|
491 |
Number readFrom:(ReadStream on:'12345.0e') decimalPointCharacters:'.' onError:[self halt]. |
|
492 |
Number readFrom:(ReadStream on:'12345.0q') decimalPointCharacters:'.' onError:[self halt]. |
|
493 |
Number readFrom:(ReadStream on:'12345.0d') decimalPointCharacters:'.' onError:[self halt]. |
|
494 |
Number readFrom:(ReadStream on:'12345.0s') decimalPointCharacters:'.' onError:[self halt]. |
|
495 |
Number readFrom:(ReadStream on:'12345.01s') decimalPointCharacters:'.' onError:[self halt]. |
|
496 |
||
497 |
Float readFrom:(ReadStream on:'12345') decimalPointCharacters:'.' onError:[self halt]. |
|
498 |
||
499 |
Number readFrom:(ReadStream on:'12345678901234567890') |
|
500 |
Number readFrom:(ReadStream on:'12345678901234567890.0') |
|
501 |
Number readFrom:(ReadStream on:'12345678901234567890.012345678901234567890') |
|
25144 | 502 |
Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
24448 | 503 |
Number readFrom:'16rAAAAFFFFAAAAFFFF' |
504 |
Number readFrom:'16r100A' |
|
505 |
Number readFrom:'16r100a' |
|
506 |
Number readFrom:'0.000001' |
|
507 |
'+00000123.45' asNumber |
|
508 |
Number readFrom:'(1/3)' |
|
509 |
Number readFrom:'(-1/3)' |
|
510 |
Number readFrom:'(1/-3)' |
|
511 |
Number readFrom:'-(1/3)' |
|
512 |
Number readFrom:'-(-1/3)' |
|
513 |
Number readFrom:'(-1/3' |
|
514 |
Number readFrom:'99s' |
|
515 |
Number readFrom:'99.00s' |
|
516 |
Number readFrom:'99.0000000s' |
|
517 |
Number readFrom:'.0000000s' |
|
518 |
Number readFrom:'.0000000q' |
|
519 |
Number readFrom:'.0000000f' |
|
520 |
Number readFrom:'.0000000e' |
|
521 |
Number readFrom:'.0000000s1' |
|
522 |
Number readFrom:'.0000000q1' |
|
523 |
Number readFrom:'.0000000f1' |
|
524 |
Number readFrom:'.0000000e1' |
|
525 |
LongFloat readFrom:'.00000001' |
|
526 |
Number readFrom:'.00000000000001' |
|
527 |
Number readFrom:'.001' |
|
528 |
ShortFloat readFrom:'.001' |
|
529 |
Number readFrom:'123garbage' -> returns 123 |
|
530 |
Number fromString:'123garbage' -> raises an error |
|
531 |
||
532 |
DecimalPointCharactersForReading := #( $. $, ). |
|
533 |
Number readFrom:'99,00' |
|
534 |
||
535 |
DecimalPointCharactersForReading := #( $. ). |
|
536 |
Number readFrom:'99,00' |
|
537 |
" |
|
538 |
||
539 |
"Modified: / 17-07-2017 / 15:18:03 / cg" |
|
540 |
"Modified: / 21-07-2019 / 13:05:19 / Claus Gittinger" |
|
25144 | 541 |
"Modified: / 17-12-2019 / 14:19:54 / Stefan Reise" |
24448 | 542 |
! |
543 |
||
544 |
readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters thousandsSeparator:thousandsSeparator allowCStyle:allowCStyle onError:exceptionBlock |
|
545 |
"return the next Number from the (character-)stream aStream; |
|
546 |
skipping all whitespace first. |
|
547 |
Return the value of exceptionBlock, if no number can be read. |
|
548 |
This method is less strict than the Smalltalk number reader; |
|
549 |
it allows for prefixed + and also allows missing fractional part after eE. |
|
550 |
It supports 0x, 0o and 0b prefixes (hex, octal and binary) |
|
551 |
and the regular Smalltalk radix prefix xr. |
|
25147 | 552 |
If also allows for strings like '1.0×1015' to be read (as 1E+15). |
24448 | 553 |
|
554 |
It also allows garbage after the number - i.e. it reads what it can. |
|
555 |
See #fromString: , which is more strict and does not allow garbage at the end. |
|
556 |
||
557 |
Notice (see examples below): |
|
558 |
if sent to Number, it will decide which type of number to return (depending on the exponent character); |
|
559 |
if sent to a concrete number-class, an instance of that class will be returned (independent of the exponent character) |
|
560 |
" |
|
561 |
||
6632
65014fd967d9
Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
6584
diff
changeset
|
562 |
^ [ |
21634 | 563 |
|value intValue mantissaAndScale scale decimalMantissa str |
24996 | 564 |
nextChar radix sign signExp exp numerator denom expChar expChar2 |
565 |
fragment mantissa| |
|
21634 | 566 |
|
567 |
str := aStringOrStream readStream. |
|
568 |
||
569 |
nextChar := str skipSeparators. |
|
570 |
nextChar isNil ifTrue:[^ exceptionBlock value]. |
|
571 |
||
572 |
(nextChar == $-) ifTrue:[ |
|
573 |
sign := -1. |
|
24449 | 574 |
nextChar := str nextPeekOrNil |
21634 | 575 |
] ifFalse:[ |
576 |
sign := 1. |
|
577 |
(nextChar == $+) ifTrue:[ |
|
24449 | 578 |
nextChar := str nextPeekOrNil |
21634 | 579 |
] |
580 |
]. |
|
581 |
nextChar == $( ifTrue:[ |
|
582 |
"maybe a Fraction e.g. (1/3)" |
|
583 |
str next. |
|
584 |
numerator := Integer readFrom:str onError:[^ exceptionBlock value]. |
|
585 |
str skipSeparators. |
|
586 |
nextChar := str peekOrNil. |
|
587 |
nextChar == $/ ifTrue:[ |
|
588 |
str next. |
|
589 |
denom := Integer readFrom:str onError:[^ exceptionBlock value]. |
|
590 |
str skipSeparators. |
|
591 |
nextChar := str peekOrNil. |
|
592 |
]. |
|
593 |
nextChar == $) ifFalse:[^ exceptionBlock value]. |
|
594 |
str next. |
|
595 |
value := Fraction numerator:numerator denominator:denom. |
|
24996 | 596 |
sign < 0 ifTrue:[ value := value negated ]. |
21871 | 597 |
(self ~~ Number and:[self ~~ Fraction]) ifTrue:[ |
598 |
value := self coerce:value. |
|
599 |
]. |
|
600 |
^ value |
|
21634 | 601 |
]. |
602 |
nextChar isNil ifTrue:[^ exceptionBlock value]. |
|
603 |
(nextChar isDigit or:[(decimalPointCharacters includes:nextChar)]) ifFalse:[ |
|
604 |
^ exceptionBlock value. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
605 |
"/ value := super readFrom:str. |
15197 | 606 |
"/ sign == -1 ifTrue:[value := value negated]. |
339 | 607 |
"/ ^ value |
21634 | 608 |
]. |
24448 | 609 |
radix := 10. |
21634 | 610 |
(decimalPointCharacters includes:nextChar) ifTrue:[ |
22043 | 611 |
self isAbstract ifTrue:[ |
612 |
value := 0.0. |
|
613 |
] ifFalse:[ |
|
614 |
value := self zero. "/ 0.0. |
|
615 |
]. |
|
21634 | 616 |
intValue := 0. |
617 |
] ifFalse:[ |
|
23481 | 618 |
(allowCStyle and:[nextChar == $0]) ifTrue:[ |
24449 | 619 |
nextChar := str nextPeekOrNil. |
23480 | 620 |
nextChar isNil ifTrue:[^ 0]. |
25144 | 621 |
((nextChar == $x) or:[nextChar == $X]) ifTrue:[ str next. radix := 16 ] |
622 |
ifFalse:[ ((nextChar == $b) or:[nextChar == $B]) ifTrue:[ str next. radix := 2 ] |
|
623 |
ifFalse:[ ((nextChar == $o) or:[nextChar == $O]) ifTrue:[ str next. radix := 8 ] |
|
24449 | 624 |
ifFalse:[ |
625 |
nextChar isDigit ifFalse:[ |
|
626 |
^ 0 |
|
627 |
]. |
|
628 |
]]]. |
|
25144 | 629 |
str peek == $- ifTrue:[ |
630 |
sign := -1. |
|
631 |
str next |
|
632 |
]. |
|
23480 | 633 |
value := Integer readFrom:str radix:radix. |
24448 | 634 |
nextChar := str peekOrNil. |
23480 | 635 |
] ifFalse:[ |
636 |
value := Integer readFrom:str radix:10. |
|
637 |
nextChar := str peekOrNil. |
|
24448 | 638 |
[ thousandsSeparator notNil and:[nextChar == thousandsSeparator] ] whileTrue:[ |
639 |
str next. |
|
640 |
fragment := str next:3. |
|
641 |
(fragment conform:#isDigit) ifFalse:[ |
|
642 |
^ exceptionBlock value. |
|
643 |
]. |
|
24449 | 644 |
value := (value * 1000) + (Integer fastFromString:fragment at:1). |
24448 | 645 |
nextChar := str peekOrNil. |
646 |
]. |
|
23480 | 647 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
648 |
str next. |
|
25144 | 649 |
str peek == $- ifTrue:[ |
650 |
sign := -1. |
|
651 |
str next |
|
652 |
]. |
|
23480 | 653 |
radix := value. |
654 |
value := Integer readFrom:str radix:radix. |
|
655 |
nextChar := str peekOrNil. |
|
656 |
] ifFalse:[ |
|
657 |
radix := 10 |
|
658 |
]. |
|
659 |
]. |
|
21634 | 660 |
intValue := value. |
661 |
]. |
|
662 |
||
663 |
(self == Integer or:[self inheritsFrom:Integer]) ifFalse:[ |
|
664 |
(decimalPointCharacters includes:nextChar) ifTrue:[ |
|
24449 | 665 |
nextChar := str nextPeekOrNil. |
21634 | 666 |
decimalMantissa := 0. |
667 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[ |
|
668 |
mantissaAndScale := self readMantissaAndScaleFrom:str radix:radix. |
|
669 |
mantissa := mantissaAndScale first. |
|
670 |
value := (mantissa coerce:value) + mantissa. |
|
671 |
nextChar := str peekOrNil. |
|
672 |
] |
|
673 |
]. |
|
21815 | 674 |
|
675 |
('eEdDqQfF' includes:nextChar) ifTrue:[ |
|
676 |
expChar := nextChar. |
|
24449 | 677 |
nextChar := str nextPeekOrNil. |
24996 | 678 |
expChar == $Q ifTrue:[ |
679 |
nextChar == $D ifTrue:[ |
|
680 |
expChar2 := nextChar. |
|
681 |
nextChar := str nextPeekOrNil. |
|
682 |
] ifFalse:[ |
|
683 |
nextChar == $L ifTrue:[ |
|
684 |
expChar2 := nextChar. |
|
685 |
nextChar := str nextPeekOrNil. |
|
686 |
] |
|
687 |
] |
|
688 |
]. |
|
21815 | 689 |
|
21634 | 690 |
signExp := 1. |
691 |
(nextChar == $+) ifTrue:[ |
|
24449 | 692 |
nextChar := str nextPeekOrNil. |
21634 | 693 |
] ifFalse:[ |
694 |
(nextChar == $-) ifTrue:[ |
|
24449 | 695 |
nextChar := str nextPeekOrNil. |
21634 | 696 |
signExp := -1 |
697 |
] |
|
698 |
]. |
|
21815 | 699 |
|
700 |
"/ if I am abstract (i.e. I am Number or LPReal), |
|
701 |
"/ let the exponent-character decide what kind of float we get: |
|
702 |
"/ qQ -> LongFloat |
|
23479 | 703 |
"/ eEdD -> Float (which is ieee-double) |
704 |
"/ fF -> ShortFloat (which is ieee-float) |
|
21815 | 705 |
|
706 |
self isAbstract ifTrue:[ |
|
707 |
('qQ' includes:expChar) ifTrue:[ |
|
24996 | 708 |
expChar2 == $D ifTrue:[ |
709 |
value := value asQDouble |
|
710 |
] ifFalse:[ |
|
711 |
expChar2 == $L ifTrue:[ |
|
712 |
value := value asLargeFloat |
|
713 |
] ifFalse:[ |
|
714 |
value := value asLongFloat. |
|
715 |
] |
|
716 |
] |
|
21815 | 717 |
] ifFalse:[ |
718 |
('fF' includes:expChar) ifTrue:[ |
|
719 |
value := value asShortFloat. |
|
720 |
] ifFalse:[ |
|
721 |
"/ maybe in the far future we might create shortfloats when seeing eE, |
|
722 |
"/ and only produce doubles on dD. |
|
723 |
"/ (for now, always create Doubles for Dolphin, Squeak etc. compatibility) |
|
724 |
||
725 |
false "('eE' includes:expChar)" ifTrue:[ |
|
726 |
value := value asShortFloat |
|
727 |
] ifFalse:[ |
|
728 |
value := value asFloat. |
|
729 |
]. |
|
730 |
]. |
|
731 |
]. |
|
21634 | 732 |
] ifFalse:[ |
21871 | 733 |
value := self coerce:value. |
21634 | 734 |
]. |
21815 | 735 |
|
21634 | 736 |
(nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[ |
737 |
exp := (Integer readFrom:str radix:radix) * signExp. |
|
738 |
value := value * ((value class unity * 10.0) raisedToInteger:exp) |
|
739 |
] |
|
740 |
] ifFalse:[ |
|
741 |
('sS' includes:nextChar) ifTrue:[ |
|
24449 | 742 |
nextChar := str nextPeekOrNil. |
21634 | 743 |
(nextChar notNil and:[ nextChar isDigit]) ifTrue:[ |
744 |
scale := (Integer readFrom:str). |
|
745 |
]. |
|
746 |
||
747 |
mantissaAndScale isNil ifTrue:[ |
|
748 |
value := intValue asFixedPoint:(scale ? 0). |
|
749 |
] ifFalse:[ |
|
750 |
denom := 10 raisedTo:mantissaAndScale last. |
|
751 |
value := FixedPoint |
|
752 |
numerator:(intValue * denom) + (mantissaAndScale second) |
|
753 |
denominator:denom |
|
754 |
scale:(scale ? mantissaAndScale third). |
|
755 |
]. |
|
756 |
] ifFalse:[ |
|
25147 | 757 |
(nextChar == $×) ifTrue:[ |
23615 | 758 |
(((nextChar := str nextPeek) == $1) |
759 |
and:[ ((nextChar := str nextPeek) == $0) ] |
|
760 |
) ifTrue:[ |
|
761 |
str next. |
|
762 |
exp := (Integer readFrom:str). |
|
763 |
value := value * ((value class unity * 10.0) raisedToInteger:exp). |
|
764 |
] ifFalse:[ |
|
765 |
^ exceptionBlock value. |
|
766 |
]. |
|
767 |
] ifFalse:[ |
|
768 |
(self inheritsFrom:LimitedPrecisionReal) ifTrue:[ |
|
769 |
"when requesting a specific Float instance, coerce it. |
|
770 |
otherwise return a value without loosing precision" |
|
25085 | 771 |
(self isAbstract not and:[value class == self]) ifFalse:[ |
772 |
value := self coerce:value. |
|
773 |
]. |
|
23615 | 774 |
]. |
21634 | 775 |
]. |
776 |
]. |
|
777 |
]. |
|
778 |
]. |
|
779 |
sign == -1 ifTrue:[ |
|
780 |
value := value negated |
|
781 |
]. |
|
782 |
value. |
|
6632
65014fd967d9
Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
6584
diff
changeset
|
783 |
] on:Error do:exceptionBlock |
1 | 784 |
|
329 | 785 |
" |
21815 | 786 |
Number readFrom:(ReadStream on:'54.32e-01') decimalPointCharacters:'.' onError:[self halt]. |
787 |
||
788 |
Number readFrom:(ReadStream on:'12345') decimalPointCharacters:'.' onError:[self halt]. |
|
789 |
Number readFrom:(ReadStream on:'12345.0') decimalPointCharacters:'.' onError:[self halt]. |
|
790 |
||
791 |
Number readFrom:(ReadStream on:'12345.0f') decimalPointCharacters:'.' onError:[self halt]. |
|
792 |
Number readFrom:(ReadStream on:'12345.0e') decimalPointCharacters:'.' onError:[self halt]. |
|
793 |
Number readFrom:(ReadStream on:'12345.0q') decimalPointCharacters:'.' onError:[self halt]. |
|
794 |
Number readFrom:(ReadStream on:'12345.0d') decimalPointCharacters:'.' onError:[self halt]. |
|
795 |
Number readFrom:(ReadStream on:'12345.0s') decimalPointCharacters:'.' onError:[self halt]. |
|
796 |
Number readFrom:(ReadStream on:'12345.01s') decimalPointCharacters:'.' onError:[self halt]. |
|
797 |
||
798 |
Float readFrom:(ReadStream on:'12345') decimalPointCharacters:'.' onError:[self halt]. |
|
799 |
||
20308 | 800 |
Number readFrom:(ReadStream on:'12345678901234567890') |
801 |
Number readFrom:(ReadStream on:'12345678901234567890.0') |
|
802 |
Number readFrom:(ReadStream on:'12345678901234567890.012345678901234567890') |
|
803 |
Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
804 |
Number readFrom:'16rAAAAFFFFAAAAFFFF' |
|
23480 | 805 |
Number readFrom:'16r100A' |
25144 | 806 |
Number readFrom:'16r100a' |
807 |
Number readFrom:'16r-100A' |
|
808 |
Number readFrom:'-16r100A' |
|
809 |
Number readFrom:'0x100A' |
|
810 |
Number readFrom:'-0x100A' |
|
811 |
Number readFrom:'0x-100A' |
|
23481 | 812 |
Number readFrom:'0.000001' |
813 |
'+00000123.45' asNumber |
|
814 |
Number readFrom:'(1/3)' |
|
815 |
Number readFrom:'(-1/3)' |
|
816 |
Number readFrom:'(1/-3)' |
|
817 |
Number readFrom:'-(1/3)' |
|
818 |
Number readFrom:'-(-1/3)' |
|
819 |
Number readFrom:'(-1/3' |
|
820 |
Number readFrom:'99s' |
|
821 |
Number readFrom:'99.00s' |
|
822 |
Number readFrom:'99.0000000s' |
|
823 |
Number readFrom:'.0000000s' |
|
824 |
Number readFrom:'.0000000q' |
|
825 |
Number readFrom:'.0000000f' |
|
826 |
Number readFrom:'.0000000e' |
|
827 |
Number readFrom:'.0000000s1' |
|
828 |
Number readFrom:'.0000000q1' |
|
829 |
Number readFrom:'.0000000f1' |
|
830 |
Number readFrom:'.0000000e1' |
|
831 |
LongFloat readFrom:'.00000001' |
|
832 |
Number readFrom:'.00000000000001' |
|
833 |
Number readFrom:'.001' |
|
834 |
ShortFloat readFrom:'.001' |
|
835 |
Number readFrom:'123garbage' -> returns 123 |
|
836 |
Number fromString:'123garbage' -> raises an error |
|
837 |
||
838 |
DecimalPointCharactersForReading := #( $. $, ). |
|
839 |
Number readFrom:'99,00' |
|
840 |
||
841 |
DecimalPointCharactersForReading := #( $. ). |
|
842 |
Number readFrom:'99,00' |
|
843 |
" |
|
844 |
||
24448 | 845 |
"Created: / 21-07-2019 / 13:05:04 / Claus Gittinger" |
24449 | 846 |
"Modified: / 21-07-2019 / 19:40:34 / Claus Gittinger" |
25144 | 847 |
"Modified (comment): / 17-12-2019 / 14:28:03 / Stefan Reise" |
23481 | 848 |
! |
849 |
||
24448 | 850 |
readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters thousandsSeparator:thousandsSeparator onError:exceptionBlock |
23481 | 851 |
"return the next Number from the (character-)stream aStream; |
852 |
skipping all whitespace first. |
|
853 |
Return the value of exceptionBlock, if no number can be read. |
|
854 |
This method is less strict than the Smalltalk number reader; |
|
855 |
it allows for prefixed + and also allows missing fractional part after eE. |
|
856 |
It supports the regular Smalltalk radix prefix xr. |
|
857 |
It also allows garbage after the number - i.e. it reads what it can. |
|
858 |
See #fromString: , which is more strict and does not allow garbage at the end. |
|
859 |
||
860 |
Notice (see examples below): |
|
861 |
if sent to Number, it will decide which type of number to return (depending on the exponent character); |
|
862 |
if sent to a concrete number-class, an instance of that class will be returned (independent of the exponent character) |
|
863 |
" |
|
864 |
||
865 |
^ self |
|
866 |
readFrom:aStringOrStream |
|
867 |
decimalPointCharacters:decimalPointCharacters |
|
24448 | 868 |
thousandsSeparator:thousandsSeparator |
23481 | 869 |
allowCStyle:false |
870 |
onError:exceptionBlock |
|
871 |
||
872 |
" |
|
873 |
Number readFrom:(ReadStream on:'12345') decimalPointCharacters:'.' onError:[self halt]. |
|
24448 | 874 |
Number readFrom:(ReadStream on:'12,345.0') decimalPointCharacters:'.' thousandsSeparator:$, onError:[self halt]. |
875 |
Number readFrom:(ReadStream on:'12,1345.0') decimalPointCharacters:'.' thousandsSeparator:$, onError:[self halt]. |
|
329 | 876 |
" |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
877 |
|
24448 | 878 |
"Created: / 21-07-2019 / 17:59:57 / Claus Gittinger" |
296 | 879 |
! |
880 |
||
9333 | 881 |
readFrom:aStringOrStream onError:exceptionBlock |
882 |
"return the next Number from the (character-)stream aStream; |
|
883 |
skipping all whitespace first; return the value of exceptionBlock, |
|
884 |
if no number can be read. |
|
885 |
This method is less strict than the smalltalk number reader; it |
|
17057 | 886 |
allows for prefixed + and also allows missing fractional part after eE. |
887 |
It also allows garbage after the number - i.e. it reads what it can. |
|
888 |
See #fromString: , which is more strict and does not allow garbage at the end." |
|
9333 | 889 |
|
20308 | 890 |
^ self |
25147 | 891 |
readFrom:aStringOrStream |
892 |
decimalPointCharacters:(self decimalPointCharactersForReading) |
|
893 |
onError:exceptionBlock |
|
9333 | 894 |
|
895 |
" |
|
20308 | 896 |
Number readFrom:(ReadStream on:'54.32e-01') |
897 |
Number readFrom:(ReadStream on:'12345678901234567890') |
|
898 |
Number readFrom:(ReadStream on:'12345678901234567890.0') |
|
899 |
Number readFrom:(ReadStream on:'12345678901234567890.012345678901234567890') |
|
900 |
Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
901 |
Number readFrom:'16rAAAAFFFFAAAAFFFF' |
|
902 |
Number readFrom:'0.000001' |
|
903 |
'+00000123.45' asNumber |
|
25147 | 904 |
Number readFrom:'0' |
20308 | 905 |
Number readFrom:'99s' |
906 |
Number readFrom:'99.00s' |
|
907 |
Number readFrom:'99.0000000s' |
|
908 |
Number readFrom:'.0000000s' |
|
909 |
Number readFrom:'.0000000q' |
|
910 |
Number readFrom:'.0000000f' |
|
911 |
Number readFrom:'.0000000e' |
|
912 |
Number readFrom:'.0000000s1' |
|
913 |
Number readFrom:'.0000000q1' |
|
914 |
Number readFrom:'.0000000f1' |
|
915 |
Number readFrom:'.0000000e1' |
|
916 |
||
917 |
DecimalPointCharactersForReading := #( $. $, ). |
|
918 |
Number readFrom:'99,00' |
|
919 |
||
920 |
DecimalPointCharactersForReading := #( $. ). |
|
921 |
Number readFrom:'99,00' |
|
9333 | 922 |
" |
25147 | 923 |
|
924 |
"Modified (comment): / 17-12-2019 / 14:42:57 / Stefan Reise" |
|
9333 | 925 |
! |
926 |
||
296 | 927 |
readSmalltalkSyntaxFrom:aStream |
928 |
"ST-80 compatibility (thanks to a note from alpha testers) |
|
20308 | 929 |
read and return the next Number in smalltalk syntax from the |
7356 | 930 |
(character-) aStream. |
931 |
Returns nil if aStream contains no valid number." |
|
329 | 932 |
|
15776 | 933 |
^ self readSmalltalkSyntaxFrom:aStream onError:nil. |
296 | 934 |
|
329 | 935 |
" |
20308 | 936 |
Number readSmalltalkSyntaxFrom:'99d' |
937 |
Number readSmalltalkSyntaxFrom:'99.00d' |
|
938 |
Number readSmalltalkSyntaxFrom:'54.32e-01' |
|
15078 | 939 |
Number readSmalltalkSyntaxFrom:'12345678901234567890' |
20308 | 940 |
Number readSmalltalkSyntaxFrom:'16rAAAAFFFFAAAAFFFF' |
941 |
Number readSmalltalkSyntaxFrom:'foobar' |
|
942 |
Number readSmalltalkSyntaxFrom:'(1/10)' |
|
943 |
||
944 |
Number readSmalltalkSyntaxFrom:'(1/0)' |
|
945 |
||
946 |
Number readFrom:'(1/3)' |
|
15078 | 947 |
Number readFrom:'(-1/3)' |
20308 | 948 |
Number readFrom:'-(1/3)' |
949 |
Number readFrom:'(1/-3)' |
|
950 |
Number readFrom:'(-1/-3)' |
|
951 |
Number readFrom:'-(-1/-3)' |
|
952 |
Number readSmalltalkSyntaxFrom:'+00000123.45' |
|
953 |
Number readFrom:'+00000123.45' |
|
5013 | 954 |
|
955 |
|s| |
|
956 |
s := ReadStream on:'2.'. |
|
957 |
Number readSmalltalkSyntaxFrom:s. |
|
20308 | 958 |
s next |
5013 | 959 |
|
960 |
|s| |
|
961 |
s := ReadStream on:'2.0.'. |
|
962 |
Number readSmalltalkSyntaxFrom:s. |
|
20308 | 963 |
s next |
329 | 964 |
" |
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
965 |
|
5013 | 966 |
"Modified: / 19.11.1999 / 18:26:47 / cg" |
15776 | 967 |
! |
968 |
||
969 |
readSmalltalkSyntaxFrom:aStream onError:errorValue |
|
970 |
"ST-80 compatibility (thanks to a note from alpha testers) |
|
20308 | 971 |
read and return the next Number in smalltalk syntax from the |
15776 | 972 |
(character-) aStream. |
973 |
Returns nil if aStream contains no valid number." |
|
974 |
||
975 |
|n| |
|
976 |
||
977 |
[ |
|
24548 | 978 |
n := Scanner scanNumberFrom:aStream. |
15776 | 979 |
] on:Error do:[:ex| |
24548 | 980 |
n := nil |
15776 | 981 |
]. |
982 |
n isNil ifTrue:[^ errorValue value]. |
|
983 |
^ n |
|
984 |
||
985 |
" |
|
20308 | 986 |
Number readSmalltalkSyntaxFrom:'foo' onError:123 |
24548 | 987 |
Number readSmalltalkSyntaxFrom:'16r123' onError:-1 |
25041 | 988 |
Number readSmalltalkSyntaxFrom:'0x123' onError:-1 |
989 |
Number readSmalltalkSyntaxFrom:'16r1.1' onError:-1 |
|
990 |
Number readSmalltalkSyntaxFrom:'10r1.1' onError:-1 |
|
991 |
Number readSmalltalkSyntaxFrom:'16r10.1' onError:-1 |
|
992 |
Number readSmalltalkSyntaxFrom:'10r10.1' onError:-1 |
|
993 |
Number readSmalltalkSyntaxFrom:'16r10.1e10' onError:-1 |
|
994 |
Number readSmalltalkSyntaxFrom:'10r10.1e10' onError:-1 |
|
15776 | 995 |
" |
1 | 996 |
! ! |
701 | 997 |
|
17686 | 998 |
!Number class methodsFor:'Compatibility-VW'! |
999 |
||
1000 |
readIntegerFrom:aStream radix:radix |
|
21789 | 1001 |
"for VisualWorks compatibility" |
17686 | 1002 |
|
1003 |
^ Integer readFrom:aStream radix:radix |
|
21789 | 1004 |
|
1005 |
"Modified (comment): / 08-06-2017 / 13:58:36 / mawalch" |
|
17686 | 1006 |
! ! |
1007 |
||
25122 | 1008 |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
1009 |
!Number class methodsFor:'constants'! |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
1010 |
|
21828 | 1011 |
e |
1012 |
"return the closest approximation of the irrational number e" |
|
1013 |
||
1014 |
^ self subclassResponsibility |
|
1015 |
||
1016 |
"Modified: / 16-06-2017 / 11:04:49 / cg" |
|
1017 |
! |
|
1018 |
||
24262 | 1019 |
eDigits |
1020 |
"return th printString of the irrational number e, |
|
1021 |
with enough digits so that instances with different precision can read from it" |
|
1022 |
||
1023 |
"/ number asked from wolfram |
|
1024 |
^ '2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746' |
|
1025 |
||
1026 |
"Created: / 06-06-2019 / 16:58:50 / Claus Gittinger" |
|
1027 |
! |
|
1028 |
||
21930 | 1029 |
i |
1030 |
"return the imaginary unit i" |
|
1031 |
||
1032 |
||
1033 |
^ Complex real:0 imaginary:1 |
|
1034 |
||
1035 |
" |
|
22287 | 1036 |
1 + Number i -> (1+1i) |
1037 |
Number i + 10 -> (10+1i) |
|
1038 |
Number i * Number i -> -1 |
|
21930 | 1039 |
" |
1040 |
||
1041 |
"Created: / 01-07-2017 / 19:44:53 / cg" |
|
22287 | 1042 |
"Modified (comment): / 22-09-2017 / 09:53:14 / cg" |
21930 | 1043 |
! |
1044 |
||
21828 | 1045 |
ln10 |
1046 |
"return ln(10) in my representation (and accuracy)." |
|
1047 |
||
1048 |
^ self subclassResponsibility |
|
1049 |
||
1050 |
"Created: / 16-06-2017 / 11:00:38 / cg" |
|
1051 |
! |
|
1052 |
||
24950 | 1053 |
ln2 |
1054 |
"return ln(2) in my representation (and accuracy)." |
|
1055 |
||
1056 |
^ self subclassResponsibility |
|
1057 |
||
1058 |
||
1059 |
! |
|
1060 |
||
24934 | 1061 |
phi |
1062 |
"return Phi in my representation (and accuracy)." |
|
1063 |
||
1064 |
^ self subclassResponsibility |
|
1065 |
||
1066 |
||
1067 |
! |
|
1068 |
||
1069 |
phiDigits |
|
1070 |
"return th printString of the irrational number pi, |
|
1071 |
with enough digits so that instances with different precision can read from it" |
|
1072 |
||
1073 |
^ '1.618033988749894848204586834365638117720309179805762862135' |
|
1074 |
! |
|
1075 |
||
21828 | 1076 |
pi |
1077 |
"return Pi in my representation (and accuracy)." |
|
1078 |
||
1079 |
^ self subclassResponsibility |
|
1080 |
||
1081 |
"Modified (format): / 16-06-2017 / 11:00:42 / cg" |
|
24262 | 1082 |
! |
1083 |
||
1084 |
piDigits |
|
1085 |
"return th printString of the irrational number pi, |
|
1086 |
with enough digits so that instances with different precision can read from it" |
|
1087 |
||
1088 |
"/ number asked from wolfram |
|
1089 |
"/ ^ '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904' |
|
1090 |
"/ rounded to 100 digits |
|
1091 |
^ '3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068' |
|
1092 |
||
1093 |
"Created: / 06-06-2019 / 17:08:17 / Claus Gittinger" |
|
21828 | 1094 |
! ! |
1095 |
||
1096 |
!Number class methodsFor:'constants & defaults'! |
|
1097 |
||
20308 | 1098 |
decimalPointCharacter |
8301 | 1099 |
"printed" |
1100 |
||
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1101 |
<resource: #obsolete> |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1102 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1103 |
^ self decimalPointCharacterForPrinting |
8301 | 1104 |
! |
1105 |
||
20308 | 1106 |
decimalPointCharacter:aCharacter |
8303 | 1107 |
"printed" |
1108 |
||
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1109 |
<resource: #obsolete> |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1110 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1111 |
self decimalPointCharacterForPrinting:aCharacter |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1112 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1113 |
" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1114 |
1.5 printString |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1115 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1116 |
Number decimalPointCharacter:$,. |
20308 | 1117 |
1.5 printString |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1118 |
Number decimalPointCharacter:$.. |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1119 |
" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1120 |
! |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1121 |
|
20308 | 1122 |
decimalPointCharacterForPrinting |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1123 |
"printed" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1124 |
|
16267 | 1125 |
^ DecimalPointCharacterForPrinting ? $. |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1126 |
! |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1127 |
|
20308 | 1128 |
decimalPointCharacterForPrinting:aCharacter |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1129 |
"printed" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1130 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1131 |
DecimalPointCharacterForPrinting := aCharacter |
8303 | 1132 |
|
1133 |
" |
|
1134 |
1.5 printString |
|
1135 |
||
9274 | 1136 |
Number decimalPointCharacterForPrinting:$,. |
20308 | 1137 |
1.5 printString |
9274 | 1138 |
Number decimalPointCharacterForPrinting:$.. |
8303 | 1139 |
" |
1140 |
! |
|
1141 |
||
20308 | 1142 |
decimalPointCharacters |
8301 | 1143 |
"accepted when converting from a string" |
1144 |
||
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1145 |
<resource: #obsolete> |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1146 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1147 |
^ self decimalPointCharactersForReading |
8303 | 1148 |
|
1149 |
" |
|
1150 |
1.5 printString |
|
1151 |
||
1152 |
Number decimalPointCharacters:#( $. $,) . |
|
20308 | 1153 |
Number fromString:'1.5'. |
1154 |
Number fromString:'1,5'. |
|
8303 | 1155 |
Number decimalPointCharacters:#( $. ). |
1156 |
" |
|
1157 |
! |
|
1158 |
||
20308 | 1159 |
decimalPointCharacters:aCollectionOfCharacters |
8303 | 1160 |
"accepted when converting from a string" |
1161 |
||
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1162 |
<resource: #obsolete> |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1163 |
|
20308 | 1164 |
self decimalPointCharactersForReading:aCollectionOfCharacters |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1165 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1166 |
" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1167 |
Number decimalPointCharacters:#( $. $,) . |
20308 | 1168 |
Number fromString:'1.5'. |
1169 |
Number fromString:'1,5'. |
|
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1170 |
Number decimalPointCharacters:#( $. ). |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1171 |
" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1172 |
! |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1173 |
|
20308 | 1174 |
decimalPointCharactersForReading |
9333 | 1175 |
"default when converting from a string" |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1176 |
|
9333 | 1177 |
"/ cg: changing the default leads to trouble in some |
1178 |
"/ language processors (PrologScanner...) |
|
1179 |
"/ PLEASE DO ONLY CHANGE THE DEFAULT BELOW FOR END-USER APPLICATIONS (if at all). |
|
1180 |
"/ BETTER: pass the DecimalPointCharacterSet explicitly |
|
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1181 |
DecimalPointCharactersForReading isNil ifTrue:[ |
20308 | 1182 |
^ #( $. ) |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1183 |
]. |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1184 |
^ DecimalPointCharactersForReading |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1185 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1186 |
" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1187 |
1.5 printString |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1188 |
|
9274 | 1189 |
Number decimalPointCharactersForReading:#( $. $,) . |
20308 | 1190 |
Number fromString:'1.5'. |
1191 |
Number fromString:'1,5'. |
|
9274 | 1192 |
Number decimalPointCharactersForReading:#( $. ). |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1193 |
" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1194 |
! |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1195 |
|
20308 | 1196 |
decimalPointCharactersForReading:aCollectionOfCharacters |
9127
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1197 |
"accepted when converting from a string" |
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1198 |
|
04cd80c64a30
decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents:
8979
diff
changeset
|
1199 |
DecimalPointCharactersForReading := aCollectionOfCharacters |
8303 | 1200 |
|
1201 |
" |
|
9274 | 1202 |
Number decimalPointCharactersForReading:#( $. $,) . |
20308 | 1203 |
Number fromString:'1.5'. |
1204 |
Number fromString:'1,5'. |
|
9274 | 1205 |
Number decimalPointCharactersForReading:#( $. ). |
8303 | 1206 |
" |
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
1207 |
! ! |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
1208 |
|
1892 | 1209 |
!Number class methodsFor:'error reporting'! |
701 | 1210 |
|
20308 | 1211 |
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arg:arg errorString:text |
3394 | 1212 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
1213 |
||
4558 | 1214 |
<context: #return> |
1215 |
||
6195 | 1216 |
^ self |
20308 | 1217 |
raise:aSignalSymbolOrErrorClass |
1218 |
receiver:someNumber |
|
1219 |
selector:sel |
|
1220 |
arguments:(Array with:arg) |
|
1221 |
errorString:text |
|
3394 | 1222 |
|
1223 |
" |
|
20308 | 1224 |
Number |
1225 |
raise:#domainErrorSignal |
|
1226 |
receiver:1.0 |
|
1227 |
selector:#sin |
|
1228 |
arg:nil |
|
1229 |
errorString:'foo bar test' |
|
3394 | 1230 |
" |
6195 | 1231 |
|
1232 |
"Modified: / 16.11.2001 / 14:12:50 / cg" |
|
3394 | 1233 |
! |
1234 |
||
20308 | 1235 |
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel errorString:text |
6195 | 1236 |
"ST-80 compatible signal raising. Provided for PD numeric classes. |
1237 |
aSignalSymbolOrErrorClass is either an Error-subclass, or |
|
1238 |
the selector which is sent to myself, to retrieve the Exception class / Signal." |
|
1239 |
||
1240 |
<context: #return> |
|
1241 |
||
1242 |
^ self |
|
20308 | 1243 |
raise:aSignalSymbolOrErrorClass |
1244 |
receiver:someNumber |
|
1245 |
selector:sel |
|
1246 |
arguments:#() |
|
1247 |
errorString:text |
|
6195 | 1248 |
|
1249 |
" |
|
20308 | 1250 |
Number |
1251 |
raise:#domainErrorSignal |
|
1252 |
receiver:1.0 |
|
1253 |
selector:#foo |
|
1254 |
errorString:'foo bar test' |
|
6195 | 1255 |
" |
1256 |
||
1257 |
"Modified: / 16.11.2001 / 14:13:16 / cg" |
|
701 | 1258 |
! ! |
1259 |
||
18863 | 1260 |
!Number class methodsFor:'misc'! |
1261 |
||
1262 |
displayRadix:aNumber |
|
1263 |
"being tired of always sending #printStringRadix: in the inspectors, |
|
1264 |
this allows you to change the default print radix for the displayString |
|
1265 |
method." |
|
1266 |
||
1267 |
DefaultDisplayRadix := aNumber |
|
1268 |
||
1269 |
" |
|
1270 |
Integer displayRadix:16. 123456 inspect |
|
1271 |
Integer displayRadix:10. 123456 inspect |
|
1272 |
" |
|
1273 |
! ! |
|
1274 |
||
25144 | 1275 |
|
1892 | 1276 |
!Number class methodsFor:'private'! |
701 | 1277 |
|
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1278 |
readMantissaAndScaleFrom:aStream radix:radix |
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1279 |
"helper for readFrom: - |
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1280 |
return the mantissa (post-decimal-point digits) from the (character-)stream aStream; |
21828 | 1281 |
in addition, the mantissa as integer and the scale (number of postDecimalPoint digits) is returned |
1282 |
(both to support reading fixedPoint numbers and to not loose precision). |
|
1283 |
The integer mantissa is needed as we do not yet know the target type (could be LongFloat or even QDouble). |
|
22859 | 1284 |
No whitespace is skipped." |
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1285 |
|
24462 | 1286 |
|nextChar intMantissa scale digit scaleFactor value| |
1287 |
||
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1288 |
scale := 0. |
21828 | 1289 |
scaleFactor := 1. |
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1290 |
intMantissa := 0. |
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1291 |
nextChar := aStream peekOrNil. |
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1292 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
21828 | 1293 |
digit := nextChar digitValue. |
1294 |
scaleFactor := scaleFactor * radix. |
|
1295 |
intMantissa := (intMantissa * radix) + digit. |
|
1296 |
scale := scale + 1. |
|
1297 |
||
1298 |
aStream next. |
|
1299 |
nextChar := aStream peekOrNil |
|
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1300 |
]. |
7388
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1301 |
|
21828 | 1302 |
self isAbstract ifFalse:[ |
24462 | 1303 |
value := (self coerce:intMantissa) / (self coerce:scaleFactor). |
21828 | 1304 |
] ifTrue:[ |
24996 | 1305 |
"/ Float decimalPrecision |
1306 |
"/ LongFloat decimalPrecision |
|
21828 | 1307 |
scale > 6 ifTrue:[ |
24996 | 1308 |
"/ scale > 19 ifTrue:[ |
1309 |
"/ value := intMantissa asLargeFloat / scaleFactor asLargeFloat. |
|
1310 |
"/ ] ifFalse:[ |
|
1311 |
value := intMantissa asLongFloat / scaleFactor asLongFloat. |
|
1312 |
"/ ] |
|
21828 | 1313 |
] ifFalse:[ |
24462 | 1314 |
value := intMantissa asFloat / scaleFactor asFloat. |
21828 | 1315 |
]. |
1316 |
]. |
|
24462 | 1317 |
^ (Array with:value with:intMantissa with:scale). |
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1318 |
|
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1319 |
" |
20308 | 1320 |
Number readMantissaAndScaleFrom:'234' readStream radix:10. |
1321 |
Number readMantissaAndScaleFrom:'2' readStream radix:10. |
|
1322 |
Number readMantissaAndScaleFrom:'234567' readStream radix:10. |
|
1323 |
Number readMantissaAndScaleFrom:'234000' readStream radix:10. |
|
1324 |
Number readMantissaAndScaleFrom:'234' readStream radix:10. |
|
21828 | 1325 |
Number readMantissaAndScaleFrom:'000234' readStream radix:10. |
22844 | 1326 |
Number readMantissaAndScaleFrom:'01' readStream radix:10. |
1327 |
Number readMantissaAndScaleFrom:'001' readStream radix:10. |
|
1328 |
Number readMantissaAndScaleFrom:'0001' readStream radix:10. |
|
21828 | 1329 |
Number readMantissaAndScaleFrom:'000000000000000000000000000024' readStream radix:10. |
22844 | 1330 |
Number readMantissaAndScaleFrom:'0000000000000000000000000000000000000000000024' readStream radix:10. |
24996 | 1331 |
Number readMantissaAndScaleFrom:'123456789012345678901234567890' readStream radix:10. |
1332 |
Number readMantissaAndScaleFrom:'1234567890123456789012345678901234567890' readStream radix:10. |
|
20308 | 1333 |
|
1334 |
Number readMantissaAndScaleFrom:'12345678901234567890' readStream radix:10. |
|
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1335 |
" |
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1336 |
|
21828 | 1337 |
"Modified: / 17-06-2017 / 03:03:03 / cg" |
24462 | 1338 |
"Modified: / 22-07-2019 / 19:37:21 / Claus Gittinger" |
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1339 |
! |
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1340 |
|
701 | 1341 |
readMantissaFrom:aStream radix:radix |
1342 |
"helper for readFrom: - |
|
6692 | 1343 |
return the mantissa (post-decimal-point digits) |
1344 |
from the (character-)stream aStream; |
|
22859 | 1345 |
No whitespace is skipped." |
701 | 1346 |
|
6899
9ed8494c9f10
support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents:
6702
diff
changeset
|
1347 |
^ (self readMantissaAndScaleFrom:aStream radix:radix) first |
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1348 |
|
6692 | 1349 |
" |
1350 |
Number readMantissaFrom:'234' readStream radix:10. |
|
1351 |
Number readMantissaFrom:'2' readStream radix:10. |
|
1352 |
Number readMantissaFrom:'234567' readStream radix:10. |
|
1353 |
" |
|
1354 |
||
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1355 |
"Modified: / 14.4.1998 / 18:47:47 / cg" |
701 | 1356 |
! ! |
1357 |
||
8892 | 1358 |
!Number class methodsFor:'queries'! |
1359 |
||
24322 | 1360 |
epsilon |
1361 |
"return the maximum relative spacing of instances of mySelf |
|
1362 |
(i.e. the value-delta of the least significant bit)" |
|
1363 |
||
1364 |
^ self subclassResponsibility |
|
1365 |
! |
|
1366 |
||
1367 |
epsilonForCloseTo |
|
1368 |
"return the epsilon used in the closeTo: comparison. |
|
1369 |
(useful would be something like self epsilon or epsilon*10, |
|
1370 |
but for Squeak compatibility.... - sigh)" |
|
1371 |
||
1372 |
^ 0.0001 |
|
1373 |
||
1374 |
" |
|
1375 |
Float epsilon |
|
1376 |
ShortFloat epsilon |
|
1377 |
Float epsilon10 |
|
1378 |
ShortFloat epsilon10 |
|
1379 |
" |
|
1380 |
! |
|
1381 |
||
8892 | 1382 |
isAbstract |
11224 | 1383 |
"Return if this class is an abstract class. |
1384 |
True is returned for Number here; false for subclasses. |
|
19457 | 1385 |
Abstract subclasses must redefine this again." |
11224 | 1386 |
|
8892 | 1387 |
^ self == Number |
1388 |
! ! |
|
1389 |
||
25144 | 1390 |
|
7261 | 1391 |
!Number methodsFor:'Compatibility-Squeak'! |
4988 | 1392 |
|
1393 |
asSmallAngleDegrees |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1394 |
"Return the receiver normalized to lie within the range (-180, 180)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1395 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1396 |
| pos | |
4988 | 1397 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1398 |
pos := self \\ 360. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1399 |
pos > 180 ifTrue: [pos := pos - 360]. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1400 |
^ pos |
4988 | 1401 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1402 |
" |
20308 | 1403 |
#(-500 -300 -150 -5 0 5 150 300 500 1200) |
1404 |
collect: [:n | n asSmallAngleDegrees] |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1405 |
" |
6072 | 1406 |
! |
4988 | 1407 |
|
23323 | 1408 |
sqrtWithErrorLessThan:epsilon |
1409 |
"compute the square root, using the Newton method. |
|
1410 |
The approximated return value has an error less than the given epsilon." |
|
1411 |
||
1412 |
^ self sqrt_withAccuracy:epsilon |
|
1413 |
||
1414 |
" |
|
1415 |
(2 asFixedPoint:4) sqrtWithErrorLessThan:0.001 |
|
1416 |
" |
|
1417 |
||
1418 |
"Modified: / 25-07-2017 / 15:58:46 / cg" |
|
1419 |
! ! |
|
1420 |
||
25122 | 1421 |
|
23323 | 1422 |
!Number methodsFor:'coercing & converting'! |
1423 |
||
1424 |
i |
|
1425 |
"return a complex number, with the receiver as imaginary part, 0 as real part" |
|
1426 |
||
1427 |
^ Complex real:0 imaginary:self |
|
1428 |
||
1429 |
" |
|
1430 |
3i -> (0+3i) |
|
1431 |
(1+1i) -> (1+1i) |
|
1432 |
" |
|
1433 |
||
1434 |
"Modified (format): / 22-09-2017 / 09:53:27 / cg" |
|
1435 |
! ! |
|
1436 |
||
1437 |
!Number methodsFor:'comparing'! |
|
1438 |
||
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1439 |
closeFrom:aNumber |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1440 |
"are these two numbers close?" |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1441 |
|
10727 | 1442 |
^ self closeFrom:aNumber withEpsilon:(self class epsilonForCloseTo) |
1443 |
||
1444 |
" |
|
20308 | 1445 |
9.0 closeTo: 8.9999 |
1446 |
9.9 closeTo: 9 |
|
1447 |
(9/3) closeTo: 2.9999 |
|
1448 |
1 closeTo: 0.9999 |
|
1449 |
1 closeTo: 1.0001 |
|
1450 |
1 closeTo: 1.001 |
|
1451 |
1 closeTo: 0.999 |
|
1452 |
||
1453 |
0.9999 closeTo: 1 |
|
1454 |
1.0001 closeTo: 1 |
|
1455 |
1.001 closeTo: 1 |
|
10727 | 1456 |
0.999 closeTo: 1 |
1457 |
Float NaN closeTo:Float NaN |
|
1458 |
Float infinity closeTo:Float infinity |
|
1459 |
" |
|
1460 |
! |
|
1461 |
||
1462 |
closeFrom:aNumber withEpsilon:eps |
|
1463 |
"are these two numbers close?" |
|
1464 |
||
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1465 |
| fuzz | |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1466 |
|
20308 | 1467 |
self isNaN == aNumber isNaN ifFalse: [^ false]. |
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1468 |
self isInfinite == aNumber isInfinite ifFalse: [^ false]. |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1469 |
|
20308 | 1470 |
fuzz := (self abs max:aNumber abs) * eps. |
24494 | 1471 |
fuzz isFinite ifFalse:[^ false]. |
1472 |
||
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1473 |
^ (self - aNumber) abs <= fuzz |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1474 |
|
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1475 |
" |
20308 | 1476 |
9.0 closeTo: 8.9999 |
23322 | 1477 |
9.0 closeTo: 8.9999 withEpsilon:0.1 |
1478 |
9.0 closeTo: 8.9999 withEpsilon:0.0001 |
|
1479 |
9.0 closeTo: 8.9999 withEpsilon:0.00001 |
|
1480 |
||
20308 | 1481 |
9.9 closeTo: 9 |
23322 | 1482 |
9.9 closeTo: 9 withEpsilon:1 |
1483 |
||
20308 | 1484 |
(9/3) closeTo: 2.9999 |
1485 |
1 closeTo: 0.9999 |
|
23322 | 1486 |
1 closeTo: 0.9999 withEpsilon:0.0001 |
1487 |
1 closeTo: 0.9999 withEpsilon:0.00001 |
|
1488 |
||
20308 | 1489 |
1 closeTo: 1.0001 |
1490 |
1 closeTo: 1.001 |
|
1491 |
1 closeTo: 0.999 |
|
24494 | 1492 |
|
1493 |
170 closeTo:(170 raisedTo:240) |
|
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1494 |
" |
23322 | 1495 |
|
1496 |
"Modified (comment): / 31-08-2018 / 12:24:39 / Claus Gittinger" |
|
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1497 |
! |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1498 |
|
6143 | 1499 |
closeTo:num |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1500 |
"are these two numbers close to each other?" |
6143 | 1501 |
|
10727 | 1502 |
^ self closeTo:num withEpsilon:(self class epsilonForCloseTo) |
1503 |
||
1504 |
" |
|
20308 | 1505 |
1 closeTo:1.0000000001 |
10727 | 1506 |
1 closeTo:1.001 |
1507 |
1 closeTo:1.001 withEpsilon:0.001 |
|
1508 |
" |
|
1509 |
||
1510 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
1511 |
! |
|
1512 |
||
1513 |
closeTo:num withEpsilon:eps |
|
1514 |
"are these two numbers close to each other?" |
|
1515 |
||
6374 | 1516 |
num isNumber ifFalse:[^false]. |
10727 | 1517 |
^ num closeFrom:self withEpsilon:eps |
6143 | 1518 |
|
1519 |
" |
|
20308 | 1520 |
1 closeTo:1.0000000001 |
1521 |
1 closeTo:1.001 |
|
1522 |
||
1523 |
1 closeTo:1.001 withEpsilon:0.1 |
|
1524 |
1 closeTo:1.201 withEpsilon:0.1 |
|
12979
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1525 |
|
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1526 |
3.14 closeTo:(3.14 asFixedPoint:2) |
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1527 |
(3.14 asFixedPoint:2) closeTo:3.14 |
6143 | 1528 |
" |
6155 | 1529 |
|
12979
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1530 |
"Created: / 05-11-2001 / 18:07:26 / cg" |
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1531 |
"Modified: / 02-08-2010 / 13:27:22 / cg" |
6143 | 1532 |
! |
1533 |
||
24322 | 1534 |
epsilonForCloseTo |
1535 |
"return the epsilon used in the closeTo: comparison." |
|
1536 |
||
1537 |
^ self class epsilonForCloseTo |
|
1538 |
||
1539 |
" |
|
1540 |
1.0 epsilon |
|
1541 |
10 asShortFloat epsilon |
|
1542 |
" |
|
1543 |
||
1544 |
"Created: / 10-06-2019 / 21:20:18 / Claus Gittinger" |
|
1545 |
! |
|
1546 |
||
20308 | 1547 |
isAlmostEqualTo:aNumber nEpsilon:nE |
16656 | 1548 |
"return true, if the argument, aNumber represents almost the same numeric value |
1549 |
as the receiver, false otherwise. |
|
1550 |
||
1551 |
nE is the number of minimal float distances, that the numbers may differ and |
|
17207 | 1552 |
still be considered equal. See documentation in LimitedPrecisionReal for more detail. |
16656 | 1553 |
|
20308 | 1554 |
For background information why floats need this |
16656 | 1555 |
read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
1556 |
" |
|
1557 |
||
1558 |
|f1 f2 diff scaledEpsilon largest| |
|
1559 |
||
1560 |
diff := (self - aNumber) abs. |
|
1561 |
||
1562 |
scaledEpsilon := nE * diff class epsilon. |
|
1563 |
||
1564 |
diff <= scaledEpsilon ifTrue:[ |
|
21812 | 1565 |
"compare for really close values near 0" |
1566 |
^ true. |
|
16656 | 1567 |
]. |
21812 | 1568 |
|
1569 |
"scaled comparison for larger values" |
|
1570 |
f1 := self abs. |
|
1571 |
f2 := aNumber abs. |
|
1572 |
largest := f1 > f2 ifTrue:[f1] ifFalse:[f2]. |
|
1573 |
^ (diff <= (scaledEpsilon * largest)). |
|
1574 |
||
1575 |
"Modified: / 15-06-2017 / 09:55:15 / cg" |
|
22290 | 1576 |
! |
1577 |
||
1578 |
isAlmostEqualTo:aNumber withError:errFraction |
|
1579 |
"return true, if the receiver,is inside the interval aNumber-err .. aNumber+err. |
|
1580 |
Err is a fraction of 0..1." |
|
1581 |
||
1582 |
^ self between:(aNumber * (1-errFraction)) and:(aNumber * (1+errFraction)) |
|
1583 |
||
1584 |
" |
|
1585 |
within 10%? |
|
1586 |
10.5 isAlmostEqualTo:10 withError:0.1 |
|
1587 |
||
1588 |
within 1% |
|
1589 |
10.5 isAlmostEqualTo:10 withError:0.01 |
|
1590 |
" |
|
1591 |
||
1592 |
"Created: / 23-09-2017 / 16:33:26 / cg" |
|
23323 | 1593 |
! |
1594 |
||
1595 |
isEqual: aNumber within: accuracy |
|
1596 |
^(self - aNumber) abs < accuracy |
|
16656 | 1597 |
! ! |
1598 |
||
701 | 1599 |
!Number methodsFor:'converting'! |
1600 |
||
20308 | 1601 |
% aNumber |
1602 |
"Return a complex number with the receiver as the real part and |
|
3634 | 1603 |
aNumber as the imaginary part" |
1604 |
||
1605 |
^ Complex real:self imaginary:aNumber |
|
1606 |
||
1607 |
"Modified: / 9.7.1998 / 10:18:12 / cg" |
|
1608 |
! |
|
1609 |
||
11458 | 1610 |
+/- anError |
14009 | 1611 |
"return a MeasurementValue with a given error." |
11458 | 1612 |
|
1613 |
^ MeasurementValue value:self minValue:(self-anError) maxValue:(self+anError) |
|
1614 |
||
1615 |
" |
|
20308 | 1616 |
(100 +/- 5) * 2 |
1617 |
(100 +/- 5) * (100 +/- 10) |
|
1618 |
(100 +/- 5) + (100 +/- 10) |
|
1619 |
(100 +/- 5) - (100 +/- 10) |
|
11458 | 1620 |
" |
14009 | 1621 |
|
1622 |
"Modified (comment): / 14-02-2012 / 14:17:36 / cg" |
|
11458 | 1623 |
! |
1624 |
||
701 | 1625 |
@ aNumber |
1626 |
"return a Point with the receiver as x-coordinate and the argument |
|
1627 |
as y-coordinate" |
|
1628 |
||
1629 |
%{ /* NOCONTEXT */ |
|
1630 |
||
1631 |
/* |
|
1632 |
* I cannot tell if this special code is worth anything |
|
1633 |
*/ |
|
5322 | 1634 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
20308 | 1635 |
OBJ newPoint; |
1636 |
int spc; |
|
1637 |
||
1638 |
__qCheckedNew(newPoint, sizeof(struct __Point)); |
|
1639 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
1640 |
__qSTORE(newPoint, @global(Point)); |
|
1641 |
__PointInstPtr(newPoint)->p_x = self; |
|
1642 |
__PointInstPtr(newPoint)->p_y = aNumber; |
|
1643 |
if (! __bothSmallInteger(self, aNumber)) { |
|
1644 |
spc = __qSpace(newPoint); |
|
1645 |
__STORE_SPC(newPoint, aNumber, spc); |
|
1646 |
__STORE_SPC(newPoint, self, spc); |
|
1647 |
} |
|
1648 |
RETURN ( newPoint ); |
|
701 | 1649 |
} |
1650 |
%} |
|
1651 |
. |
|
1652 |
^ Point x:self y:aNumber |
|
1653 |
! |
|
1654 |
||
3634 | 1655 |
asComplex |
20308 | 1656 |
"Return a complex number with the receiver as the real part and |
3634 | 1657 |
zero as the imaginary part" |
1658 |
||
7356 | 1659 |
^ Complex real:self |
3634 | 1660 |
|
1661 |
"Modified: / 9.7.1998 / 10:18:16 / cg" |
|
1662 |
! |
|
1663 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1664 |
asMetaNumber |
21894 | 1665 |
self isNaN ifTrue:[ |
1666 |
^ NotANumber NaN |
|
1667 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1668 |
^ SomeNumber new realNumber:self |
21894 | 1669 |
|
1670 |
" |
|
1671 |
Float NaN asMetaNumber |
|
1672 |
Float infinity asMetaNumber |
|
1673 |
Float negativeInfinity asMetaNumber |
|
1674 |
" |
|
1675 |
||
1676 |
"Modified (comment): / 21-06-2017 / 20:47:59 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1677 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1678 |
|
7214 | 1679 |
asNumber |
17060 | 1680 |
"I am a number, so return myself" |
7214 | 1681 |
|
1682 |
^ self. |
|
1683 |
! |
|
1684 |
||
19702 | 1685 |
asPercentFrom:fullAmount |
1686 |
"what is the percentage |
|
1687 |
taking the receiver's value from the argument" |
|
20308 | 1688 |
|
19702 | 1689 |
^ (self / fullAmount) * 100. |
19701 | 1690 |
|
1691 |
" |
|
1692 |
20 asPercentFrom:100 |
|
1693 |
(10 asPercentFrom:156) asFixedPoint:2 |
|
19702 | 1694 |
(15.6 asPercentFrom:156) asFixedPoint:2 |
19701 | 1695 |
" |
1696 |
! |
|
1697 |
||
701 | 1698 |
asPoint |
20308 | 1699 |
"return a new Point with the receiver as all coordinates; |
1700 |
often used to supply the same value in two dimensions, as with |
|
701 | 1701 |
symmetrical gridding or scaling." |
1702 |
||
1703 |
%{ /* NOCONTEXT */ |
|
1704 |
||
5322 | 1705 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
20308 | 1706 |
OBJ newPoint; |
1707 |
||
1708 |
__qCheckedNew(newPoint, sizeof(struct __Point)); |
|
1709 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
1710 |
__qSTORE(newPoint, @global(Point)); |
|
1711 |
__PointInstPtr(newPoint)->p_x = self; |
|
1712 |
__PointInstPtr(newPoint)->p_y = self; |
|
1713 |
__STORE(newPoint, self); |
|
1714 |
RETURN ( newPoint ); |
|
701 | 1715 |
} |
1716 |
%}. |
|
1717 |
^ Point x:self y:self |
|
1718 |
! |
|
1719 |
||
13898 | 1720 |
asTimeDuration |
23619 | 1721 |
"return an TimeDuration object from the receiver, |
1722 |
taking the receiver as number of seconds" |
|
13898 | 1723 |
|
20308 | 1724 |
^ TimeDuration seconds:self |
13898 | 1725 |
|
1726 |
" |
|
20308 | 1727 |
5 asTimeDuration |
1728 |
50.25 asTimeDuration |
|
1729 |
3600 asTimeDuration |
|
23619 | 1730 |
'5m' asTimeDuration |
13898 | 1731 |
" |
1732 |
||
1733 |
"Created: / 08-01-2012 / 19:04:04 / cg" |
|
23619 | 1734 |
"Modified (comment): / 21-01-2019 / 10:31:15 / Claus Gittinger" |
13898 | 1735 |
! |
1736 |
||
701 | 1737 |
degreesToRadians |
21105 | 1738 |
"interpreting the receiver as degrees, return the radians" |
701 | 1739 |
|
8203 | 1740 |
^ self * (Float pi / 180.0) |
7388
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1741 |
|
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1742 |
" |
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1743 |
180 degreesToRadians |
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1744 |
Float pi radiansToDegrees |
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1745 |
" |
701 | 1746 |
! |
1747 |
||
1748 |
literalArrayEncoding |
|
1245 | 1749 |
"encode myself as an array literal, from which a copy of the receiver |
1750 |
can be reconstructed with #decodeAsLiteralArray." |
|
701 | 1751 |
|
1752 |
^ self |
|
1753 |
||
1754 |
"Modified: 1.9.1995 / 02:25:26 / claus" |
|
1245 | 1755 |
"Modified: 22.4.1996 / 13:00:27 / cg" |
701 | 1756 |
! |
1757 |
||
19701 | 1758 |
percentOf:hundredPercent |
1759 |
"how many is self-percent from the argument" |
|
20308 | 1760 |
|
19701 | 1761 |
^ (hundredPercent / 100 * self) |
1762 |
||
1763 |
" |
|
1764 |
20 percentOf:100 |
|
1765 |
(10 percentOf:156) asFixedPoint:2 |
|
1766 |
(105 percentOf:156) asFixedPoint:2 |
|
1767 |
" |
|
1768 |
! |
|
1769 |
||
701 | 1770 |
radiansToDegrees |
21105 | 1771 |
"interpreting the receiver as radians, return the degrees" |
701 | 1772 |
|
8203 | 1773 |
^ self * (180.0 / Float pi) |
7388
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1774 |
|
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1775 |
" |
20308 | 1776 |
180 degreesToRadians |
7388
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1777 |
Float pi radiansToDegrees |
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1778 |
" |
6637 | 1779 |
! |
1780 |
||
1781 |
withScale:newScale |
|
20308 | 1782 |
"return a fixedPoint number representing the same value as the receiver, |
6637 | 1783 |
with newScale number of post-decimal digits" |
1784 |
||
1785 |
^ self asFixedPoint:newScale |
|
1786 |
||
1787 |
" |
|
20308 | 1788 |
1234 withScale:2 |
1789 |
1234.1 withScale:2 |
|
1790 |
1234.12 withScale:2 |
|
1791 |
1234.123 withScale:2 |
|
6637 | 1792 |
(1/7) withScale:2 |
1793 |
" |
|
701 | 1794 |
! ! |
1795 |
||
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1796 |
!Number methodsFor:'converting-times'! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1797 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1798 |
days |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1799 |
"return a TimeDuration representing this number of days" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1800 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1801 |
^ TimeDuration days:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1802 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1803 |
" |
20308 | 1804 |
1000 milliseconds |
1805 |
10 seconds |
|
1806 |
10 minutes |
|
1807 |
1 days |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1808 |
" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1809 |
! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1810 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1811 |
hours |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1812 |
"return a TimeDuration representing this number of hours" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1813 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1814 |
^ TimeDuration hours:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1815 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1816 |
" |
20308 | 1817 |
1000 milliseconds |
1818 |
10 seconds |
|
1819 |
10 minutes |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1820 |
" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1821 |
! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1822 |
|
22844 | 1823 |
microseconds |
1824 |
"return a TimeDuration representing this number of microseconds." |
|
1825 |
||
1826 |
^ TimeDuration fromMicroseconds:self |
|
1827 |
||
1828 |
" |
|
1829 |
40 microseconds |
|
1830 |
" |
|
1831 |
||
1832 |
"Modified (comment): / 21-09-2017 / 17:37:18 / cg" |
|
1833 |
! |
|
1834 |
||
22279 | 1835 |
milliSeconds |
1836 |
"return a TimeDuration representing this number of milliseconds |
|
24431 | 1837 |
Same as milliseconds, for dialect compatibility" |
22279 | 1838 |
|
1839 |
^ TimeDuration fromMilliseconds:self |
|
1840 |
||
1841 |
" |
|
1842 |
1000 milliSeconds |
|
1843 |
" |
|
1844 |
||
1845 |
"Created: / 21-09-2017 / 17:26:32 / cg" |
|
24431 | 1846 |
"Modified (comment): / 18-07-2019 / 21:33:09 / Claus Gittinger" |
22279 | 1847 |
! |
1848 |
||
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1849 |
milliseconds |
22279 | 1850 |
"return a TimeDuration representing this number of milliseconds. |
24431 | 1851 |
Same as milliSeconds, for dialect compatibility" |
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1852 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1853 |
^ TimeDuration fromMilliseconds:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1854 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1855 |
" |
20308 | 1856 |
1000 milliseconds |
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1857 |
" |
22279 | 1858 |
|
1859 |
"Modified (comment): / 21-09-2017 / 17:37:18 / cg" |
|
24431 | 1860 |
"Modified (comment): / 18-07-2019 / 21:33:12 / Claus Gittinger" |
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1861 |
! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1862 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1863 |
minutes |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1864 |
"return a TimeDuration representing this number of minutes" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1865 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1866 |
^ TimeDuration minutes:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1867 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1868 |
" |
20308 | 1869 |
1000 milliseconds |
1870 |
10 seconds |
|
1871 |
10 minutes |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1872 |
" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1873 |
! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1874 |
|
22844 | 1875 |
nanoseconds |
1876 |
"return a TimeDuration representing this number of nanoseconds." |
|
1877 |
||
1878 |
^ TimeDuration fromNanoseconds:self |
|
1879 |
||
1880 |
" |
|
22869 | 1881 |
40.5 nanoseconds asPicoseconds |
1882 |
(40.5 nanoseconds / 2) asPicoseconds |
|
1883 |
||
22844 | 1884 |
40 nanoseconds |
22869 | 1885 |
40 nanoseconds asPicoseconds |
1886 |
44 milliseconds asMicroseconds |
|
1887 |
44 milliseconds |
|
22844 | 1888 |
" |
1889 |
||
1890 |
"Modified (comment): / 21-09-2017 / 17:37:18 / cg" |
|
1891 |
! |
|
1892 |
||
1893 |
picoseconds |
|
1894 |
"return a TimeDuration representing this number of picoseconds." |
|
1895 |
||
1896 |
^ TimeDuration fromPicoseconds:self |
|
1897 |
||
1898 |
" |
|
1899 |
40 picoseconds |
|
1900 |
" |
|
1901 |
||
1902 |
"Modified (comment): / 21-09-2017 / 17:37:18 / cg" |
|
1903 |
! |
|
1904 |
||
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1905 |
seconds |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1906 |
"return a TimeDuration representing this number of seconds" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1907 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1908 |
^ TimeDuration seconds:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1909 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1910 |
" |
20308 | 1911 |
1000 milliseconds |
1912 |
10 seconds |
|
1913 |
10 minutes |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1914 |
" |
13631 | 1915 |
! |
1916 |
||
1917 |
weeks |
|
1918 |
"return a TimeDuration representing this number of weeks" |
|
1919 |
||
1920 |
^ TimeDuration weeks:self |
|
1921 |
||
1922 |
" |
|
20308 | 1923 |
1000 milliseconds |
1924 |
10 seconds |
|
1925 |
10 minutes |
|
1926 |
1 days |
|
1927 |
1 weeks |
|
13631 | 1928 |
" |
1929 |
||
1930 |
"Created: / 05-09-2011 / 11:17:59 / cg" |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1931 |
! ! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1932 |
|
16406 | 1933 |
!Number methodsFor:'double dispatching'! |
1934 |
||
1935 |
differenceFromTimestamp:aTimestamp |
|
1936 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
1937 |
before aTimestamp" |
|
1938 |
||
1939 |
^ aTimestamp subtractMilliseconds:(self * 1000) truncated. |
|
1940 |
||
1941 |
" |
|
20308 | 1942 |
100.0 differenceFromTimestamp:Timestamp now |
16406 | 1943 |
|
1944 |
|t1 t2| |
|
20308 | 1945 |
t1 := Timestamp now. |
16406 | 1946 |
t2 := 1.5 differenceFromTimestamp:t1. |
1947 |
t1 inspect. t2 inspect. |
|
1948 |
" |
|
1949 |
! ! |
|
1950 |
||
24237 | 1951 |
!Number methodsFor:'inspecting'! |
1952 |
||
1953 |
inspectorValueStringInListFor:anInspector |
|
1954 |
"returns a string to be shown in the inspector's list" |
|
1955 |
||
1956 |
^ self printString contractTo:30 |
|
1957 |
||
1958 |
"Created: / 29-05-2019 / 15:01:19 / Claus Gittinger" |
|
1959 |
! ! |
|
23615 | 1960 |
|
701 | 1961 |
!Number methodsFor:'intervals'! |
1962 |
||
6072 | 1963 |
downTo:stop |
1964 |
"return an interval from receiver down to the argument, incrementing by -1" |
|
1965 |
||
1966 |
^ self to:stop by:-1 |
|
1967 |
||
1968 |
" |
|
1969 |
(10 downTo:1) do:[:i | Transcript showCR:i]. |
|
1970 |
" |
|
1971 |
! |
|
1972 |
||
15595 | 1973 |
downTo:stop by:step |
1974 |
"return an interval from receiver down to the argument, decrementing by step" |
|
1975 |
||
1976 |
^ self to:stop by:step negated |
|
1977 |
||
1978 |
" |
|
1979 |
(10 downTo:1 by:0.5) do:[:i | Transcript showCR:i]. |
|
1980 |
" |
|
1981 |
||
1982 |
"Created: / 01-08-2013 / 14:36:56 / cg" |
|
1983 |
! |
|
1984 |
||
701 | 1985 |
to:stop |
1986 |
"return an interval from receiver up to the argument, incrementing by 1" |
|
1987 |
||
1988 |
^ Interval from:self to:stop |
|
1989 |
! |
|
1990 |
||
1991 |
to:stop by:step |
|
1992 |
"return an interval from receiver up to the argument, incrementing by step" |
|
1993 |
||
1994 |
^ Interval from:self to:stop by:step |
|
6154 | 1995 |
! |
1996 |
||
1997 |
to:stop byFactor:factor |
|
1998 |
"return a geometric series from receiver up to the argument; |
|
1999 |
elements have a constant factor in between" |
|
2000 |
||
2001 |
^ GeometricSeries from:self to:stop byFactor:factor |
|
2002 |
||
2003 |
" |
|
2004 |
(1 to:256 byFactor:2) |
|
20308 | 2005 |
(256 to:1 byFactor:1/2) |
6154 | 2006 |
" |
701 | 2007 |
! ! |
2008 |
||
2009 |
!Number methodsFor:'iteration'! |
|
2010 |
||
2011 |
timesRepeat:aBlock |
|
2012 |
"evaluate the argument, aBlock self times" |
|
2013 |
||
2014 |
|count| |
|
2015 |
||
2016 |
count := self. |
|
2017 |
[count > 0] whileTrue:[ |
|
20308 | 2018 |
aBlock value. |
2019 |
count := count - 1 |
|
701 | 2020 |
] |
24394 | 2021 |
! |
2022 |
||
2023 |
timesRepeatWithExit:aOneArgBlock |
|
2024 |
"evaluate the argument, aBlock self times; |
|
2025 |
pass an exit block to the one-arg-block" |
|
2026 |
||
2027 |
|count exit| |
|
2028 |
||
24395 | 2029 |
exit := [:values | ^ values firstIfEmpty:nil] asVarArgBlock. |
24394 | 2030 |
count := self. |
2031 |
[count > 0] whileTrue:[ |
|
2032 |
aOneArgBlock value:exit. |
|
2033 |
count := count - 1 |
|
2034 |
] |
|
2035 |
||
2036 |
" |
|
2037 |
10 timesRepeatWithExit:[:exit | |
|
2038 |
Transcript showCR:'iteration'. |
|
2039 |
(Random nextBetween:1 and:10) > 7 ifTrue:[exit value]. |
|
2040 |
]. |
|
2041 |
" |
|
2042 |
||
2043 |
"Created: / 28-06-2019 / 12:01:24 / Claus Gittinger" |
|
701 | 2044 |
! ! |
2045 |
||
3634 | 2046 |
!Number methodsFor:'mathematical functions'! |
2047 |
||
21982 | 2048 |
agm:y |
2049 |
"return the arithmetic-geometric mean agm(x, y) of the receiver (x) and the argument, y. |
|
2050 |
See https://en.wikipedia.org/wiki/Arithmetic-geometric_mean |
|
2051 |
and http://www.wolframalpha.com/input/?i=agm(24,+6)" |
|
2052 |
||
2053 |
|ai an gi gn epsilon delta| |
|
2054 |
||
2055 |
ai := (self + y) / 2. |
|
2056 |
gi := (self * y) sqrt. |
|
2057 |
epsilon := self epsilon. |
|
2058 |
||
2059 |
[ |
|
2060 |
an := (ai + gi) / 2. |
|
2061 |
gn := (ai * gi) sqrt. |
|
2062 |
delta := (an - ai) abs. |
|
2063 |
ai := an. |
|
2064 |
gi := gn. |
|
2065 |
] doUntil:[ delta < epsilon ]. |
|
2066 |
^ ai |
|
2067 |
||
2068 |
" |
|
2069 |
24 agm:6 |
|
2070 |
" |
|
2071 |
||
2072 |
"Created: / 03-07-2017 / 12:05:00 / cg" |
|
2073 |
! |
|
2074 |
||
17190 | 2075 |
cbrt |
2076 |
"return the cubic root of the receiver" |
|
2077 |
||
19770 | 2078 |
"/ if I am not a Float (or a less general lpReal), |
2079 |
"/ retry after converting to float |
|
17190 | 2080 |
(self isLimitedPrecisionReal not |
2081 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 2082 |
|f| |
2083 |
||
2084 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
2085 |
^ f cbrt. |
|
2086 |
]. |
|
17190 | 2087 |
]. |
19770 | 2088 |
"/ very slow fallback |
17190 | 2089 |
^ self cbrt_withAccuracy:self epsilon |
21988 | 2090 |
|
2091 |
"Modified: / 05-07-2017 / 17:23:27 / cg" |
|
17190 | 2092 |
! |
2093 |
||
3634 | 2094 |
conjugated |
2095 |
"Return the complex conjugate of this Number." |
|
2096 |
||
2097 |
^ self |
|
2098 |
||
2099 |
"Modified: / 9.7.1998 / 10:17:31 / cg" |
|
2100 |
! |
|
2101 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2102 |
exp |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2103 |
"compute e**x of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2104 |
|
19770 | 2105 |
"/ if I am not a Float (or a less general lpReal), |
2106 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2107 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2108 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 2109 |
|f| |
2110 |
||
2111 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
2112 |
^ f exp. |
|
2113 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2114 |
]. |
19770 | 2115 |
"/ very slow fallback |
8634 | 2116 |
^ self exp_withAccuracy:self epsilon |
21915 | 2117 |
|
21988 | 2118 |
"Modified: / 05-07-2017 / 17:23:36 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2119 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2120 |
|
24934 | 2121 |
fibPhi |
2122 |
^ (1 / 5 sqrt) * ((self class phi raisedTo:self) - ((-1 / self class phi) raisedTo:self)) |
|
2123 |
||
2124 |
" |
|
2125 |
3 fib |
|
2126 |
3.0 fibPhi |
|
2127 |
100 fib |
|
2128 |
100.0 fibPhi |
|
2129 |
" |
|
2130 |
! |
|
2131 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2132 |
floorLog:radix |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2133 |
"return the logarithm truncated as an integer" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2134 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2135 |
^ (self log:radix) floor |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2136 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2137 |
|
3634 | 2138 |
imaginary |
23046 | 2139 |
"Return the imaginary part of a complex number. |
2140 |
For non-complex numbers, zero is returned." |
|
3634 | 2141 |
|
2142 |
^ 0 |
|
2143 |
||
23046 | 2144 |
"Modified: / 09-07-1998 / 10:17:24 / cg" |
2145 |
"Modified (comment): / 01-06-2018 / 13:09:09 / Claus Gittinger" |
|
3634 | 2146 |
! |
2147 |
||
21833 | 2148 |
ldexp:exp |
2149 |
"multiply the receiver by an integral power of 2. |
|
24971 | 2150 |
I.e. return self * (2 ^ exp). |
2151 |
This is also the operation to reconstruct the original float from its |
|
2152 |
mantissa and exponent: (f mantissa ldexp:f exponent) = f" |
|
21833 | 2153 |
|
2154 |
^ self * (2 raisedTo:exp) |
|
2155 |
||
24959 | 2156 |
" |
2157 |
1.0 ldexp:16 -> 65536.0 |
|
2158 |
1.0 ldexp:100 -> 1.26765060022823E+30 |
|
2159 |
1 * (2 raisedToInteger:100) -> 1267650600228229401496703205376 |
|
2160 |
1 ldexp:200 -> 1606938044258990275541962092341162602522202993782792835301376 |
|
2161 |
1.0 ldexp:200 -> 1.60693804425899E+60 |
|
2162 |
" |
|
2163 |
||
21833 | 2164 |
"Created: / 19-06-2017 / 01:43:35 / cg" |
2165 |
! |
|
2166 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2167 |
ln |
18862 | 2168 |
"return the natural logarithm of myself. |
2169 |
Raises an exception, if the receiver is less or equal to zero." |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2170 |
|
19770 | 2171 |
"/ if I am not a Float (or a less general lpReal), |
2172 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2173 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2174 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 2175 |
|f| |
2176 |
||
2177 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
2178 |
^ f ln. |
|
2179 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2180 |
]. |
19770 | 2181 |
"/ very slow fallback |
8634 | 2182 |
^ self ln_withAccuracy:self epsilon |
19249 | 2183 |
|
2184 |
" |
|
21833 | 2185 |
(10 raisedTo:1000) ln |
19249 | 2186 |
" |
21833 | 2187 |
|
21988 | 2188 |
"Modified: / 05-07-2017 / 17:23:50 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2189 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2190 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2191 |
log |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2192 |
"return log base 10 of the receiver. |
12584 | 2193 |
Alias for log:10." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2194 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2195 |
^ self log10 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2196 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2197 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2198 |
log10 |
21828 | 2199 |
"return log base-10 of the receiver. |
2200 |
Raises an exception, if the receiver is less or equal to zero. |
|
2201 |
Here, fallback to the general logarithm code." |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2202 |
|
10550
0f2a07595d1c
Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents:
10325
diff
changeset
|
2203 |
(self isLimitedPrecisionReal not |
0f2a07595d1c
Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents:
10325
diff
changeset
|
2204 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 2205 |
|f| |
2206 |
||
2207 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
2208 |
^ f log10. |
|
2209 |
]. |
|
10550
0f2a07595d1c
Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents:
10325
diff
changeset
|
2210 |
]. |
21828 | 2211 |
^ self ln / self class ln10 |
19249 | 2212 |
|
2213 |
" |
|
21828 | 2214 |
(10 raisedTo:1000) log10 |
2215 |
(10 raisedTo:2000) log10 |
|
2216 |
(10 raisedTo:4000) log10 |
|
2217 |
(10 raisedTo:8000) log10 |
|
19249 | 2218 |
" |
21828 | 2219 |
|
21988 | 2220 |
"Modified: / 05-07-2017 / 17:23:06 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2221 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2222 |
|
24950 | 2223 |
log2 |
2224 |
"return log base-2 of the receiver. |
|
2225 |
Raises an exception, if the receiver is less or equal to zero. |
|
2226 |
Here, fallback to the general logarithm code." |
|
2227 |
||
2228 |
(self isLimitedPrecisionReal not |
|
2229 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
2230 |
|f| |
|
2231 |
||
2232 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
2233 |
^ f log2. |
|
2234 |
]. |
|
2235 |
]. |
|
2236 |
^ self ln / self class ln2 |
|
2237 |
||
2238 |
" |
|
2239 |
2.0 log2 |
|
2240 |
4.0 log2 |
|
2241 |
(2.0 raisedTo:100.0) log2 |
|
2242 |
(10 raisedTo:1000) log2 |
|
2243 |
(10 raisedTo:2000) log2 |
|
2244 |
(10 raisedTo:4000) log2 |
|
2245 |
(10 raisedTo:8000) log2 |
|
2246 |
" |
|
2247 |
! |
|
2248 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2249 |
log:aNumber |
18850 | 2250 |
"return log base aNumber of the receiver. |
2251 |
This will usually return a float value" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2252 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2253 |
^ self ln / aNumber ln |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2254 |
|
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2255 |
" |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2256 |
1000 log:10 |
19049 | 2257 |
9 log:3 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2258 |
(1000 log:10) floor |
19249 | 2259 |
(10 raisedTo:1000) log:10 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2260 |
" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2261 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2262 |
|
22098 | 2263 |
nthRoot:n |
2264 |
"return the nth root of the receiver" |
|
2265 |
||
2266 |
n == 2 ifTrue:[^ self sqrt ]. |
|
2267 |
n == 3 ifTrue:[^ self cbrt ]. |
|
2268 |
"/ slow fallback |
|
2269 |
^ self nthRoot:n withAccuracy:self epsilon |
|
2270 |
||
2271 |
" |
|
2272 |
10 nthRoot:2 -> 3.16227766016838 |
|
2273 |
10 nthRoot:3 -> 2.154434690031883722 |
|
22287 | 2274 |
|
2275 |
100.0 nthRoot:4 |
|
2276 |
100.0 nthRoot:5 |
|
2277 |
(100.0 nthRoot:6) raisedTo:6 |
|
2278 |
||
2279 |
16.0 nthRoot:2 |
|
2280 |
-16.0 nthRoot:3 |
|
2281 |
-16.0 nthRoot:5 |
|
2282 |
||
2283 |
-16.0 nthRoot:4 |
|
22098 | 2284 |
" |
2285 |
||
2286 |
"Created: / 25-07-2017 / 16:15:11 / cg" |
|
22287 | 2287 |
"Modified (comment): / 22-09-2017 / 10:15:33 / cg" |
22098 | 2288 |
! |
2289 |
||
7468
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2290 |
raisedTo:aNumber |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2291 |
"return the receiver raised to aNumber" |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2292 |
|
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2293 |
aNumber = 0 ifTrue:[^ 1]. |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2294 |
aNumber = 1 ifTrue:[^ self]. |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2295 |
aNumber isInteger ifTrue:[ |
20308 | 2296 |
^ self raisedToInteger:aNumber |
7468
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2297 |
]. |
19018 | 2298 |
aNumber isNumber ifFalse:[ |
20308 | 2299 |
^ aNumber raisedFromNumber:self. |
2300 |
]. |
|
7468
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2301 |
^ self asFloat raisedTo:aNumber |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2302 |
|
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2303 |
" |
19092 | 2304 |
2 raisedTo: 4 |
2305 |
-2 raisedTo: 4 |
|
2306 |
4 raisedTo: 1/2 |
|
2307 |
-4 raisedTo: 1/2 |
|
2308 |
8 raisedTo: 1/3 |
|
2309 |
-8 raisedTo: 1/3 |
|
20308 | 2310 |
10 raisedTo: 4 |
2311 |
10 raisedTo: -4 |
|
7468
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2312 |
" |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2313 |
! |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
2314 |
|
3634 | 2315 |
real |
23046 | 2316 |
"Return the real part of a complex number. |
2317 |
For non-complex numbers, the receiver is returned." |
|
3634 | 2318 |
|
2319 |
^ self |
|
2320 |
||
23046 | 2321 |
"Modified: / 09-07-1998 / 10:17:17 / cg" |
2322 |
"Modified (comment): / 01-06-2018 / 13:09:33 / Claus Gittinger" |
|
6258 | 2323 |
! |
2324 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2325 |
sqrt |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2326 |
"return the square root of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2327 |
|
19770 | 2328 |
"/ if I am not a Float (or a less general lpReal), |
2329 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2330 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2331 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 2332 |
|f| |
2333 |
||
22115 | 2334 |
"/ do not make this a LongFloat; |
22119 | 2335 |
"/ it will return a LongFloat then, and Image rotated will fail then... |
21992 | 2336 |
(f := self asFloat) isFinite ifTrue:[ |
21988 | 2337 |
^ f sqrt. |
2338 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2339 |
]. |
19770 | 2340 |
"/ very slow fallback |
8634 | 2341 |
^ self sqrt_withAccuracy:self epsilon |
21988 | 2342 |
|
21992 | 2343 |
"Modified (format): / 11-07-2017 / 13:32:02 / cg" |
22115 | 2344 |
"Modified (format): / 26-07-2017 / 12:30:16 / mawalch" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2345 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2346 |
|
6258 | 2347 |
timesTwoPower:anInteger |
12762 | 2348 |
"Return the receiver multiplied by 2 raised to the power of the argument. |
6258 | 2349 |
For protocol completeness wrt. Squeak and ST80." |
2350 |
||
12762 | 2351 |
anInteger >= 0 ifTrue:[ |
24157 | 2352 |
^ self * (1 bitShift:anInteger) |
12762 | 2353 |
]. |
2354 |
^ self / (1 bitShift:anInteger negated) |
|
6258 | 2355 |
|
2356 |
" |
|
24157 | 2357 |
123 timesTwoPower:0 = 123*1 -> 123 |
2358 |
123 timesTwoPower:1 = 123*2 -> 246 |
|
2359 |
123 timesTwoPower:2 = 123*4 -> 492 |
|
2360 |
123 timesTwoPower:3 = 123*8 -> 984 |
|
12762 | 2361 |
|
2362 |
(2 timesTwoPower: -150) timesTwoPower: 150 -> 2 |
|
24157 | 2363 |
(2 timesTwoPower: 150) timesTwoPower: -150 -> 2 |
2364 |
(2 timesTwoPower: 150) timesTwoPower: -149 -> 4 |
|
6258 | 2365 |
" |
24157 | 2366 |
|
2367 |
"Modified (comment): / 26-05-2019 / 03:26:09 / Claus Gittinger" |
|
3634 | 2368 |
! ! |
2369 |
||
11458 | 2370 |
!Number methodsFor:'measurement values'! |
2371 |
||
2372 |
maxValue |
|
20308 | 2373 |
"the maximum possible value taking me as a measurement with possible error; |
20421 | 2374 |
as I am exact, that's myself" |
11458 | 2375 |
|
2376 |
^ self |
|
2377 |
! |
|
2378 |
||
2379 |
minValue |
|
20308 | 2380 |
"the minimum possible value taking me as a measurement with possible error; |
20421 | 2381 |
as I am exact, that's myself" |
11458 | 2382 |
|
2383 |
^ self |
|
2384 |
! ! |
|
2385 |
||
25085 | 2386 |
|
2387 |
||
2388 |
||
2389 |
||
701 | 2390 |
!Number methodsFor:'printing & storing'! |
2391 |
||
18863 | 2392 |
displayOn:aGCOrStream |
2393 |
"return a string to display the receiver. |
|
2394 |
The output radix is usually 10, but can be changed by setting |
|
2395 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
2396 |
||
2397 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
21536 | 2398 |
"/ old ST80 means: draw-yourself on a GC. |
18863 | 2399 |
(aGCOrStream isStream) ifFalse:[ |
21536 | 2400 |
^ super displayOn:aGCOrStream |
18863 | 2401 |
]. |
2402 |
||
2403 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
|
21536 | 2404 |
self printOn:aGCOrStream |
18863 | 2405 |
] ifFalse:[ |
21536 | 2406 |
self printOn:aGCOrStream base:DefaultDisplayRadix showRadix:true. |
18863 | 2407 |
]. |
2408 |
||
2409 |
" |
|
2410 |
Integer displayRadix:16. 12345 |
|
2411 |
Integer displayRadix:2. 12345 |
|
2412 |
Integer displayRadix:10. 12345 |
|
2413 |
" |
|
21536 | 2414 |
|
2415 |
"Modified (comment): / 22-02-2017 / 16:52:16 / cg" |
|
18863 | 2416 |
! |
2417 |
||
18862 | 2418 |
printOn:aStream |
2419 |
"append a printed description of the receiver to aStream" |
|
2420 |
||
2421 |
self printOn:aStream base:10 |
|
2422 |
||
2423 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
|
2424 |
! |
|
2425 |
||
2426 |
printOn:aStream base:b |
|
2427 |
"return a string representation of the receiver in the specified |
|
2428 |
radix (without the initial XXr)" |
|
2429 |
||
2430 |
^ self printOn:aStream base:b showRadix:false |
|
2431 |
||
2432 |
" |
|
2433 |
10 printOn:Transcript base:3 |
|
2434 |
31 printOn:Transcript base:3 |
|
2435 |
-20 printOn:Transcript base:16 |
|
2436 |
-20 printOn:Transcript base:10 |
|
2437 |
3000 factorial printOn:Transcript base:10 |
|
2438 |
" |
|
2439 |
||
2440 |
"Modified: / 20.1.1998 / 18:05:02 / stefan" |
|
2441 |
"Modified: / 7.9.2001 / 13:52:17 / cg" |
|
2442 |
! |
|
2443 |
||
2444 |
printOn:aStream base:b showRadix:showRadix |
|
2445 |
"the central print method for integer. |
|
2446 |
Must be defined in concrete classes" |
|
2447 |
||
2448 |
self subclassResponsibility |
|
2449 |
! |
|
2450 |
||
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2451 |
printOn:aStream paddedWith:padCharacter to:size base:radix |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2452 |
|s| |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2453 |
|
8301 | 2454 |
radix == 10 ifTrue:[ |
20308 | 2455 |
s := self printString. |
8301 | 2456 |
] ifFalse:[ |
20308 | 2457 |
s := self printStringRadix:radix. |
8301 | 2458 |
]. |
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2459 |
s printOn: aStream leftPaddedTo:size with: padCharacter |
11098 | 2460 |
|
2461 |
" |
|
2462 |
100 printOn:Transcript paddedWith:$0 to:10 base:10. Transcript cr. |
|
2463 |
100 printOn:Transcript paddedWith:$0 to:10 base:16. Transcript cr. |
|
2464 |
100 printOn:Transcript paddedWith:(Character space) to:10 base:16. Transcript cr. |
|
2465 |
100 printOn:Transcript paddedWith:(Character space) to:10 base:2. Transcript cr. |
|
2466 |
" |
|
8301 | 2467 |
! |
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2468 |
|
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2469 |
printOn:aStream thousandsSeparator:thousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2470 |
"print the receiver as business number with thousands separator to aStream. |
11136
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2471 |
thousandsSeparator is locale specific and is usualy a single quote ('), a comma or period." |
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2472 |
|
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2473 |
|rest| |
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2474 |
|
11594
5440d97af571
#printStringWithThousandsSeparator fix for non-integers
Stefan Vogel <sv@exept.de>
parents:
11526
diff
changeset
|
2475 |
self >= 1000 ifTrue:[ |
20308 | 2476 |
(self // 1000) printOn:aStream thousandsSeparator:thousandsSeparator. |
2477 |
thousandsSeparator printOn:aStream. |
|
2478 |
rest := self \\ 1000. |
|
2479 |
rest < 100 ifTrue:[ |
|
2480 |
aStream nextPut:$0. |
|
2481 |
rest < 10 ifTrue:[ |
|
2482 |
aStream nextPut:$0. |
|
2483 |
]. |
|
2484 |
]. |
|
2485 |
rest printOn:aStream. |
|
2486 |
^ self. |
|
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2487 |
]. |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2488 |
self printOn:aStream. |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2489 |
|
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2490 |
" |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2491 |
swiss style: |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2492 |
1000000 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
20308 | 2493 |
12345678 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2494 |
1234567 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2495 |
123456 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
11136
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2496 |
123056 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2497 |
12345 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2498 |
1234 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2499 |
123 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2500 |
|
20308 | 2501 |
(12345678.12 asFixedPoint:2) printOn:Transcript thousandsSeparator:$'. Transcript cr. |
11136
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2502 |
1234567.12 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2503 |
123456.12 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2504 |
123056.12 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2505 |
12345.12 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2506 |
1234.12 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2507 |
123.12 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2508 |
|
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2509 |
us style: |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2510 |
1000000 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
20308 | 2511 |
12345678 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2512 |
1234567 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2513 |
123456 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2514 |
12345 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2515 |
1234 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2516 |
123 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2517 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2518 |
german (european ?) style |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2519 |
1000000 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
20308 | 2520 |
12345678 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2521 |
1234567 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2522 |
123456 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2523 |
12345 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2524 |
1234 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2525 |
123 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2526 |
" |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2527 |
! |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2528 |
|
11660 | 2529 |
printStringFormat:formatString |
23047 | 2530 |
"Return a printed representation of the receiver as specified by formatString, |
2531 |
which is defined by PrintfScanf." |
|
2532 |
||
2533 |
^ PrintfScanf printf:formatString argument:self. |
|
2534 |
"/ ^ self printfPrintString:formatString |
|
2535 |
||
2536 |
" |
|
2537 |
1.2345 printStringFormat:'%4.2f' -> '1.23' |
|
2538 |
123 printStringFormat:'%4d' -> ' 123' |
|
2539 |
123 printStringFormat:'%5.2f' -> '123.0' |
|
2540 |
" |
|
2541 |
||
2542 |
"Modified (comment): / 03-06-2018 / 09:15:01 / Claus Gittinger" |
|
11660 | 2543 |
! |
2544 |
||
20142 | 2545 |
printStringRadix:base |
2546 |
"return a string representation of the receiver in the specified |
|
2547 |
base; does NOT prepend XXr to the string. |
|
2548 |
See also: radixPrintStringRadix: |
|
20331 | 2549 |
printOn:base:showRadix:" |
20142 | 2550 |
|
2551 |
^ self printStringRadix:base showRadix:false |
|
11098 | 2552 |
|
2553 |
" |
|
20331 | 2554 |
10000000000000000000000000000000000000000000 printStringRadix:16 |
2555 |
-10000000000000000000000000000000000000000000 printStringRadix:16 |
|
11098 | 2556 |
" |
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2557 |
! |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2558 |
|
18862 | 2559 |
printStringRadix:base showRadix:showRadixBoolean |
23127 | 2560 |
"return a string representation of the receiver in the specified base; |
2561 |
optionally prepend XXr to the string. |
|
18862 | 2562 |
See also: radixPrintStringRadix: |
23127 | 2563 |
printOn:base:showRadix:" |
18862 | 2564 |
|
2565 |
|s| |
|
2566 |
||
2567 |
s := WriteStream on:(String basicNew:20). |
|
2568 |
self printOn:s base:base showRadix:showRadixBoolean. |
|
2569 |
^ s contents |
|
2570 |
||
20142 | 2571 |
" |
2572 |
10000000000000000000000000000000000000000000 printStringRadix:16 showRadix:false |
|
2573 |
10000000000000000000000000000000000000000000 printStringRadix:16 showRadix:true |
|
2574 |
" |
|
2575 |
||
18862 | 2576 |
"Created: / 23-09-2011 / 13:59:19 / cg" |
23127 | 2577 |
"Modified (comment): / 22-06-2018 / 09:56:17 / Claus Gittinger" |
18862 | 2578 |
! |
2579 |
||
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2580 |
printStringWithThousandsSeparator |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2581 |
"print the receiver as swiss business number with thousands separator to aStream. |
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2582 |
Caveat: Should use the separator from the locale here" |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2583 |
|
20866 | 2584 |
^ self printStringWithThousandsSeparator:(UserPreferences current thousandsSeparatorCharacter). |
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2585 |
|
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2586 |
" |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2587 |
1000000 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2588 |
12345678 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2589 |
1234567 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2590 |
123456 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2591 |
12345 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2592 |
1234 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2593 |
123 printStringWithThousandsSeparator |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2594 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2595 |
1000000 asFixedPoint printStringWithThousandsSeparator |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2596 |
12345678 asFixedPoint printStringWithThousandsSeparator |
20308 | 2597 |
1234567 asFixedPoint printStringWithThousandsSeparator |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2598 |
123456 asFixedPoint printStringWithThousandsSeparator |
20308 | 2599 |
12345 asFixedPoint printStringWithThousandsSeparator |
2600 |
1234 asFixedPoint printStringWithThousandsSeparator |
|
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2601 |
123 asFixedPoint printStringWithThousandsSeparator |
20308 | 2602 |
((9999999//10000) asFixedPoint:9) printStringWithThousandsSeparator |
20866 | 2603 |
((99999999//10000) asFixedPoint:9) printStringWithThousandsSeparator |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2604 |
" |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2605 |
! |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2606 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2607 |
printStringWithThousandsSeparator:thousandsSeparator |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2608 |
"print the receiver as business number with a thousands separator to aStream. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2609 |
Notice: |
20308 | 2610 |
americans use comma |
2611 |
germans (europeans ?) use a dot |
|
2612 |
swiss people (business people ?) use a single quote |
|
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2613 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2614 |
Caveat: Should use the separator from the locale here" |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2615 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2616 |
^ String streamContents:[:s | self printOn:s thousandsSeparator:thousandsSeparator]. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2617 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2618 |
" |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2619 |
Transcript showCR:(1000000 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2620 |
Transcript showCR:(12345678 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2621 |
Transcript showCR:(1234567 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2622 |
Transcript showCR:(123456 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2623 |
Transcript showCR:(12345 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2624 |
Transcript showCR:(1234 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2625 |
Transcript showCR:(123 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2626 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2627 |
Transcript showCR:(1000000 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2628 |
Transcript showCR:(12345678 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2629 |
Transcript showCR:(1234567 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2630 |
Transcript showCR:(123456 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2631 |
Transcript showCR:(12345 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2632 |
Transcript showCR:(1234 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2633 |
Transcript showCR:(123 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2634 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2635 |
Transcript showCR:((1000000 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2636 |
Transcript showCR:((12345678 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
20308 | 2637 |
Transcript showCR:((1234567 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2638 |
Transcript showCR:((123456 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
20308 | 2639 |
Transcript showCR:((12345 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
2640 |
Transcript showCR:((1234 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
|
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2641 |
Transcript showCR:((123 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2642 |
" |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2643 |
! |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2644 |
|
21910 | 2645 |
printfPrintString:formatString |
2646 |
"Return a printed representation of the receiver as specified by formatString, |
|
2647 |
which is defined by printf." |
|
2648 |
||
2649 |
^ PrintfScanf printf:formatString argument:self |
|
2650 |
||
2651 |
" |
|
2652 |
2.0 asQDouble printfPrintString:'%10f' |
|
2653 |
2.0 asQDouble printfPrintString:'%10.8f' |
|
2654 |
2.0 printfPrintString:'%10.8f' |
|
25122 | 2655 |
12345 printfPrintString:'0x%06x' |
21910 | 2656 |
" |
2657 |
||
2658 |
"Created: / 22-06-2017 / 13:55:22 / cg" |
|
2659 |
! |
|
2660 |
||
18862 | 2661 |
radixPrintStringRadix:radix |
2662 |
"return a string representation of the receiver in the specified |
|
2663 |
base; prepend XXr to the string" |
|
2664 |
||
2665 |
^ self printStringRadix:radix showRadix:true |
|
2666 |
||
2667 |
" |
|
2668 |
31 radixPrintStringRadix:2 |
|
2669 |
31 radixPrintStringRadix:3 |
|
2670 |
31 radixPrintStringRadix:10 |
|
2671 |
31 radixPrintStringRadix:16 |
|
2672 |
31 radixPrintStringRadix:36 |
|
2673 |
" |
|
2674 |
||
2675 |
"Created: / 19-01-1998 / 17:38:00 / stefan" |
|
2676 |
"Modified: / 20-01-1998 / 14:11:03 / stefan" |
|
2677 |
"Modified: / 23-09-2011 / 14:00:02 / cg" |
|
2678 |
! |
|
2679 |
||
701 | 2680 |
storeOn:aStream |
11460 | 2681 |
"append a string for storing the receiver onto the argument, aStream |
2682 |
- since numbers are literals,they store as they print." |
|
701 | 2683 |
|
2684 |
^ self printOn:aStream |
|
2685 |
! |
|
2686 |
||
2687 |
storeString |
|
20308 | 2688 |
"return a string for storing |
701 | 2689 |
- since numbers are literals, they store as they print." |
2690 |
||
2691 |
^ self printString |
|
2692 |
! ! |
|
2693 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2694 |
!Number methodsFor:'taylor series'! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2695 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2696 |
arcSin_withAccuracy:epsilon |
17190 | 2697 |
"compute the arcSine of the receiver using a taylor series approx." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2698 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2699 |
"/ uses taylor series: |
20308 | 2700 |
"/ 1*x^3 1*3 * x^5 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2701 |
"/ arcSin = x + ----- + ---------- + ... |
20308 | 2702 |
"/ 2* 3 2*4 * 5 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2703 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2704 |
|x2 num numf den denf approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2705 |
|
20308 | 2706 |
((self < -1) or:[self > 1]) ifTrue:[ |
25086 | 2707 |
^ self class |
2708 |
raise:#domainErrorSignal |
|
2709 |
receiver:self |
|
2710 |
selector:#arcSin |
|
2711 |
arguments:#() |
|
2712 |
errorString:'bad receiver in arcSin' |
|
8844 | 2713 |
]. |
2714 |
||
25086 | 2715 |
x2 := self squared. |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2716 |
num := x2 * self. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2717 |
approx := self + (num / 6). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2718 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2719 |
numf := 3. |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2720 |
denf := 4. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2721 |
den := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2722 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2723 |
[ |
25086 | 2724 |
num := (num * x2) * numf. numf := numf + 2. |
2725 |
den := den * denf. denf := denf + 2. |
|
2726 |
||
2727 |
delta := num / (den * numf). |
|
2728 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2729 |
] doUntil:[delta abs <= epsilon]. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2730 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2731 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2732 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2733 |
0.5 arcSin 0.523599 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2734 |
0.5q arcSin 0.523598776 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2735 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2736 |
0.5q arcSin_withAccuracy:1 0.520833333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2737 |
0.5q arcSin_withAccuracy:0.1 0.520833333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2738 |
0.5q arcSin_withAccuracy:0.01 0.523177083 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2739 |
0.5q arcSin_withAccuracy:0.001 0.523525856 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2740 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2741 |
0.5q arcSin_withAccuracy:1e-20 0.523598776 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2742 |
|
20308 | 2743 |
0.5 asLargeFloat arcSin_withAccuracy:1e-30 -- not yet |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2744 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2745 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2746 |
0.1 arcSin 0.100167 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2747 |
0.1q arcSin 0.100167421 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2748 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2749 |
0.1q arcSin_withAccuracy:1 0.100166667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2750 |
0.1q arcSin_withAccuracy:0.1 0.100166667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2751 |
0.1q arcSin_withAccuracy:0.01 0.100166667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2752 |
0.1q arcSin_withAccuracy:0.001 0.100166667 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2753 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2754 |
0.1q arcSin_withAccuracy:1e-20 0.100167421 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2755 |
|
20308 | 2756 |
0.1 asLargeFloat arcSin_withAccuracy:1e-30 -- not yet |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2757 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2758 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2759 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2760 |
arcTan_withAccuracy:epsilon |
17190 | 2761 |
"compute the arcTangent of the receiver using a taylor series approx." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2762 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2763 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2764 |
"/ x^3 x^5 x^7 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2765 |
"/ arcTan(x) = x - --- + --- - --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2766 |
"/ 3 5 7 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2767 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2768 |
|x2 num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2769 |
|
25086 | 2770 |
x2 := self squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2771 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2772 |
num := (x2 * self) negated. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2773 |
den := 3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2774 |
approx := self + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2775 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2776 |
[ |
25086 | 2777 |
den := den + 2. |
2778 |
num := (num * x2) negated. |
|
2779 |
||
2780 |
delta := num / den. |
|
2781 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2782 |
] doUntil:[delta abs <= epsilon]. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2783 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2784 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2785 |
" |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2786 |
1.0 arcTan 0.785398 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2787 |
1q arcTan 0.785398163 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2788 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2789 |
1q arcTan_withAccuracy:1 0.666666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2790 |
1q arcTan_withAccuracy:0.1 0.744011544 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2791 |
1q arcTan_withAccuracy:0.01 0.790299653 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2792 |
1q arcTan_withAccuracy:0.001 0.785897165 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2793 |
|
20308 | 2794 |
1q arcTan_withAccuracy:1e-8 0.785398168 |
2795 |
1q arcTan_withAccuracy:1e-20 -- not yet, converges very slow |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2796 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2797 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2798 |
0.5 arcTan 0.463648 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2799 |
0.5q arcTan 0.463647609 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2800 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2801 |
0.5q arcTan_withAccuracy:1 0.458333333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2802 |
0.5q arcTan_withAccuracy:0.1 0.458333333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2803 |
0.5q arcTan_withAccuracy:0.01 0.464583333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2804 |
0.5q arcTan_withAccuracy:0.001 0.463684276 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2805 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2806 |
0.5q arcTan_withAccuracy:1e-20 0.463647609 |
20308 | 2807 |
0.5 asLargeFloat arcTan_withAccuracy:1e-30 -- not yet |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2808 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2809 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2810 |
|
20308 | 2811 |
cbrt_withAccuracy:epsilon |
17190 | 2812 |
"compute cubic root of the receiver using a newton approx." |
2813 |
||
22097 | 2814 |
"Use Newton's method (not taylor): |
2815 |
||
2816 |
2*x_n + (a / x_n^2) |
|
2817 |
x_n+1 = --------------- |
|
2818 |
3 |
|
2819 |
||
2820 |
cbrt(a) = x_n |
|
17190 | 2821 |
" |
20308 | 2822 |
|
17190 | 2823 |
|approx| |
2824 |
||
2825 |
self = 0 ifTrue:[ |
|
22097 | 2826 |
^ self |
17190 | 2827 |
]. |
2828 |
||
2829 |
approx := 1. |
|
2830 |
[ |
|
22097 | 2831 |
|lastApprox| |
2832 |
||
2833 |
lastApprox := approx. |
|
2834 |
approx := ((approx * 2) + (self / approx / approx)) / 3. |
|
2835 |
(approx - lastApprox) abs > epsilon |
|
17190 | 2836 |
] whileTrue. |
2837 |
^ approx |
|
2838 |
||
2839 |
" |
|
22097 | 2840 |
8q cbrt 2.0 |
2841 |
8q cbrt_withAccuracy:0.01 2.000004911675504018 |
|
2842 |
8q cbrt_withAccuracy:0.0001 2.000000000012062239 |
|
2843 |
8q cbrt_withAccuracy:0.0000001 2.0 |
|
2844 |
8q cbrt_withAccuracy:0.0000000001 2.0 |
|
2845 |
8q cbrt_withAccuracy:0.000000000001 2.0 |
|
2846 |
8q cbrt_withAccuracy:LongFloat epsilon 2.0 |
|
2847 |
8q asQDouble cbrt_withAccuracy:QDouble epsilon 2.0 |
|
2848 |
||
2849 |
27q cbrt_withAccuracy:0.01 3.000000541064176501 |
|
2850 |
27q cbrt_withAccuracy:LongFloat epsilon 3.0 |
|
2851 |
-27q cbrt_withAccuracy:LongFloat epsilon -3.0 |
|
17190 | 2852 |
|
2853 |
MessageTally spyOn:[ |arg| |
|
22097 | 2854 |
arg := 2 asLongFloat. |
2855 |
1000000 timesRepeat:[ |
|
2856 |
arg cbrt_withAccuracy:0.000000000001 |
|
2857 |
] |
|
17190 | 2858 |
] |
2859 |
Time millisecondsToRun:[ |arg| |
|
22097 | 2860 |
arg := 2 asLongFloat. |
2861 |
1000000 timesRepeat:[ |
|
2862 |
arg cbrt_withAccuracy:0.000000000001 |
|
2863 |
] |
|
17190 | 2864 |
] |
2865 |
" |
|
22097 | 2866 |
|
2867 |
"Modified (comment): / 25-07-2017 / 16:09:22 / cg" |
|
17190 | 2868 |
! |
2869 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2870 |
cos_withAccuracy:epsilon |
17190 | 2871 |
"compute the cosine of the receiver using a taylor series approx." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2872 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2873 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2874 |
"/ x^2 x^4 x^6 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2875 |
"/ cos(x) = 1 - --- + --- - --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2876 |
"/ 2!! 4!! 6!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2877 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2878 |
|x2 facN num den approx lastApprox| |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2879 |
|
25086 | 2880 |
x2 := self squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2881 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2882 |
num := x2 negated. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2883 |
den := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2884 |
facN := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2885 |
approx := 1 + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2886 |
lastApprox := 1. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2887 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2888 |
[ (lastApprox - approx) abs > epsilon ] whileTrue:[ |
24988 | 2889 |
facN := facN + 2. |
2890 |
den := den * (facN - 1) * facN. |
|
2891 |
num := (num * x2) negated. |
|
2892 |
lastApprox := approx. |
|
2893 |
approx := approx + (num / den). |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2894 |
]. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2895 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2896 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2897 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2898 |
1.0 cos 0.540302 |
24988 | 2899 |
1.0 asLongFloat cos 0.5403023058681397174 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2900 |
1.0 asLongFloat cos_withAccuracy:1 0.5 |
20308 | 2901 |
1.0 asLongFloat cos_withAccuracy:0.1 0.541666667 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2902 |
1.0 asLongFloat cos_withAccuracy:0.01 0.540277778 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2903 |
1.0 asLongFloat cos_withAccuracy:0.001 0.540302579 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2904 |
|
24988 | 2905 |
1.0 asLongFloat cos_withAccuracy:1e-40 0.5403023058681397175 |
2906 |
1.0 asQDouble cos 0.5403023058681396874081335957994 |
|
2907 |
1.0 asQDouble cos_withAccuracy:1e-40 |
|
2908 |
Wolfram: |
|
2909 |
0.5403023058681396874081335957994 |
|
2910 |
0.540302305868139717400936607442976603732310420617922227670... |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2911 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2912 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2913 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2914 |
cosh_withAccuracy:epsilon |
17190 | 2915 |
"compute the hyperbolic cosine of the receiver using a taylor series approx." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2916 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2917 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2918 |
"/ x^2 x^4 x^6 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2919 |
"/ cosh = x + --- + --- + --- ... |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2920 |
"/ 2!! 4!! 6!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2921 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2922 |
|x2 facN num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2923 |
|
25086 | 2924 |
x2 := self squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2925 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2926 |
num := x2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2927 |
den := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2928 |
facN := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2929 |
approx := self + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2930 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2931 |
[ |
22143 | 2932 |
facN := facN + 2. |
2933 |
den := den * (facN - 1) * facN. |
|
2934 |
num := num * x2. |
|
2935 |
||
2936 |
delta := num / den. |
|
2937 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2938 |
] doUntil:[delta <= epsilon]. |
22143 | 2939 |
^ approx + self. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2940 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2941 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2942 |
1.0 cosh 1.54308 |
20308 | 2943 |
1.0q cosh_withAccuracy:1 1.5 |
2944 |
1.0q cosh_withAccuracy:0.1 1.54308 |
|
2945 |
1.0q cosh_withAccuracy:0.01 1.54308 |
|
2946 |
1.0q cosh_withAccuracy:0.001 1.54308 |
|
2947 |
||
2948 |
1.0q cosh_withAccuracy:1e-40 -> 1.543080 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2949 |
" |
22143 | 2950 |
|
2951 |
"Modified: / 01-08-2017 / 14:54:40 / stefan" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2952 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2953 |
|
8634 | 2954 |
epsilon |
20308 | 2955 |
"return the maximum relative spacing of instances of mySelf |
8634 | 2956 |
(i.e. the value-delta of the least significant bit)" |
2957 |
||
2958 |
^ self class epsilon |
|
2959 |
! |
|
2960 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2961 |
exp_withAccuracy:epsilon |
21888 | 2962 |
"compute e^x of the receiver using a taylor series approximation. |
2963 |
This method is only invoked for limitedPrecisionReal classes, which do not compute |
|
2964 |
exp themself (i.e. QDouble)" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2965 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2966 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2967 |
"/ x x^2 x^3 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2968 |
"/ e^x = 1 + --- + --- + --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2969 |
"/ 1!! 2!! 3!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2970 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2971 |
|x2 facN num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2972 |
|
25086 | 2973 |
x2 := self squared. "/ self squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2974 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2975 |
num := x2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2976 |
den := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2977 |
facN := 2. |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2978 |
approx := self + 1 + (num / den). |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2979 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2980 |
[ |
21888 | 2981 |
facN := facN + 1. |
2982 |
den := den * facN. |
|
2983 |
num := num * self. |
|
2984 |
||
2985 |
delta := num / den. |
|
22302 | 2986 |
"/ delta mantissa == 0 ifTrue:[self halt. num / den]. |
2987 |
"/ Transcript showCR:delta. |
|
2988 |
delta isNaN ifTrue:[self halt:'nan when dividing for delta'. num / den]. |
|
2989 |
delta = 0 ifTrue:[self halt:'zero delta']. |
|
21888 | 2990 |
approx := approx + delta. |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2991 |
] doUntil:[delta abs <= epsilon]. |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2992 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2993 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2994 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2995 |
" |
22302 | 2996 |
wolfram: |
2997 |
7.389056098930650227230427460575007813180315570551847324087 |
|
2998 |
||
2999 |
(2 asLargeFloat exp_withAccuracy:1e-100) printfPrintString:'%50.48f' |
|
3000 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3001 |
1.0 exp 2.71828 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3002 |
1q exp 2.71828183 |
20308 | 3003 |
2q exp 7.3890561 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3004 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3005 |
1q exp_withAccuracy:1 2.66666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3006 |
1q exp_withAccuracy:0.1 2.70833333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3007 |
1q exp_withAccuracy:0.01 2.71666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3008 |
1q exp_withAccuracy:0.001 2.71825397 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3009 |
|
21912 | 3010 |
-1 exp 0.367879441171442 |
3011 |
-1q exp_withAccuracy:(1e-60) 0.3678794411714423216 |
|
3012 |
||
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3013 |
2q exp_withAccuracy:LongFloat epsilon 7.3890561 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3014 |
|
21982 | 3015 |
42 asQDouble exp_withAccuracy:QDouble epsilon |
3016 |
42 asQDouble exp_withAccuracy:LongFloat epsilon |
|
3017 |
42 asQDouble exp_withAccuracy:Float epsilon |
|
3018 |
||
3019 |
2 asQDouble exp_withAccuracy:QDouble epsilon 7.38905609893065022723 |
|
3020 |
2 asQDouble exp_withAccuracy:LongFloat epsilon 7.38905609893065022723 |
|
3021 |
2 asQDouble exp_withAccuracy:Float epsilon 7.38905609893065022489 |
|
3022 |
||
3023 |
1 asQDouble exp_withAccuracy:QDouble epsilon 2.71828182845904523536 |
|
3024 |
1 asQDouble exp_withAccuracy:LongFloat epsilon 2.71828182845904523536 |
|
3025 |
1 asQDouble exp_withAccuracy:Float epsilon 2.71828182845904522671 |
|
3026 |
||
20308 | 3027 |
1.0 asLongFloat exp_withAccuracy:1e-40 2.71828183 |
3028 |
||
3029 |
5 exp_withAccuracy:1e-40 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3030 |
(1 exp_withAccuracy:1e-100) asFixedPoint:100 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3031 |
" |
21888 | 3032 |
|
22302 | 3033 |
"Modified: / 10-10-2017 / 16:04:08 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3034 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3035 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3036 |
ln_withAccuracy:epsilon |
17190 | 3037 |
"compute ln of the receiver using a taylor series approx." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3038 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3039 |
"uses taylor series: |
21982 | 3040 |
u^2 u^3 |
3041 |
ln = u - --- + --- ... |
|
3042 |
2 3 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3043 |
where: |
21982 | 3044 |
u = x - 1 and: x < 1 |
3045 |
||
3046 |
Now we use modified taylor, which converges a little faster: |
|
3047 |
||
3048 |
1+y 1 1 1 |
|
3049 |
ln(x) = ln --- = 2y ( - + - y^2 + - y^4 + ....) |
|
3050 |
1-y 1 3 5 |
|
3051 |
||
3052 |
where y = (x-1) / (x+1) and x > 0 |
|
3053 |
||
3054 |
Warning: this converges very slowly. Find a better algorithm |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3055 |
" |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3056 |
|
21982 | 3057 |
|denominator approx y y2 exp delta count| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3058 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3059 |
self <= 0 ifTrue:[ |
21982 | 3060 |
^ self class |
3061 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
|
3062 |
receiver:self |
|
3063 |
selector:#ln |
|
3064 |
arguments:#() |
|
3065 |
errorString:'bad receiver in ln (not strictly positive)' |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3066 |
]. |
20308 | 3067 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3068 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3069 |
y := (self - 1)/(self + 1). |
21982 | 3070 |
exp := y2 := y squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3071 |
|
21988 | 3072 |
approx := 1.0. |
3073 |
denominator := 3.0. |
|
21982 | 3074 |
count := 1. |
3075 |
||
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3076 |
[ |
21982 | 3077 |
delta := exp / denominator. |
3078 |
approx := approx + delta. |
|
3079 |
exp := exp * y2. |
|
3080 |
denominator := denominator + 2. |
|
3081 |
||
3082 |
count := count + 1. |
|
3083 |
(count \\ 100) == 0 ifTrue:[ |
|
3084 |
Logger warning:'slow ln-taylor converging...'. |
|
3085 |
]. |
|
3086 |
] doUntil:[delta <= epsilon or:[count > 10000]]. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3087 |
|
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3088 |
^ y * 2 * approx. |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3089 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3090 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3091 |
" |
21982 | 3092 |
2.0 ln 0.693147180559945 |
3093 |
2.0q ln 0.6931471805599453094 |
|
3094 |
||
3095 |
2.0q ln_withAccuracy:1 0.691358025 |
|
3096 |
2.0q ln_withAccuracy:0.1 0.691358025 |
|
3097 |
2.0q ln_withAccuracy:0.01 0.693004115 |
|
3098 |
2.0q ln_withAccuracy:0.0000001 0.69314718 |
|
3099 |
2.0q ln_withAccuracy:1e-10 0.6931471805589163927 |
|
3100 |
2.0q ln_withAccuracy:1e-20 0.6931471805599453094 |
|
3101 |
2.0q ln_withAccuracy:1e-40 0.6931471805599453094 |
|
3102 |
2.0q ln_withAccuracy:2.0q class epsilon 0.6931471805599453094 |
|
3103 |
||
3104 |
(2 ln_withAccuracy:1e-40) -> a fraction |
|
20308 | 3105 |
0 ln_withAccuracy:1e-40 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3106 |
|
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3107 |
(2 ln_withAccuracy:1e-100) asFixedPoint:100 |
21982 | 3108 |
(2 asFixedPoint:200) ln_withAccuracy:(1/(10 raisedTo:200)) |
3109 |
0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335 |
|
3110 |
||
3111 |
(2 asFixedPoint:400) ln_withAccuracy:(1/(10 raisedTo:400)) |
|
3112 |
0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335 01153644979552391204751726815749320651555247341395258829504530070953263666426541042391578149520437404303855008019441706416715186447128399681717845469570262716310645461502572074024816377733896385506953 |
|
3113 |
||
3114 |
(2.0 asQDouble ln_withAccuracy:QDouble epsilon) printfPrintString:'%60.58f' |
|
3115 |
0.69314718055994 52709398341558750792990469129794959648865081' |
|
3116 |
||
3117 |
Wolfram says: |
|
3118 |
0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335 01153644979552391204751726... |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3119 |
" |
21982 | 3120 |
|
21988 | 3121 |
"Modified: / 05-07-2017 / 17:16:24 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3122 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3123 |
|
21915 | 3124 |
nthRoot:n withAccuracy:epsilon |
3125 |
"compute nth root of the receiver using a newton approx." |
|
3126 |
||
3127 |
" |
|
22104 | 3128 |
Use Newton's method: |
21915 | 3129 |
|
3130 |
(n-1)*x_n^n + (a / x_n^(n-1)) |
|
3131 |
x_n+1 = ----------------------------- |
|
3132 |
n |
|
3133 |
||
3134 |
rt(n, a) = x_n |
|
3135 |
" |
|
3136 |
||
22287 | 3137 |
|approx delta absDelta prevDelta| |
21915 | 3138 |
|
3139 |
self = 0 ifTrue:[ |
|
3140 |
^ self |
|
3141 |
]. |
|
3142 |
||
22104 | 3143 |
"/ the demanded epsilon may NEVER be smaller than the real representation's epsilon |
3144 |
self assert:(epsilon >= (self class epsilon)). |
|
3145 |
||
3146 |
approx := (self / 2). |
|
22287 | 3147 |
delta := ((self / (approx raisedToInteger:(n-1))) - approx) / n. |
3148 |
absDelta := delta abs. |
|
3149 |
approx := approx + delta. |
|
3150 |
||
21915 | 3151 |
[ |
22287 | 3152 |
prevDelta := absDelta. |
3153 |
||
22104 | 3154 |
delta := ((self / (approx raisedToInteger:(n-1))) - approx) / n. |
22287 | 3155 |
absDelta := delta abs. |
22104 | 3156 |
approx := approx + delta. |
22098 | 3157 |
|
22287 | 3158 |
(absDelta < prevDelta) ifFalse:[ |
3159 |
DomainError raiseRequestErrorString:'no convergence in Newton approx.' |
|
3160 |
]. |
|
3161 |
||
3162 |
(absDelta > epsilon) |
|
21915 | 3163 |
] whileTrue. |
3164 |
^ approx |
|
3165 |
||
22104 | 3166 |
" |
3167 |
8q nthRoot:3 withAccuracy:0.01 2.00000002488636242 |
|
3168 |
8q nthRoot:3 withAccuracy:0.0001 2.00000000000000031 |
|
3169 |
8q nthRoot:3 withAccuracy:0.0000001 2.00000000000000031 |
|
21915 | 3170 |
8q nthRoot:3 withAccuracy:0.0000000001 2.0 |
3171 |
8q nthRoot:3 withAccuracy:0.000000000001 2.0 |
|
3172 |
8q nthRoot:3 withAccuracy:LongFloat epsilon 2.0 |
|
3173 |
||
22104 | 3174 |
27q nthRoot:3 withAccuracy:0.01 3.000000081210202031 |
3175 |
27q nthRoot:3 withAccuracy:LongFloat epsilon 3.0 |
|
3176 |
-27q nthRoot:3 withAccuracy:LongFloat epsilon -3.0 |
|
3177 |
||
3178 |
10000q nthRoot:5 withAccuracy:1e-18 -> 6.309573444801932495 |
|
3179 |
||
3180 |
(10000 asQDouble nthRoot:5) printfPrintString:'%70.68f' |
|
3181 |
6.30957344480193249434360136622343864672945257188228724527729528834741' |
|
3182 |
||
3183 |
actual result (Mathematica): |
|
3184 |
6.309573444801932494343601366223438646729452571882287245277... |
|
21915 | 3185 |
" |
3186 |
||
3187 |
"Created: / 22-06-2017 / 15:51:55 / cg" |
|
22287 | 3188 |
"Modified: / 22-09-2017 / 10:14:59 / cg" |
21915 | 3189 |
! |
3190 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3191 |
sin_withAccuracy:epsilon |
17190 | 3192 |
"compute the sine of the receiver using a taylor series approx." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3193 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3194 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3195 |
"/ x^3 x^5 x^7 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3196 |
"/ sin(x) = x - --- + --- - --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3197 |
"/ 3!! 5!! 7!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3198 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3199 |
|x2 facN num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3200 |
|
25086 | 3201 |
x2 := self squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3202 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3203 |
num := (x2 * self) negated. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3204 |
den := 2*3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3205 |
facN := 3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3206 |
approx := self + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3207 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3208 |
[ |
25086 | 3209 |
facN := facN + 2. |
3210 |
den := den * (facN - 1) * facN. |
|
3211 |
num := (num * x2) negated. |
|
3212 |
||
3213 |
delta := num / den. |
|
3214 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3215 |
] doUntil:[delta abs <= epsilon]. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3216 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3217 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3218 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3219 |
1.0 sin 0.841471 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3220 |
1.0q sin 0.841470985 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3221 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3222 |
1.0q sin_withAccuracy:1 0.833333333 |
20308 | 3223 |
1.0q sin_withAccuracy:0.1 0.841666667 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3224 |
1.0q sin_withAccuracy:0.01 0.841666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3225 |
1.0q sin_withAccuracy:0.001 0.841468254 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3226 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3227 |
1.0q sin_withAccuracy:1e-40 0.841470985 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3228 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3229 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3230 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3231 |
sinh_withAccuracy:epsilon |
17190 | 3232 |
"compute the hyperbolic sine of the receiver using a taylor series approx." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3233 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3234 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3235 |
"/ x^3 x^5 x^7 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3236 |
"/ sinh(x) = x + --- + --- + --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3237 |
"/ 3!! 5!! 7!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3238 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3239 |
|x2 facN num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3240 |
|
25086 | 3241 |
x2 := self squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3242 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3243 |
num := x2 * self. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3244 |
den := 2*3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3245 |
facN := 3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3246 |
approx := self + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3247 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3248 |
[ |
25086 | 3249 |
facN := facN + 2. |
3250 |
den := den * (facN - 1) * facN. |
|
3251 |
num := num * x2. |
|
3252 |
||
3253 |
delta := num / den. |
|
3254 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3255 |
] doUntil:[delta abs <= epsilon]. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3256 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3257 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3258 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3259 |
1.0 sinh 1.1752 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3260 |
1q sinh 1.17520119 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3261 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3262 |
1q sinh_withAccuracy:1 1.16666667 |
20308 | 3263 |
1q sinh_withAccuracy:0.1 1.175 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3264 |
1q sinh_withAccuracy:0.01 1.175 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3265 |
1q sinh_withAccuracy:0.001 1.17519841 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3266 |
|
20308 | 3267 |
1q sinh_withAccuracy:1e-40 1.17520119 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3268 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3269 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3270 |
|
20308 | 3271 |
sqrt_withAccuracy:epsilon |
21910 | 3272 |
"compute square root of the receiver using newton-raphson/heron algorithm" |
22097 | 3273 |
"Use the Heron algorithm (not Taylor)" |
21910 | 3274 |
|
22096 | 3275 |
|guess| |
3276 |
||
3277 |
false "self isInteger" ifTrue:[ |
|
3278 |
guess := 1 bitShift:(self highBit // 2) |
|
3279 |
] ifFalse:[ |
|
22106 | 3280 |
guess := self / 2.0. |
22096 | 3281 |
]. |
3282 |
^ self sqrt_withAccuracy:epsilon fromInitialGuess:guess |
|
21910 | 3283 |
|
3284 |
" |
|
3285 |
2 sqrt 1.4142135623731 - computed by CPU/FPU |
|
22096 | 3286 |
200000000 sqrt |
21910 | 3287 |
|
3288 |
2q sqrt 1.414213562373095049 - computed by CPU/FPU |
|
3289 |
||
3290 |
2q sqrt_withAccuracy:0.01 1.414215686274509804 - computed by Smalltalk |
|
3291 |
2q sqrt_withAccuracy:0.0001 1.414213562374689911 |
|
3292 |
2q sqrt_withAccuracy:0.0000001 1.414213562373095049 |
|
3293 |
2q sqrt_withAccuracy:0.0000000001 1.414213562373095049 |
|
3294 |
2q sqrt_withAccuracy:0.000000000001 1.414213562373095049 |
|
3295 |
||
3296 |
2q sqrt_withAccuracy:LongFloat epsilon 1.414213562373095049 |
|
3297 |
||
3298 |
(4 sqrt_withAccuracy:Integer epsilon) asFloat |
|
3299 |
||
3300 |
MessageTally spyOn:[ |arg| |
|
3301 |
arg := 2 asLongFloat. |
|
3302 |
1000000 timesRepeat:[ |
|
3303 |
arg sqrt_withAccuracy:0.000000000001 |
|
3304 |
] |
|
3305 |
] |
|
3306 |
Time millisecondsToRun:[ |arg| |
|
3307 |
arg := 2 asLongFloat. |
|
3308 |
1000000 timesRepeat:[ |
|
3309 |
arg sqrt_withAccuracy:0.000000000001 |
|
3310 |
] |
|
3311 |
] |
|
3312 |
" |
|
3313 |
||
22106 | 3314 |
"Modified: / 25-07-2017 / 17:44:46 / cg" |
21910 | 3315 |
! |
3316 |
||
3317 |
sqrt_withAccuracy:epsilon fromInitialGuess:guess |
|
3318 |
"compute square root of the receiver using newton-raphson/heron algorithm" |
|
22097 | 3319 |
|
3320 |
"Use the Heron algorithm (not Taylor): |
|
21910 | 3321 |
|
3322 |
x_n + (a / x_n) |
|
3323 |
x_n+1 = --------------- |
|
3324 |
2 |
|
3325 |
||
3326 |
sqrt(a) = x_n |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3327 |
" |
20308 | 3328 |
|
25086 | 3329 |
|approx lastApprox| |
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3330 |
|
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3331 |
self <= 0 ifTrue:[ |
21910 | 3332 |
self = 0 ifTrue:[ |
3333 |
^ self |
|
3334 |
]. |
|
3335 |
^ self class |
|
3336 |
raise:#imaginaryResultSignal |
|
3337 |
receiver:self |
|
3338 |
selector:#sqrt |
|
3339 |
arguments:#() |
|
3340 |
errorString:'bad (negative) receiver in sqrt' |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3341 |
]. |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3342 |
|
21910 | 3343 |
approx := guess. |
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3344 |
[ |
21910 | 3345 |
lastApprox := approx. |
3346 |
approx := ((self / approx) + approx) / 2. |
|
3347 |
(approx - lastApprox) abs > epsilon |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3348 |
] whileTrue. |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3349 |
^ approx |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3350 |
|
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3351 |
" |
17190 | 3352 |
2 sqrt 1.4142135623731 |
3353 |
2q sqrt 1.414213562373095049 |
|
3354 |
2q sqrt_withAccuracy:0.01 1.414215686274509804 |
|
3355 |
2q sqrt_withAccuracy:0.0001 1.414213562374689911 |
|
3356 |
2q sqrt_withAccuracy:0.0000001 1.414213562373095049 |
|
3357 |
2q sqrt_withAccuracy:0.0000000001 1.414213562373095049 |
|
3358 |
2q sqrt_withAccuracy:0.000000000001 1.414213562373095049 |
|
3359 |
2q sqrt_withAccuracy:LongFloat epsilon 1.414213562373095049 |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3360 |
|
21916 | 3361 |
(2 asQDouble sqrt_withAccuracy:LongFloat epsilon) printfPrintString:'%70.68f' |
3362 |
1.41421356237309504880168872420969807856967187537723400156101313309966 |
|
3363 |
||
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3364 |
(4 sqrt_withAccuracy:Integer epsilon) asFloat |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3365 |
|
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3366 |
MessageTally spyOn:[ |arg| |
21910 | 3367 |
arg := 2 asLongFloat. |
3368 |
1000000 timesRepeat:[ |
|
3369 |
arg sqrt_withAccuracy:0.000000000001 |
|
3370 |
] |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3371 |
] |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3372 |
Time millisecondsToRun:[ |arg| |
21910 | 3373 |
arg := 2 asLongFloat. |
3374 |
1000000 timesRepeat:[ |
|
3375 |
arg sqrt_withAccuracy:0.000000000001 |
|
3376 |
] |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3377 |
] |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3378 |
" |
21910 | 3379 |
|
3380 |
"Created: / 22-06-2017 / 13:59:48 / cg" |
|
22097 | 3381 |
"Modified (comment): / 25-07-2017 / 16:07:37 / cg" |
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3382 |
! |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3383 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3384 |
tan_withAccuracy:epsilon |
17190 | 3385 |
"compute the tangens of the receiver using a taylor series approx." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3386 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3387 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3388 |
"/ x^3 x^5 x^7 x^9 2^2n * ( 2^2n - 1) * B2n * x^(2n-1) |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3389 |
"/ tan = x + 1*--- + 2*--- + 17*--- + 62*----... ----------------------------------... |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3390 |
"/ 3 15 315 2835 (2n)!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3391 |
"/ where Bi is the ith bernoulli number. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3392 |
|
22543 | 3393 |
|factors idx x2 num t approx lastApprox delta nFactors| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3394 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3395 |
"/ (1 to:20) collect:[:n| |num den| |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3396 |
"/ num := (2 raisedTo:(2*n)) * ((2 raisedTo:(2*n))-1) * ((n*2) bernoulli). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3397 |
"/ den := (2*n) factorial. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3398 |
"/ num / den |
20308 | 3399 |
"/ ] |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3400 |
factors := #( |
22543 | 3401 |
(1 3) |
3402 |
(2 15) |
|
3403 |
(17 315) |
|
3404 |
(62 2835) |
|
3405 |
(1382 155925) |
|
3406 |
(21844 6081075) |
|
3407 |
(929569 638512875) |
|
3408 |
(6404582 10854718875) |
|
3409 |
(443861162 1856156927625) |
|
3410 |
(18888466084 194896477400625) |
|
3411 |
(113927491862 2900518163668125) |
|
3412 |
(58870668456604 3698160658676859375) |
|
3413 |
(8374643517010684 1298054391195577640625) |
|
3414 |
(689005380505609448 263505041412702261046875) |
|
3415 |
(129848163681107301953 122529844256906551386796875) |
|
3416 |
(1736640792209901647222 4043484860477916195764296875) |
|
3417 |
(418781231495293038913922 2405873491984360136479756640625) |
|
3418 |
(56518638202982204522669764 801155872830791925447758961328125) |
|
3419 |
(32207686319158956594455462 1126482925555250126673224649609375)). |
|
3420 |
||
3421 |
nFactors := factors size. |
|
25086 | 3422 |
x2 := self squared. |
8203 | 3423 |
num := x2 * self. "/ = x^3 |
3424 |
approx := self + (num / 3). "/ do the first iteration |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3425 |
lastApprox := self. |
8203 | 3426 |
idx := 2. |
3427 |
[ |
|
22543 | 3428 |
idx > nFactors ifTrue:[ |
3429 |
ArithmeticError raiseErrorString:'too many iterations'. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3430 |
"/ Not enough bernoulli numbers for now... |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3431 |
"/ |tempNum tempDen| |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3432 |
"/ tempNum := 2 raisedTo:(2*idx). |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3433 |
"/ tempNum := tempNum * (tempNum-1) * ((2*idx) bernoulli). |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3434 |
"/ tempDen := (2*idx) factorial. |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3435 |
"/ t := Array with:tempNum with:tempDen. |
22543 | 3436 |
]. |
3437 |
t := factors at:idx. |
|
3438 |
idx := idx + 1. |
|
3439 |
num := num * x2. |
|
3440 |
||
3441 |
delta := num * t first / t second. |
|
3442 |
approx := approx + delta. |
|
3443 |
delta abs > epsilon |
|
3444 |
] whileTrue. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3445 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3446 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3447 |
" |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3448 |
0.5 tan 0.546302 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3449 |
0.5q tan 0.54630249 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3450 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3451 |
0.5q tan_withAccuracy:1 0.541666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3452 |
0.5q tan_withAccuracy:0.1 0.541666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3453 |
0.5q tan_withAccuracy:0.01 0.545833333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3454 |
0.5q tan_withAccuracy:0.001 0.54625496 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3455 |
0.5q tan_withAccuracy:1e-15 0.54630249 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3456 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3457 |
0.5q tan_withAccuracy:1e-40 -- too many iterations |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3458 |
" |
22543 | 3459 |
|
3460 |
"Modified: / 15-02-2018 / 18:53:37 / stefan" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3461 |
! ! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3462 |
|
701 | 3463 |
!Number methodsFor:'testing'! |
3464 |
||
17224 | 3465 |
even |
3466 |
"return true if the receiver is divisible by 2." |
|
3467 |
||
3468 |
self fractionPart = 0 ifTrue:[ |
|
21982 | 3469 |
^ (self / 2) fractionPart = 0 |
17224 | 3470 |
]. |
3471 |
||
3472 |
"this will raise an error" |
|
3473 |
^ super even |
|
3474 |
||
3475 |
" |
|
21982 | 3476 |
2 even |
3477 |
2.0 even |
|
3478 |
3.0 even |
|
3479 |
2.4 even |
|
3480 |
(5/3) even |
|
3481 |
||
3482 |
2 asFraction even |
|
3483 |
(2 asFixedPoint:5) even |
|
3484 |
2 asFloatE even |
|
3485 |
2 asFloatD even |
|
3486 |
2 asFloatQ even |
|
3487 |
2 asFloatQD even |
|
3488 |
||
3489 |
(2.1 asFloatQD - (QDouble readFrom:'2.1')) fractionPart |
|
17224 | 3490 |
" |
21982 | 3491 |
|
3492 |
"Modified (comment): / 03-07-2017 / 14:14:46 / cg" |
|
17224 | 3493 |
! |
3494 |
||
5360 | 3495 |
isDivisibleBy:aNumber |
3496 |
"return true, if the receiver can be divided by the argument, aNumber without a remainder. |
|
3497 |
Notice, that the result is only worth trusting, if the receiver is an integer." |
|
3498 |
||
3499 |
aNumber = 0 ifTrue: [^ false]. |
|
3500 |
aNumber isInteger ifFalse: [^ false]. |
|
3501 |
^ (self \\ aNumber) = 0 |
|
3502 |
||
3503 |
" |
|
20308 | 3504 |
3 isDivisibleBy:2 |
5360 | 3505 |
4 isDivisibleBy:2 |
20308 | 3506 |
4.0 isDivisibleBy:2 |
3507 |
4.5 isDivisibleBy:4.5 |
|
3508 |
4.5 isDivisibleBy:1.0 |
|
5360 | 3509 |
" |
3510 |
! |
|
3511 |
||
6143 | 3512 |
isNaN |
24702 | 3513 |
"return true, if the receiver is an invalid number (NaN - not a number)." |
6143 | 3514 |
|
3515 |
^ false |
|
6155 | 3516 |
|
3517 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
6143 | 3518 |
! |
3519 |
||
701 | 3520 |
isNumber |
3521 |
"return true, if the receiver is a kind of number" |
|
3522 |
||
3523 |
^ true |
|
3524 |
! |
|
3525 |
||
19136 | 3526 |
isPerfectSquare |
3527 |
"return true if I am a perfect square. |
|
3528 |
That is a number for which the square root is an integer." |
|
3529 |
||
3530 |
self truncated = self ifFalse:[^ false]. |
|
3531 |
^ self asInteger isPerfectSquare |
|
3532 |
||
3533 |
" |
|
24938 | 3534 |
0 isPerfectSquare |
19136 | 3535 |
0.0 isPerfectSquare |
24938 | 3536 |
1 isPerfectSquare |
3537 |
1.0 isPerfectSquare |
|
3538 |
2 isPerfectSquare |
|
3539 |
2.0 isPerfectSquare |
|
19136 | 3540 |
3 isPerfectSquare |
3541 |
3.0 isPerfectSquare |
|
3542 |
4 isPerfectSquare |
|
3543 |
4.0 isPerfectSquare |
|
24938 | 3544 |
9 isPerfectSquare |
3545 |
9.0 isPerfectSquare |
|
24867 | 3546 |
123456789012345678901234567890 squared isPerfectSquare |
24938 | 3547 |
1000 factorial squared isPerfectSquare |
19136 | 3548 |
" |
3549 |
! |
|
3550 |
||
6087 | 3551 |
isReal |
3552 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
3553 |
true is returned here - the method is redefined from Object." |
|
3554 |
||
3555 |
^ true |
|
3556 |
! |
|
3557 |
||
701 | 3558 |
isZero |
1557 | 3559 |
"return true, if the receiver is zero" |
3560 |
||
701 | 3561 |
^ self = 0 |
1557 | 3562 |
|
3563 |
"Modified: 18.7.1996 / 12:40:49 / cg" |
|
701 | 3564 |
! ! |
3565 |
||
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3566 |
!Number methodsFor:'tracing'! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3567 |
|
4682 | 3568 |
traceInto:aRequestor level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3569 |
"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
|
3570 |
|
4682 | 3571 |
^ aRequestor traceNumber:self level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3572 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3573 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3574 |
! ! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3575 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3576 |
!Number methodsFor:'trigonometric'! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3577 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3578 |
arcCos |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3579 |
"return the arccosine of the receiver (in radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3580 |
|
19769 | 3581 |
"/ if I am not a Float (or a less general lpReal), |
3582 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3583 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3584 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3585 |
|f| |
3586 |
||
3587 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3588 |
^ f arcCos. |
|
3589 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3590 |
]. |
19769 | 3591 |
"/ slow fallback |
8844 | 3592 |
^ (self class pi / 2) - self arcSin |
21988 | 3593 |
|
3594 |
"Modified: / 05-07-2017 / 17:24:32 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3595 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3596 |
|
19768 | 3597 |
arcCosech |
3598 |
"return the inverse hyperbolic cosecant of the receiver." |
|
19769 | 3599 |
"caveat: misnomer; should be called aCosech or arCosech" |
3600 |
||
3601 |
"/ if I am not a Float (or a less general lpReal), |
|
3602 |
"/ retry after converting to float |
|
19768 | 3603 |
(self isLimitedPrecisionReal not |
3604 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 3605 |
|f| |
3606 |
||
3607 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3608 |
^ f arcCosech. |
|
3609 |
]. |
|
19768 | 3610 |
]. |
19769 | 3611 |
"/ slow fallback |
25086 | 3612 |
^ ((1 + ((self squared)+1) sqrt) / self) ln |
21988 | 3613 |
|
3614 |
"Modified: / 05-07-2017 / 17:24:56 / cg" |
|
19768 | 3615 |
! |
3616 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3617 |
arcCosh |
19768 | 3618 |
"return the inverse hyperbolic cosine of the receiver." |
19769 | 3619 |
"caveat: misnomer; should be called aCosh or arCosh" |
3620 |
||
3621 |
"/ if I am not a Float (or a less general lpReal), |
|
3622 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3623 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3624 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3625 |
|f| |
3626 |
||
3627 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3628 |
^ f arcCosh. |
|
3629 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3630 |
]. |
19769 | 3631 |
"/ slow fallback |
25086 | 3632 |
^ (self + (self squared-1) sqrt) ln. |
21988 | 3633 |
|
3634 |
"Modified: / 05-07-2017 / 17:25:03 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3635 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3636 |
|
19768 | 3637 |
arcCoth |
3638 |
"return the inverse hyperbolic cotangent of the receiver." |
|
19769 | 3639 |
"caveat: misnomer; should be called aCoth or arCoth" |
3640 |
||
3641 |
"/ if I am not a Float (or a less general lpReal), |
|
3642 |
"/ retry after converting to float |
|
19768 | 3643 |
(self isLimitedPrecisionReal not |
3644 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 3645 |
|f| |
3646 |
||
3647 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3648 |
^ f arcCoth. |
|
3649 |
]. |
|
19768 | 3650 |
]. |
19769 | 3651 |
"/ slow fallback |
19768 | 3652 |
^ ((self+1) / (self-1)) ln / 2 |
21988 | 3653 |
|
3654 |
"Modified: / 05-07-2017 / 17:25:14 / cg" |
|
19768 | 3655 |
! |
3656 |
||
3657 |
arcSech |
|
3658 |
"return the inverse hyperbolic secant of the receiver." |
|
19769 | 3659 |
"caveat: misnomer; should be called aSech or arSech" |
3660 |
||
3661 |
"/ if I am not a Float (or a less general lpReal), |
|
3662 |
"/ retry after converting to float |
|
19768 | 3663 |
(self isLimitedPrecisionReal not |
3664 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 3665 |
|f| |
3666 |
||
3667 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3668 |
^ f arcSech. |
|
3669 |
]. |
|
19768 | 3670 |
]. |
19769 | 3671 |
"/ slow fallback |
25086 | 3672 |
^ ((1 + (1-(self squared)) sqrt) / self) ln |
21988 | 3673 |
|
3674 |
"Modified: / 05-07-2017 / 17:25:22 / cg" |
|
19768 | 3675 |
! |
3676 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3677 |
arcSin |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3678 |
"return the arcsine of the receiver (in radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3679 |
|
19769 | 3680 |
"/ if I am not a Float (or a less general lpReal), |
3681 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3682 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3683 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3684 |
|f| |
3685 |
||
3686 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3687 |
^ f arcSin. |
|
3688 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3689 |
]. |
19769 | 3690 |
"/ very slow fallback |
8634 | 3691 |
^ self arcSin_withAccuracy:self epsilon |
21988 | 3692 |
|
3693 |
"Modified: / 05-07-2017 / 17:25:30 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3694 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3695 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3696 |
arcSinh |
19768 | 3697 |
"return the inverse hyperbolic sine of the receiver." |
19769 | 3698 |
"caveat: misnomer; should be called aSinh or arSinh" |
3699 |
||
3700 |
"/ if I am not a Float (or a less general lpReal), |
|
3701 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3702 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3703 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3704 |
|f| |
3705 |
||
3706 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3707 |
^ f arcSinh. |
|
3708 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3709 |
]. |
19769 | 3710 |
"/ slow fallback |
25086 | 3711 |
^ ( self + (self squared+1) sqrt ) ln |
22730 | 3712 |
"/ ^ self arcSinh_withAccuracy:self epsilon |
21988 | 3713 |
|
3714 |
"Modified: / 05-07-2017 / 17:25:38 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3715 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3716 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3717 |
arcTan |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3718 |
"return the arctangent of the receiver (as radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3719 |
|
19769 | 3720 |
"/ if I am not a Float (or a less general lpReal), |
3721 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3722 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3723 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3724 |
|f| |
3725 |
||
3726 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3727 |
^ f arcTan. |
|
3728 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3729 |
]. |
19769 | 3730 |
"/ very slow fallback |
8634 | 3731 |
^ self arcTan_withAccuracy:self epsilon |
21988 | 3732 |
|
3733 |
"Modified: / 05-07-2017 / 17:25:46 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3734 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3735 |
|
11488 | 3736 |
arcTan2:x |
3737 |
"return atan2(self,x) (as radians)" |
|
3738 |
||
19769 | 3739 |
"/ if I am not a Float (or a less general lpReal), |
3740 |
"/ retry after converting to float |
|
11488 | 3741 |
(self isLimitedPrecisionReal not |
3742 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 3743 |
|f| |
3744 |
||
3745 |
(f := self asFloat) isFinite ifTrue:[ |
|
3746 |
^ f arcTan2:x. |
|
3747 |
]. |
|
11488 | 3748 |
]. |
19769 | 3749 |
"/ very slow fallback |
11488 | 3750 |
^ self arcTan2_withAccuracy:self epsilon x:x |
21988 | 3751 |
|
3752 |
"Modified: / 05-07-2017 / 17:26:16 / cg" |
|
11488 | 3753 |
! |
3754 |
||
22727 | 3755 |
arcTan:denominator |
3756 |
"Evaluate the four quadrant arc tangent of the argument denominator (x) and the receiver (y)." |
|
3757 |
||
3758 |
|t| |
|
3759 |
||
3760 |
(self isZero) ifTrue: [ |
|
3761 |
(denominator strictlyPositive) |
|
3762 |
ifTrue: [ ^ 0 ] |
|
3763 |
ifFalse: [ ^ self class pi ] |
|
3764 |
]. |
|
3765 |
(denominator isZero) ifTrue: [ |
|
3766 |
(self strictlyPositive) |
|
3767 |
ifTrue: [ ^ self class halfpi ] |
|
3768 |
ifFalse: [ ^ self class halfpiNegative ] |
|
3769 |
]. |
|
3770 |
t := (self / denominator) arcTan. |
|
3771 |
(denominator strictlyPositive) |
|
3772 |
ifTrue: [ ^ t ] |
|
3773 |
ifFalse: [ ^ t + self class pi ] |
|
3774 |
||
3775 |
"Created: / 07-06-2007 / 21:10:32 / cg" |
|
3776 |
"Modified: / 11-06-2007 / 12:58:34 / cg" |
|
3777 |
! |
|
3778 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3779 |
arcTanh |
19768 | 3780 |
"return the inverse hyperbolic tangent of the receiver." |
19769 | 3781 |
"caveat: misnomer; should be called aTanh or arTanh" |
20308 | 3782 |
|
19769 | 3783 |
"/ if I am not a Float (or a less general lpReal), |
3784 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3785 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3786 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3787 |
|f| |
3788 |
||
3789 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3790 |
^ f arcTanh. |
|
3791 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3792 |
]. |
19769 | 3793 |
"/ slow fallback |
19768 | 3794 |
^ ((1 + self) / (1 - self)) ln / 2 |
3795 |
"/ s^ ((1 + self) ln / 2) - ((1 - self) ln / 2) |
|
21988 | 3796 |
|
3797 |
"Modified: / 05-07-2017 / 17:26:27 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3798 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3799 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3800 |
cos |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3801 |
"return the cosine of the receiver (interpreted as radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3802 |
|
19769 | 3803 |
"/ if I am not a Float (or a less general lpReal), |
3804 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3805 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3806 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3807 |
|f| |
3808 |
||
3809 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3810 |
^ f cos. |
|
3811 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3812 |
]. |
19769 | 3813 |
"/ very slow fallback |
8634 | 3814 |
^ self cos_withAccuracy:self epsilon |
21988 | 3815 |
|
3816 |
"Modified: / 05-07-2017 / 17:26:33 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3817 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3818 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3819 |
cosh |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3820 |
"return the hyperbolic cosine of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3821 |
|
19769 | 3822 |
"/ if I am not a Float (or a less general lpReal), |
3823 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3824 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3825 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3826 |
|f| |
3827 |
||
3828 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3829 |
^ f cosh. |
|
3830 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3831 |
]. |
19769 | 3832 |
"/ very slow fallback |
8634 | 3833 |
^ self cosh_withAccuracy:self epsilon |
21988 | 3834 |
|
3835 |
"Modified: / 05-07-2017 / 17:26:39 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3836 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3837 |
|
8844 | 3838 |
cot |
3839 |
"return the cotangent of the receiver" |
|
3840 |
||
3841 |
^ 1 / self tan |
|
3842 |
! |
|
3843 |
||
24896 | 3844 |
csc |
3845 |
"return the cosecant of the receiver (interpreted as radians)" |
|
3846 |
||
3847 |
^ 1 / self sin |
|
3848 |
! |
|
3849 |
||
3850 |
sec |
|
3851 |
"return the secant of the receiver (interpreted as radians)" |
|
3852 |
||
3853 |
^ 1 / self cos |
|
3854 |
! |
|
3855 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3856 |
sin |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3857 |
"return the sine of the receiver (interpreted as radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3858 |
|
19769 | 3859 |
"/ if I am not a Float (or a less general lpReal), |
3860 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3861 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3862 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3863 |
|f| |
3864 |
||
3865 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3866 |
^ f sin. |
|
3867 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3868 |
]. |
8634 | 3869 |
^ self sin_withAccuracy:self epsilon |
21988 | 3870 |
|
3871 |
"Modified: / 05-07-2017 / 17:26:47 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3872 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3873 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3874 |
sinh |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3875 |
"return the hyperbolic sine of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3876 |
|
19769 | 3877 |
"/ if I am not a Float (or a less general lpReal), |
3878 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3879 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3880 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3881 |
|f| |
3882 |
||
3883 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3884 |
^ f sinh. |
|
3885 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3886 |
]. |
8634 | 3887 |
^ self sinh_withAccuracy:self epsilon |
21988 | 3888 |
|
3889 |
" |
|
3890 |
10 sinh -> 11013.23287470339338 |
|
3891 |
(10 exp - (-10 exp)) / 2 -> 11013.23287470339338 |
|
3892 |
" |
|
3893 |
||
3894 |
"Modified (comment): / 05-07-2017 / 17:32:27 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3895 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3896 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3897 |
tan |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3898 |
"return the tangens of the receiver (interpreted as radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3899 |
|
19769 | 3900 |
"/ slow fallback |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3901 |
^ self sin / self cos |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3902 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3903 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3904 |
tanh |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3905 |
"return the hyperbolic tangens of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3906 |
|
21988 | 3907 |
"/ tanh is: |
3908 |
"/ sinh(x) |
|
3909 |
"/ ------- |
|
3910 |
"/ cosh(x) |
|
3911 |
"/ |
|
3912 |
"/ which is: |
|
3913 |
"/ (exp(x) - exp(-x)) / 2 |
|
3914 |
"/ ---------------------- |
|
3915 |
"/ (exp(x) + exp(-x)) / 2 |
|
3916 |
||
3917 |
|exp nexp| |
|
3918 |
||
19769 | 3919 |
"/ if I am not a Float (or a less general lpReal), |
3920 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3921 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3922 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3923 |
|f| |
3924 |
||
3925 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3926 |
^ f tanh. |
|
3927 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3928 |
]. |
21988 | 3929 |
|
19769 | 3930 |
"/ very slow fallback |
21988 | 3931 |
"/ assuming that if we arrive here, the stuff is taylor computed manually anyway, |
3932 |
"/ the question is if taylor-for-exp converges faster than tailor-for-sin/cos |
|
3933 |
"/ So it may be faster to: |
|
3934 |
"/ ^ self sinh / self cosh |
|
3935 |
||
3936 |
exp := self exp. |
|
3937 |
nexp := self negated exp. |
|
3938 |
||
3939 |
^ (exp - nexp) / (exp + nexp) |
|
3940 |
||
3941 |
"Modified (comment): / 05-07-2017 / 17:34:59 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3942 |
! ! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3943 |
|
24158 | 3944 |
!Number methodsFor:'trigonometric (degrees)'! |
3945 |
||
3946 |
degreeCos |
|
3947 |
"Return the cosine of the receiver taken as an angle in degrees." |
|
3948 |
||
3949 |
^ self degreesToRadians cos |
|
3950 |
! |
|
3951 |
||
3952 |
degreeSin |
|
3953 |
"Return the sine of the receiver taken as an angle in degrees." |
|
3954 |
||
3955 |
^ self degreesToRadians sin |
|
3956 |
! |
|
3957 |
||
3958 |
degreeTan |
|
3959 |
"Return the cosine of the receiver taken as an angle in degrees." |
|
3960 |
||
3961 |
^ self degreesToRadians tan |
|
3962 |
! ! |
|
3963 |
||
5552 | 3964 |
!Number methodsFor:'truncation & rounding'! |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3965 |
|
4895 | 3966 |
detentBy: detent atMultiplesOf: grid snap: snap |
20308 | 3967 |
"Map all values that are within detent/2 of any multiple of grid |
3968 |
to that multiple. |
|
3969 |
Otherwise, if snap is true, return self, meaning that the values |
|
3970 |
in the dead zone will never be returned. |
|
4895 | 3971 |
If snap is false, then expand the range between dead zones |
20308 | 3972 |
so that it covers the range between multiples of the grid, |
4895 | 3973 |
and scale the value by that factor." |
3974 |
||
3975 |
| r1 r2 | |
|
3976 |
||
3977 |
r1 := self roundTo: grid. "Nearest multiple of grid" |
|
3978 |
(self roundTo: detent) = r1 ifTrue: [^ r1]. "Snap to that multiple..." |
|
3979 |
snap ifTrue: [^ self]. "...or return self" |
|
3980 |
||
3981 |
r2 := self < r1 "Nearest end of dead zone" |
|
20308 | 3982 |
ifTrue: [r1 - (detent asFloat/2)] |
3983 |
ifFalse: [r1 + (detent asFloat/2)]. |
|
4895 | 3984 |
|
3985 |
"Scale values between dead zones to fill range between multiples" |
|
3986 |
^ r1 + ((self - r2) * grid asFloat / (grid - detent)) |
|
3987 |
||
3988 |
" |
|
20308 | 3989 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: true] |
4895 | 3990 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: false] |
20308 | 3991 |
(3.9 to: 4.1 by: 0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: true] |
4895 | 3992 |
(-3.9 to: -4.1 by: -0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: false] |
3993 |
" |
|
3994 |
! |
|
3995 |
||
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3996 |
fractionPart |
25051 | 3997 |
"return a number with value from digits after the decimal point. |
7356 | 3998 |
i.e. the receiver minus its truncated value, |
25051 | 3999 |
such that (self truncated + self fractionPart) = self |
4000 |
Floats will return an inexact float; fractions will return a fraction" |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4001 |
|
7356 | 4002 |
^ self - self truncated |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4003 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4004 |
" |
24996 | 4005 |
1234.56789 fractionPart -- beware rounding errors in floats |
25051 | 4006 |
1.2345e0 fractionPart |
4007 |
1.2345e1 fractionPart |
|
24996 | 4008 |
1.2345e6 fractionPart |
25051 | 4009 |
(16/10) fractionPart |
4010 |
(8/9) fractionPart |
|
4011 |
(11/9) fractionPart |
|
24996 | 4012 |
|
4013 |
1.6 asLongFloat fractionPart + 1.6 asLongFloat truncated -- beware rounding errors in floats |
|
4014 |
-1.6 asLongFloat fractionPart + -1.6 asLongFloat truncated -1.600000000000000089 |
|
4015 |
(16/10) fractionPart + (16/10) truncated -> 16/10 |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4016 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4017 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4018 |
"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
|
4019 |
"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
|
4020 |
! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4021 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4022 |
integerPart |
25051 | 4023 |
"return a number with value from digits before the decimal point. |
4024 |
(i.e. the value truncated towards zero) |
|
4025 |
such that (self integerPart + self fractionPart) = self |
|
4026 |
Floats will return an inexact float; fractions will return an exact integer. |
|
4027 |
" |
|
4028 |
||
4029 |
^ self truncated |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4030 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4031 |
" |
24996 | 4032 |
1234.56789 integerPart |
20308 | 4033 |
1.2345e6 integerPart |
4034 |
12.5 integerPart |
|
4035 |
-12.5 integerPart |
|
4036 |
(5/3) integerPart |
|
4037 |
(-5/3) integerPart |
|
25051 | 4038 |
(5/3) truncated |
4039 |
(-5/3) truncated |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4040 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4041 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4042 |
"Created: / 28.10.1998 / 17:14:56 / cg" |
6154 | 4043 |
"Modified: / 5.11.2001 / 17:54:22 / cg" |
23323 | 4044 |
! |
4045 |
||
4046 |
rounded:n |
|
4047 |
"Answer the float rounded with n digits of precision" |
|
4048 |
||
4049 |
| mult | |
|
4050 |
||
4051 |
mult := 10 raisedTo: n. |
|
24996 | 4052 |
^ (((self * mult) rounded) asLimitedPrecisionReal / mult). |
23323 | 4053 |
|
4054 |
" |
|
4055 |
7 rounded:2 |
|
4056 |
7.1 rounded:2 |
|
24996 | 4057 |
7.2345 rounded:2 -> 7.23 |
4058 |
7.2385 rounded:2 -> 7.24 |
|
4059 |
7.2341 rounded:3 -> 7.234 |
|
23323 | 4060 |
7.2345 rounded:3 |
4061 |
7.2348 rounded:3 |
|
24996 | 4062 |
(1/3) rounded:3 -> 0.333 |
4063 |
(2/3) rounded:3 -> 0.667 |
|
23323 | 4064 |
" |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4065 |
! ! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
4066 |
|
25144 | 4067 |
|
1892 | 4068 |
!Number class methodsFor:'documentation'! |
701 | 4069 |
|
4070 |
version |
|
18850 | 4071 |
^ '$Header$' |
12112
949f9713c4a5
changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents:
11944
diff
changeset
|
4072 |
! |
949f9713c4a5
changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents:
11944
diff
changeset
|
4073 |
|
949f9713c4a5
changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents:
11944
diff
changeset
|
4074 |
version_CVS |
18850 | 4075 |
^ '$Header$' |
701 | 4076 |
! ! |
15078 | 4077 |