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