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