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