author | Claus Gittinger <cg@exept.de> |
Tue, 09 Jul 2019 20:55:17 +0200 | |
changeset 24417 | 03b083548da2 |
parent 24395 | c245e7c02c2d |
child 24431 | 47ab366e910f |
permissions | -rw-r--r-- |
24394 | 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. |
|
24394 | 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:[ |
|
24394 | 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 |
||
24262 | 733 |
eDigits |
734 |
"return th printString of the irrational number e, |
|
735 |
with enough digits so that instances with different precision can read from it" |
|
736 |
||
737 |
"/ number asked from wolfram |
|
738 |
^ '2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746' |
|
739 |
||
740 |
"Created: / 06-06-2019 / 16:58:50 / Claus Gittinger" |
|
741 |
! |
|
742 |
||
21930 | 743 |
i |
744 |
"return the imaginary unit i" |
|
745 |
||
746 |
||
747 |
^ Complex real:0 imaginary:1 |
|
748 |
||
749 |
" |
|
22287 | 750 |
1 + Number i -> (1+1i) |
751 |
Number i + 10 -> (10+1i) |
|
752 |
Number i * Number i -> -1 |
|
21930 | 753 |
" |
754 |
||
755 |
"Created: / 01-07-2017 / 19:44:53 / cg" |
|
22287 | 756 |
"Modified (comment): / 22-09-2017 / 09:53:14 / cg" |
21930 | 757 |
! |
758 |
||
21828 | 759 |
ln10 |
760 |
"return ln(10) in my representation (and accuracy)." |
|
761 |
||
762 |
^ self subclassResponsibility |
|
763 |
||
764 |
"Created: / 16-06-2017 / 11:00:38 / cg" |
|
765 |
! |
|
766 |
||
767 |
pi |
|
768 |
"return Pi in my representation (and accuracy)." |
|
769 |
||
770 |
^ self subclassResponsibility |
|
771 |
||
772 |
"Modified (format): / 16-06-2017 / 11:00:42 / cg" |
|
24262 | 773 |
! |
774 |
||
775 |
piDigits |
|
776 |
"return th printString of the irrational number pi, |
|
777 |
with enough digits so that instances with different precision can read from it" |
|
778 |
||
779 |
"/ number asked from wolfram |
|
780 |
"/ ^ '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904' |
|
781 |
"/ rounded to 100 digits |
|
782 |
^ '3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068' |
|
783 |
||
784 |
"Created: / 06-06-2019 / 17:08:17 / Claus Gittinger" |
|
21828 | 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 |
||
24322 | 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 |
||
24322 | 1055 |
epsilon |
1056 |
"return the maximum relative spacing of instances of mySelf |
|
1057 |
(i.e. the value-delta of the least significant bit)" |
|
1058 |
||
1059 |
^ self subclassResponsibility |
|
1060 |
! |
|
1061 |
||
1062 |
epsilonForCloseTo |
|
1063 |
"return the epsilon used in the closeTo: comparison. |
|
1064 |
(useful would be something like self epsilon or epsilon*10, |
|
1065 |
but for Squeak compatibility.... - sigh)" |
|
1066 |
||
1067 |
^ 0.0001 |
|
1068 |
||
1069 |
" |
|
1070 |
Float epsilon |
|
1071 |
ShortFloat epsilon |
|
1072 |
Float epsilon10 |
|
1073 |
ShortFloat epsilon10 |
|
1074 |
" |
|
1075 |
! |
|
1076 |
||
8892 | 1077 |
isAbstract |
11224 | 1078 |
"Return if this class is an abstract class. |
1079 |
True is returned for Number here; false for subclasses. |
|
19457 | 1080 |
Abstract subclasses must redefine this again." |
11224 | 1081 |
|
8892 | 1082 |
^ self == Number |
1083 |
! ! |
|
1084 |
||
24322 | 1085 |
|
7261 | 1086 |
!Number methodsFor:'Compatibility-Squeak'! |
4988 | 1087 |
|
1088 |
asSmallAngleDegrees |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1089 |
"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
|
1090 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1091 |
| pos | |
4988 | 1092 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1093 |
pos := self \\ 360. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1094 |
pos > 180 ifTrue: [pos := pos - 360]. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1095 |
^ pos |
4988 | 1096 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1097 |
" |
20308 | 1098 |
#(-500 -300 -150 -5 0 5 150 300 500 1200) |
1099 |
collect: [:n | n asSmallAngleDegrees] |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1100 |
" |
6072 | 1101 |
! |
4988 | 1102 |
|
23323 | 1103 |
sqrtWithErrorLessThan:epsilon |
1104 |
"compute the square root, using the Newton method. |
|
1105 |
The approximated return value has an error less than the given epsilon." |
|
1106 |
||
1107 |
^ self sqrt_withAccuracy:epsilon |
|
1108 |
||
1109 |
" |
|
1110 |
(2 asFixedPoint:4) sqrtWithErrorLessThan:0.001 |
|
1111 |
" |
|
1112 |
||
1113 |
"Modified: / 25-07-2017 / 15:58:46 / cg" |
|
1114 |
! ! |
|
1115 |
||
1116 |
||
1117 |
!Number methodsFor:'coercing & converting'! |
|
1118 |
||
1119 |
i |
|
1120 |
"return a complex number, with the receiver as imaginary part, 0 as real part" |
|
1121 |
||
1122 |
^ Complex real:0 imaginary:self |
|
1123 |
||
1124 |
" |
|
1125 |
3i -> (0+3i) |
|
1126 |
(1+1i) -> (1+1i) |
|
1127 |
" |
|
1128 |
||
1129 |
"Modified (format): / 22-09-2017 / 09:53:27 / cg" |
|
1130 |
! ! |
|
1131 |
||
1132 |
!Number methodsFor:'comparing'! |
|
1133 |
||
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1134 |
closeFrom:aNumber |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1135 |
"are these two numbers close?" |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1136 |
|
10727 | 1137 |
^ self closeFrom:aNumber withEpsilon:(self class epsilonForCloseTo) |
1138 |
||
1139 |
" |
|
20308 | 1140 |
9.0 closeTo: 8.9999 |
1141 |
9.9 closeTo: 9 |
|
1142 |
(9/3) closeTo: 2.9999 |
|
1143 |
1 closeTo: 0.9999 |
|
1144 |
1 closeTo: 1.0001 |
|
1145 |
1 closeTo: 1.001 |
|
1146 |
1 closeTo: 0.999 |
|
1147 |
||
1148 |
0.9999 closeTo: 1 |
|
1149 |
1.0001 closeTo: 1 |
|
1150 |
1.001 closeTo: 1 |
|
10727 | 1151 |
0.999 closeTo: 1 |
1152 |
Float NaN closeTo:Float NaN |
|
1153 |
Float infinity closeTo:Float infinity |
|
1154 |
" |
|
1155 |
! |
|
1156 |
||
1157 |
closeFrom:aNumber withEpsilon:eps |
|
1158 |
"are these two numbers close?" |
|
1159 |
||
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1160 |
| fuzz | |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1161 |
|
20308 | 1162 |
self isNaN == aNumber isNaN ifFalse: [^ false]. |
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1163 |
self isInfinite == aNumber isInfinite ifFalse: [^ false]. |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1164 |
|
20308 | 1165 |
fuzz := (self abs max:aNumber abs) * eps. |
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1166 |
^ (self - aNumber) abs <= fuzz |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1167 |
|
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1168 |
" |
20308 | 1169 |
9.0 closeTo: 8.9999 |
23322 | 1170 |
9.0 closeTo: 8.9999 withEpsilon:0.1 |
1171 |
9.0 closeTo: 8.9999 withEpsilon:0.0001 |
|
1172 |
9.0 closeTo: 8.9999 withEpsilon:0.00001 |
|
1173 |
||
20308 | 1174 |
9.9 closeTo: 9 |
23322 | 1175 |
9.9 closeTo: 9 withEpsilon:1 |
1176 |
||
20308 | 1177 |
(9/3) closeTo: 2.9999 |
1178 |
1 closeTo: 0.9999 |
|
23322 | 1179 |
1 closeTo: 0.9999 withEpsilon:0.0001 |
1180 |
1 closeTo: 0.9999 withEpsilon:0.00001 |
|
1181 |
||
20308 | 1182 |
1 closeTo: 1.0001 |
1183 |
1 closeTo: 1.001 |
|
1184 |
1 closeTo: 0.999 |
|
6335
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1185 |
" |
23322 | 1186 |
|
1187 |
"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
|
1188 |
! |
8286bc57d05e
closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents:
6258
diff
changeset
|
1189 |
|
6143 | 1190 |
closeTo:num |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1191 |
"are these two numbers close to each other?" |
6143 | 1192 |
|
10727 | 1193 |
^ self closeTo:num withEpsilon:(self class epsilonForCloseTo) |
1194 |
||
1195 |
" |
|
20308 | 1196 |
1 closeTo:1.0000000001 |
10727 | 1197 |
1 closeTo:1.001 |
1198 |
1 closeTo:1.001 withEpsilon:0.001 |
|
1199 |
" |
|
1200 |
||
1201 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
1202 |
! |
|
1203 |
||
1204 |
closeTo:num withEpsilon:eps |
|
1205 |
"are these two numbers close to each other?" |
|
1206 |
||
6374 | 1207 |
num isNumber ifFalse:[^false]. |
10727 | 1208 |
^ num closeFrom:self withEpsilon:eps |
6143 | 1209 |
|
1210 |
" |
|
20308 | 1211 |
1 closeTo:1.0000000001 |
1212 |
1 closeTo:1.001 |
|
1213 |
||
1214 |
1 closeTo:1.001 withEpsilon:0.1 |
|
1215 |
1 closeTo:1.201 withEpsilon:0.1 |
|
12979
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1216 |
|
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1217 |
3.14 closeTo:(3.14 asFixedPoint:2) |
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1218 |
(3.14 asFixedPoint:2) closeTo:3.14 |
6143 | 1219 |
" |
6155 | 1220 |
|
12979
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1221 |
"Created: / 05-11-2001 / 18:07:26 / cg" |
917a726bf375
comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents:
12762
diff
changeset
|
1222 |
"Modified: / 02-08-2010 / 13:27:22 / cg" |
6143 | 1223 |
! |
1224 |
||
24322 | 1225 |
epsilonForCloseTo |
1226 |
"return the epsilon used in the closeTo: comparison." |
|
1227 |
||
1228 |
^ self class epsilonForCloseTo |
|
1229 |
||
1230 |
" |
|
1231 |
1.0 epsilon |
|
1232 |
10 asShortFloat epsilon |
|
1233 |
" |
|
1234 |
||
1235 |
"Created: / 10-06-2019 / 21:20:18 / Claus Gittinger" |
|
1236 |
! |
|
1237 |
||
20308 | 1238 |
isAlmostEqualTo:aNumber nEpsilon:nE |
16656 | 1239 |
"return true, if the argument, aNumber represents almost the same numeric value |
1240 |
as the receiver, false otherwise. |
|
1241 |
||
1242 |
nE is the number of minimal float distances, that the numbers may differ and |
|
17207 | 1243 |
still be considered equal. See documentation in LimitedPrecisionReal for more detail. |
16656 | 1244 |
|
20308 | 1245 |
For background information why floats need this |
16656 | 1246 |
read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
1247 |
" |
|
1248 |
||
1249 |
|f1 f2 diff scaledEpsilon largest| |
|
1250 |
||
1251 |
diff := (self - aNumber) abs. |
|
1252 |
||
1253 |
scaledEpsilon := nE * diff class epsilon. |
|
1254 |
||
1255 |
diff <= scaledEpsilon ifTrue:[ |
|
21812 | 1256 |
"compare for really close values near 0" |
1257 |
^ true. |
|
16656 | 1258 |
]. |
21812 | 1259 |
|
1260 |
"scaled comparison for larger values" |
|
1261 |
f1 := self abs. |
|
1262 |
f2 := aNumber abs. |
|
1263 |
largest := f1 > f2 ifTrue:[f1] ifFalse:[f2]. |
|
1264 |
^ (diff <= (scaledEpsilon * largest)). |
|
1265 |
||
1266 |
"Modified: / 15-06-2017 / 09:55:15 / cg" |
|
22290 | 1267 |
! |
1268 |
||
1269 |
isAlmostEqualTo:aNumber withError:errFraction |
|
1270 |
"return true, if the receiver,is inside the interval aNumber-err .. aNumber+err. |
|
1271 |
Err is a fraction of 0..1." |
|
1272 |
||
1273 |
^ self between:(aNumber * (1-errFraction)) and:(aNumber * (1+errFraction)) |
|
1274 |
||
1275 |
" |
|
1276 |
within 10%? |
|
1277 |
10.5 isAlmostEqualTo:10 withError:0.1 |
|
1278 |
||
1279 |
within 1% |
|
1280 |
10.5 isAlmostEqualTo:10 withError:0.01 |
|
1281 |
" |
|
1282 |
||
1283 |
"Created: / 23-09-2017 / 16:33:26 / cg" |
|
23323 | 1284 |
! |
1285 |
||
1286 |
isEqual: aNumber within: accuracy |
|
1287 |
^(self - aNumber) abs < accuracy |
|
16656 | 1288 |
! ! |
1289 |
||
701 | 1290 |
!Number methodsFor:'converting'! |
1291 |
||
20308 | 1292 |
% aNumber |
1293 |
"Return a complex number with the receiver as the real part and |
|
3634 | 1294 |
aNumber as the imaginary part" |
1295 |
||
1296 |
^ Complex real:self imaginary:aNumber |
|
1297 |
||
1298 |
"Modified: / 9.7.1998 / 10:18:12 / cg" |
|
1299 |
! |
|
1300 |
||
11458 | 1301 |
+/- anError |
14009 | 1302 |
"return a MeasurementValue with a given error." |
11458 | 1303 |
|
1304 |
^ MeasurementValue value:self minValue:(self-anError) maxValue:(self+anError) |
|
1305 |
||
1306 |
" |
|
20308 | 1307 |
(100 +/- 5) * 2 |
1308 |
(100 +/- 5) * (100 +/- 10) |
|
1309 |
(100 +/- 5) + (100 +/- 10) |
|
1310 |
(100 +/- 5) - (100 +/- 10) |
|
11458 | 1311 |
" |
14009 | 1312 |
|
1313 |
"Modified (comment): / 14-02-2012 / 14:17:36 / cg" |
|
11458 | 1314 |
! |
1315 |
||
701 | 1316 |
@ aNumber |
1317 |
"return a Point with the receiver as x-coordinate and the argument |
|
1318 |
as y-coordinate" |
|
1319 |
||
1320 |
%{ /* NOCONTEXT */ |
|
1321 |
||
1322 |
/* |
|
1323 |
* I cannot tell if this special code is worth anything |
|
1324 |
*/ |
|
5322 | 1325 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
20308 | 1326 |
OBJ newPoint; |
1327 |
int spc; |
|
1328 |
||
1329 |
__qCheckedNew(newPoint, sizeof(struct __Point)); |
|
1330 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
1331 |
__qSTORE(newPoint, @global(Point)); |
|
1332 |
__PointInstPtr(newPoint)->p_x = self; |
|
1333 |
__PointInstPtr(newPoint)->p_y = aNumber; |
|
1334 |
if (! __bothSmallInteger(self, aNumber)) { |
|
1335 |
spc = __qSpace(newPoint); |
|
1336 |
__STORE_SPC(newPoint, aNumber, spc); |
|
1337 |
__STORE_SPC(newPoint, self, spc); |
|
1338 |
} |
|
1339 |
RETURN ( newPoint ); |
|
701 | 1340 |
} |
1341 |
%} |
|
1342 |
. |
|
1343 |
^ Point x:self y:aNumber |
|
1344 |
! |
|
1345 |
||
3634 | 1346 |
asComplex |
20308 | 1347 |
"Return a complex number with the receiver as the real part and |
3634 | 1348 |
zero as the imaginary part" |
1349 |
||
7356 | 1350 |
^ Complex real:self |
3634 | 1351 |
|
1352 |
"Modified: / 9.7.1998 / 10:18:16 / cg" |
|
1353 |
! |
|
1354 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1355 |
asMetaNumber |
21894 | 1356 |
self isNaN ifTrue:[ |
1357 |
^ NotANumber NaN |
|
1358 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1359 |
^ SomeNumber new realNumber:self |
21894 | 1360 |
|
1361 |
" |
|
1362 |
Float NaN asMetaNumber |
|
1363 |
Float infinity asMetaNumber |
|
1364 |
Float negativeInfinity asMetaNumber |
|
1365 |
" |
|
1366 |
||
1367 |
"Modified (comment): / 21-06-2017 / 20:47:59 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1368 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1369 |
|
7214 | 1370 |
asNumber |
17060 | 1371 |
"I am a number, so return myself" |
7214 | 1372 |
|
1373 |
^ self. |
|
1374 |
! |
|
1375 |
||
19702 | 1376 |
asPercentFrom:fullAmount |
1377 |
"what is the percentage |
|
1378 |
taking the receiver's value from the argument" |
|
20308 | 1379 |
|
19702 | 1380 |
^ (self / fullAmount) * 100. |
19701 | 1381 |
|
1382 |
" |
|
1383 |
20 asPercentFrom:100 |
|
1384 |
(10 asPercentFrom:156) asFixedPoint:2 |
|
19702 | 1385 |
(15.6 asPercentFrom:156) asFixedPoint:2 |
19701 | 1386 |
" |
1387 |
! |
|
1388 |
||
701 | 1389 |
asPoint |
20308 | 1390 |
"return a new Point with the receiver as all coordinates; |
1391 |
often used to supply the same value in two dimensions, as with |
|
701 | 1392 |
symmetrical gridding or scaling." |
1393 |
||
1394 |
%{ /* NOCONTEXT */ |
|
1395 |
||
5322 | 1396 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
20308 | 1397 |
OBJ newPoint; |
1398 |
||
1399 |
__qCheckedNew(newPoint, sizeof(struct __Point)); |
|
1400 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
1401 |
__qSTORE(newPoint, @global(Point)); |
|
1402 |
__PointInstPtr(newPoint)->p_x = self; |
|
1403 |
__PointInstPtr(newPoint)->p_y = self; |
|
1404 |
__STORE(newPoint, self); |
|
1405 |
RETURN ( newPoint ); |
|
701 | 1406 |
} |
1407 |
%}. |
|
1408 |
^ Point x:self y:self |
|
1409 |
! |
|
1410 |
||
13898 | 1411 |
asTimeDuration |
23619 | 1412 |
"return an TimeDuration object from the receiver, |
1413 |
taking the receiver as number of seconds" |
|
13898 | 1414 |
|
20308 | 1415 |
^ TimeDuration seconds:self |
13898 | 1416 |
|
1417 |
" |
|
20308 | 1418 |
5 asTimeDuration |
1419 |
50.25 asTimeDuration |
|
1420 |
3600 asTimeDuration |
|
23619 | 1421 |
'5m' asTimeDuration |
13898 | 1422 |
" |
1423 |
||
1424 |
"Created: / 08-01-2012 / 19:04:04 / cg" |
|
23619 | 1425 |
"Modified (comment): / 21-01-2019 / 10:31:15 / Claus Gittinger" |
13898 | 1426 |
! |
1427 |
||
701 | 1428 |
degreesToRadians |
21105 | 1429 |
"interpreting the receiver as degrees, return the radians" |
701 | 1430 |
|
8203 | 1431 |
^ 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
|
1432 |
|
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1433 |
" |
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1434 |
180 degreesToRadians |
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1435 |
Float pi radiansToDegrees |
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1436 |
" |
701 | 1437 |
! |
1438 |
||
1439 |
literalArrayEncoding |
|
1245 | 1440 |
"encode myself as an array literal, from which a copy of the receiver |
1441 |
can be reconstructed with #decodeAsLiteralArray." |
|
701 | 1442 |
|
1443 |
^ self |
|
1444 |
||
1445 |
"Modified: 1.9.1995 / 02:25:26 / claus" |
|
1245 | 1446 |
"Modified: 22.4.1996 / 13:00:27 / cg" |
701 | 1447 |
! |
1448 |
||
19701 | 1449 |
percentOf:hundredPercent |
1450 |
"how many is self-percent from the argument" |
|
20308 | 1451 |
|
19701 | 1452 |
^ (hundredPercent / 100 * self) |
1453 |
||
1454 |
" |
|
1455 |
20 percentOf:100 |
|
1456 |
(10 percentOf:156) asFixedPoint:2 |
|
1457 |
(105 percentOf:156) asFixedPoint:2 |
|
1458 |
" |
|
1459 |
! |
|
1460 |
||
701 | 1461 |
radiansToDegrees |
21105 | 1462 |
"interpreting the receiver as radians, return the degrees" |
701 | 1463 |
|
8203 | 1464 |
^ 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
|
1465 |
|
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1466 |
" |
20308 | 1467 |
180 degreesToRadians |
7388
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1468 |
Float pi radiansToDegrees |
bb89a53e2682
fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1469 |
" |
6637 | 1470 |
! |
1471 |
||
1472 |
withScale:newScale |
|
20308 | 1473 |
"return a fixedPoint number representing the same value as the receiver, |
6637 | 1474 |
with newScale number of post-decimal digits" |
1475 |
||
1476 |
^ self asFixedPoint:newScale |
|
1477 |
||
1478 |
" |
|
20308 | 1479 |
1234 withScale:2 |
1480 |
1234.1 withScale:2 |
|
1481 |
1234.12 withScale:2 |
|
1482 |
1234.123 withScale:2 |
|
6637 | 1483 |
(1/7) withScale:2 |
1484 |
" |
|
701 | 1485 |
! ! |
1486 |
||
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1487 |
!Number methodsFor:'converting-times'! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1488 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1489 |
days |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1490 |
"return a TimeDuration representing this number of days" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1491 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1492 |
^ TimeDuration days:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1493 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1494 |
" |
20308 | 1495 |
1000 milliseconds |
1496 |
10 seconds |
|
1497 |
10 minutes |
|
1498 |
1 days |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1499 |
" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1500 |
! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1501 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1502 |
hours |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1503 |
"return a TimeDuration representing this number of hours" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1504 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1505 |
^ TimeDuration hours:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1506 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1507 |
" |
20308 | 1508 |
1000 milliseconds |
1509 |
10 seconds |
|
1510 |
10 minutes |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1511 |
" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1512 |
! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1513 |
|
22844 | 1514 |
microseconds |
1515 |
"return a TimeDuration representing this number of microseconds." |
|
1516 |
||
1517 |
^ TimeDuration fromMicroseconds:self |
|
1518 |
||
1519 |
" |
|
1520 |
40 microseconds |
|
1521 |
" |
|
1522 |
||
1523 |
"Modified (comment): / 21-09-2017 / 17:37:18 / cg" |
|
1524 |
! |
|
1525 |
||
22279 | 1526 |
milliSeconds |
1527 |
"return a TimeDuration representing this number of milliseconds |
|
1528 |
Same as milliseconds, for compatibility" |
|
1529 |
||
1530 |
^ TimeDuration fromMilliseconds:self |
|
1531 |
||
1532 |
" |
|
1533 |
1000 milliSeconds |
|
1534 |
" |
|
1535 |
||
1536 |
"Created: / 21-09-2017 / 17:26:32 / cg" |
|
1537 |
! |
|
1538 |
||
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1539 |
milliseconds |
22279 | 1540 |
"return a TimeDuration representing this number of milliseconds. |
1541 |
Same as milliSeconds, for compatibility" |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1542 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1543 |
^ TimeDuration fromMilliseconds:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1544 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1545 |
" |
20308 | 1546 |
1000 milliseconds |
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1547 |
" |
22279 | 1548 |
|
1549 |
"Modified (comment): / 21-09-2017 / 17:37:18 / cg" |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1550 |
! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1551 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1552 |
minutes |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1553 |
"return a TimeDuration representing this number of minutes" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1554 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1555 |
^ TimeDuration minutes:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1556 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1557 |
" |
20308 | 1558 |
1000 milliseconds |
1559 |
10 seconds |
|
1560 |
10 minutes |
|
11008
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 |
! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1563 |
|
22844 | 1564 |
nanoseconds |
1565 |
"return a TimeDuration representing this number of nanoseconds." |
|
1566 |
||
1567 |
^ TimeDuration fromNanoseconds:self |
|
1568 |
||
1569 |
" |
|
22869 | 1570 |
40.5 nanoseconds asPicoseconds |
1571 |
(40.5 nanoseconds / 2) asPicoseconds |
|
1572 |
||
22844 | 1573 |
40 nanoseconds |
22869 | 1574 |
40 nanoseconds asPicoseconds |
1575 |
44 milliseconds asMicroseconds |
|
1576 |
44 milliseconds |
|
22844 | 1577 |
" |
1578 |
||
1579 |
"Modified (comment): / 21-09-2017 / 17:37:18 / cg" |
|
1580 |
! |
|
1581 |
||
1582 |
picoseconds |
|
1583 |
"return a TimeDuration representing this number of picoseconds." |
|
1584 |
||
1585 |
^ TimeDuration fromPicoseconds:self |
|
1586 |
||
1587 |
" |
|
1588 |
40 picoseconds |
|
1589 |
" |
|
1590 |
||
1591 |
"Modified (comment): / 21-09-2017 / 17:37:18 / cg" |
|
1592 |
! |
|
1593 |
||
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1594 |
seconds |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1595 |
"return a TimeDuration representing this number of seconds" |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1596 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1597 |
^ TimeDuration seconds:self |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1598 |
|
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1599 |
" |
20308 | 1600 |
1000 milliseconds |
1601 |
10 seconds |
|
1602 |
10 minutes |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1603 |
" |
13631 | 1604 |
! |
1605 |
||
1606 |
weeks |
|
1607 |
"return a TimeDuration representing this number of weeks" |
|
1608 |
||
1609 |
^ TimeDuration weeks:self |
|
1610 |
||
1611 |
" |
|
20308 | 1612 |
1000 milliseconds |
1613 |
10 seconds |
|
1614 |
10 minutes |
|
1615 |
1 days |
|
1616 |
1 weeks |
|
13631 | 1617 |
" |
1618 |
||
1619 |
"Created: / 05-09-2011 / 11:17:59 / cg" |
|
11008
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1620 |
! ! |
5a92714a522b
timeDuration conversions added
Michael Beyl <mb@exept.de>
parents:
10727
diff
changeset
|
1621 |
|
16406 | 1622 |
!Number methodsFor:'double dispatching'! |
1623 |
||
1624 |
differenceFromTimestamp:aTimestamp |
|
1625 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
1626 |
before aTimestamp" |
|
1627 |
||
1628 |
^ aTimestamp subtractMilliseconds:(self * 1000) truncated. |
|
1629 |
||
1630 |
" |
|
20308 | 1631 |
100.0 differenceFromTimestamp:Timestamp now |
16406 | 1632 |
|
1633 |
|t1 t2| |
|
20308 | 1634 |
t1 := Timestamp now. |
16406 | 1635 |
t2 := 1.5 differenceFromTimestamp:t1. |
1636 |
t1 inspect. t2 inspect. |
|
1637 |
" |
|
1638 |
! ! |
|
1639 |
||
24237 | 1640 |
!Number methodsFor:'inspecting'! |
1641 |
||
1642 |
inspectorValueStringInListFor:anInspector |
|
1643 |
"returns a string to be shown in the inspector's list" |
|
1644 |
||
1645 |
^ self printString contractTo:30 |
|
1646 |
||
1647 |
"Created: / 29-05-2019 / 15:01:19 / Claus Gittinger" |
|
1648 |
! ! |
|
23615 | 1649 |
|
701 | 1650 |
!Number methodsFor:'intervals'! |
1651 |
||
6072 | 1652 |
downTo:stop |
1653 |
"return an interval from receiver down to the argument, incrementing by -1" |
|
1654 |
||
1655 |
^ self to:stop by:-1 |
|
1656 |
||
1657 |
" |
|
1658 |
(10 downTo:1) do:[:i | Transcript showCR:i]. |
|
1659 |
" |
|
1660 |
! |
|
1661 |
||
15595 | 1662 |
downTo:stop by:step |
1663 |
"return an interval from receiver down to the argument, decrementing by step" |
|
1664 |
||
1665 |
^ self to:stop by:step negated |
|
1666 |
||
1667 |
" |
|
1668 |
(10 downTo:1 by:0.5) do:[:i | Transcript showCR:i]. |
|
1669 |
" |
|
1670 |
||
1671 |
"Created: / 01-08-2013 / 14:36:56 / cg" |
|
1672 |
! |
|
1673 |
||
701 | 1674 |
to:stop |
1675 |
"return an interval from receiver up to the argument, incrementing by 1" |
|
1676 |
||
1677 |
^ Interval from:self to:stop |
|
1678 |
! |
|
1679 |
||
1680 |
to:stop by:step |
|
1681 |
"return an interval from receiver up to the argument, incrementing by step" |
|
1682 |
||
1683 |
^ Interval from:self to:stop by:step |
|
6154 | 1684 |
! |
1685 |
||
1686 |
to:stop byFactor:factor |
|
1687 |
"return a geometric series from receiver up to the argument; |
|
1688 |
elements have a constant factor in between" |
|
1689 |
||
1690 |
^ GeometricSeries from:self to:stop byFactor:factor |
|
1691 |
||
1692 |
" |
|
1693 |
(1 to:256 byFactor:2) |
|
20308 | 1694 |
(256 to:1 byFactor:1/2) |
6154 | 1695 |
" |
701 | 1696 |
! ! |
1697 |
||
1698 |
!Number methodsFor:'iteration'! |
|
1699 |
||
1700 |
timesRepeat:aBlock |
|
1701 |
"evaluate the argument, aBlock self times" |
|
1702 |
||
1703 |
|count| |
|
1704 |
||
1705 |
count := self. |
|
1706 |
[count > 0] whileTrue:[ |
|
20308 | 1707 |
aBlock value. |
1708 |
count := count - 1 |
|
701 | 1709 |
] |
24394 | 1710 |
! |
1711 |
||
1712 |
timesRepeatWithExit:aOneArgBlock |
|
1713 |
"evaluate the argument, aBlock self times; |
|
1714 |
pass an exit block to the one-arg-block" |
|
1715 |
||
1716 |
|count exit| |
|
1717 |
||
24395 | 1718 |
exit := [:values | ^ values firstIfEmpty:nil] asVarArgBlock. |
24394 | 1719 |
count := self. |
1720 |
[count > 0] whileTrue:[ |
|
1721 |
aOneArgBlock value:exit. |
|
1722 |
count := count - 1 |
|
1723 |
] |
|
1724 |
||
1725 |
" |
|
1726 |
10 timesRepeatWithExit:[:exit | |
|
1727 |
Transcript showCR:'iteration'. |
|
1728 |
(Random nextBetween:1 and:10) > 7 ifTrue:[exit value]. |
|
1729 |
]. |
|
1730 |
" |
|
1731 |
||
1732 |
"Created: / 28-06-2019 / 12:01:24 / Claus Gittinger" |
|
701 | 1733 |
! ! |
1734 |
||
3634 | 1735 |
!Number methodsFor:'mathematical functions'! |
1736 |
||
21982 | 1737 |
agm:y |
1738 |
"return the arithmetic-geometric mean agm(x, y) of the receiver (x) and the argument, y. |
|
1739 |
See https://en.wikipedia.org/wiki/Arithmetic-geometric_mean |
|
1740 |
and http://www.wolframalpha.com/input/?i=agm(24,+6)" |
|
1741 |
||
1742 |
|ai an gi gn epsilon delta| |
|
1743 |
||
1744 |
ai := (self + y) / 2. |
|
1745 |
gi := (self * y) sqrt. |
|
1746 |
epsilon := self epsilon. |
|
1747 |
||
1748 |
[ |
|
1749 |
an := (ai + gi) / 2. |
|
1750 |
gn := (ai * gi) sqrt. |
|
1751 |
delta := (an - ai) abs. |
|
1752 |
ai := an. |
|
1753 |
gi := gn. |
|
1754 |
] doUntil:[ delta < epsilon ]. |
|
1755 |
^ ai |
|
1756 |
||
1757 |
" |
|
1758 |
24 agm:6 |
|
1759 |
" |
|
1760 |
||
1761 |
"Created: / 03-07-2017 / 12:05:00 / cg" |
|
1762 |
! |
|
1763 |
||
17190 | 1764 |
cbrt |
1765 |
"return the cubic root of the receiver" |
|
1766 |
||
19770 | 1767 |
"/ if I am not a Float (or a less general lpReal), |
1768 |
"/ retry after converting to float |
|
17190 | 1769 |
(self isLimitedPrecisionReal not |
1770 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 1771 |
|f| |
1772 |
||
1773 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
1774 |
^ f cbrt. |
|
1775 |
]. |
|
17190 | 1776 |
]. |
19770 | 1777 |
"/ very slow fallback |
17190 | 1778 |
^ self cbrt_withAccuracy:self epsilon |
21988 | 1779 |
|
1780 |
"Modified: / 05-07-2017 / 17:23:27 / cg" |
|
17190 | 1781 |
! |
1782 |
||
3634 | 1783 |
conjugated |
1784 |
"Return the complex conjugate of this Number." |
|
1785 |
||
1786 |
^ self |
|
1787 |
||
1788 |
"Modified: / 9.7.1998 / 10:17:31 / cg" |
|
1789 |
! |
|
1790 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1791 |
exp |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1792 |
"compute e**x of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1793 |
|
19770 | 1794 |
"/ if I am not a Float (or a less general lpReal), |
1795 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1796 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1797 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 1798 |
|f| |
1799 |
||
1800 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
1801 |
^ f exp. |
|
1802 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1803 |
]. |
19770 | 1804 |
"/ very slow fallback |
8634 | 1805 |
^ self exp_withAccuracy:self epsilon |
21915 | 1806 |
|
21988 | 1807 |
"Modified: / 05-07-2017 / 17:23:36 / 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 |
floorLog:radix |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1811 |
"return the logarithm truncated as an integer" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1812 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1813 |
^ (self log:radix) floor |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1814 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1815 |
|
3634 | 1816 |
imaginary |
23046 | 1817 |
"Return the imaginary part of a complex number. |
1818 |
For non-complex numbers, zero is returned." |
|
3634 | 1819 |
|
1820 |
^ 0 |
|
1821 |
||
23046 | 1822 |
"Modified: / 09-07-1998 / 10:17:24 / cg" |
1823 |
"Modified (comment): / 01-06-2018 / 13:09:09 / Claus Gittinger" |
|
3634 | 1824 |
! |
1825 |
||
21833 | 1826 |
ldexp:exp |
1827 |
"multiply the receiver by an integral power of 2. |
|
1828 |
I.e. return self * (2 ^ exp)" |
|
1829 |
||
1830 |
^ self * (2 raisedTo:exp) |
|
1831 |
||
1832 |
"Created: / 19-06-2017 / 01:43:35 / cg" |
|
1833 |
! |
|
1834 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1835 |
ln |
18862 | 1836 |
"return the natural logarithm of myself. |
1837 |
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
|
1838 |
|
19770 | 1839 |
"/ if I am not a Float (or a less general lpReal), |
1840 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1841 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1842 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 1843 |
|f| |
1844 |
||
1845 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
1846 |
^ f ln. |
|
1847 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1848 |
]. |
19770 | 1849 |
"/ very slow fallback |
8634 | 1850 |
^ self ln_withAccuracy:self epsilon |
19249 | 1851 |
|
1852 |
" |
|
21833 | 1853 |
(10 raisedTo:1000) ln |
19249 | 1854 |
" |
21833 | 1855 |
|
21988 | 1856 |
"Modified: / 05-07-2017 / 17:23:50 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1857 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1858 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1859 |
log |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1860 |
"return log base 10 of the receiver. |
12584 | 1861 |
Alias for log:10." |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1862 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1863 |
^ self log10 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1864 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1865 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1866 |
log10 |
21828 | 1867 |
"return log base-10 of the receiver. |
1868 |
Raises an exception, if the receiver is less or equal to zero. |
|
1869 |
Here, fallback to the general logarithm code." |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1870 |
|
10550
0f2a07595d1c
Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents:
10325
diff
changeset
|
1871 |
(self isLimitedPrecisionReal not |
0f2a07595d1c
Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents:
10325
diff
changeset
|
1872 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 1873 |
|f| |
1874 |
||
1875 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
1876 |
^ f log10. |
|
1877 |
]. |
|
10550
0f2a07595d1c
Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents:
10325
diff
changeset
|
1878 |
]. |
21828 | 1879 |
^ self ln / self class ln10 |
19249 | 1880 |
|
1881 |
" |
|
21828 | 1882 |
(10 raisedTo:1000) log10 |
1883 |
(10 raisedTo:2000) log10 |
|
1884 |
(10 raisedTo:4000) log10 |
|
1885 |
(10 raisedTo:8000) log10 |
|
19249 | 1886 |
" |
21828 | 1887 |
|
21988 | 1888 |
"Modified: / 05-07-2017 / 17:23:06 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1889 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1890 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1891 |
log:aNumber |
18850 | 1892 |
"return log base aNumber of the receiver. |
1893 |
This will usually return a float value" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1894 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1895 |
^ self ln / aNumber ln |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
1896 |
|
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
1897 |
" |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
1898 |
1000 log:10 |
19049 | 1899 |
9 log:3 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
1900 |
(1000 log:10) floor |
19249 | 1901 |
(10 raisedTo:1000) log:10 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
1902 |
" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1903 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1904 |
|
22098 | 1905 |
nthRoot:n |
1906 |
"return the nth root of the receiver" |
|
1907 |
||
1908 |
n == 2 ifTrue:[^ self sqrt ]. |
|
1909 |
n == 3 ifTrue:[^ self cbrt ]. |
|
1910 |
"/ slow fallback |
|
1911 |
^ self nthRoot:n withAccuracy:self epsilon |
|
1912 |
||
1913 |
" |
|
1914 |
10 nthRoot:2 -> 3.16227766016838 |
|
1915 |
10 nthRoot:3 -> 2.154434690031883722 |
|
22287 | 1916 |
|
1917 |
100.0 nthRoot:4 |
|
1918 |
100.0 nthRoot:5 |
|
1919 |
(100.0 nthRoot:6) raisedTo:6 |
|
1920 |
||
1921 |
16.0 nthRoot:2 |
|
1922 |
-16.0 nthRoot:3 |
|
1923 |
-16.0 nthRoot:5 |
|
1924 |
||
1925 |
-16.0 nthRoot:4 |
|
22098 | 1926 |
" |
1927 |
||
1928 |
"Created: / 25-07-2017 / 16:15:11 / cg" |
|
22287 | 1929 |
"Modified (comment): / 22-09-2017 / 10:15:33 / cg" |
22098 | 1930 |
! |
1931 |
||
7468
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1932 |
raisedTo:aNumber |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1933 |
"return the receiver raised to aNumber" |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1934 |
|
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1935 |
aNumber = 0 ifTrue:[^ 1]. |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1936 |
aNumber = 1 ifTrue:[^ self]. |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1937 |
aNumber isInteger ifTrue:[ |
20308 | 1938 |
^ self raisedToInteger:aNumber |
7468
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1939 |
]. |
19018 | 1940 |
aNumber isNumber ifFalse:[ |
20308 | 1941 |
^ aNumber raisedFromNumber:self. |
1942 |
]. |
|
7468
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1943 |
^ self asFloat raisedTo:aNumber |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1944 |
|
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1945 |
" |
19092 | 1946 |
2 raisedTo: 4 |
1947 |
-2 raisedTo: 4 |
|
1948 |
4 raisedTo: 1/2 |
|
1949 |
-4 raisedTo: 1/2 |
|
1950 |
8 raisedTo: 1/3 |
|
1951 |
-8 raisedTo: 1/3 |
|
20308 | 1952 |
10 raisedTo: 4 |
1953 |
10 raisedTo: -4 |
|
7468
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1954 |
" |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1955 |
! |
61c8a3053bf4
moved math functions to number
Claus Gittinger <cg@exept.de>
parents:
7456
diff
changeset
|
1956 |
|
3634 | 1957 |
real |
23046 | 1958 |
"Return the real part of a complex number. |
1959 |
For non-complex numbers, the receiver is returned." |
|
3634 | 1960 |
|
1961 |
^ self |
|
1962 |
||
23046 | 1963 |
"Modified: / 09-07-1998 / 10:17:17 / cg" |
1964 |
"Modified (comment): / 01-06-2018 / 13:09:33 / Claus Gittinger" |
|
6258 | 1965 |
! |
1966 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1967 |
sqrt |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1968 |
"return the square root of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1969 |
|
19770 | 1970 |
"/ if I am not a Float (or a less general lpReal), |
1971 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1972 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1973 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 1974 |
|f| |
1975 |
||
22115 | 1976 |
"/ do not make this a LongFloat; |
22119 | 1977 |
"/ it will return a LongFloat then, and Image rotated will fail then... |
21992 | 1978 |
(f := self asFloat) isFinite ifTrue:[ |
21988 | 1979 |
^ f sqrt. |
1980 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1981 |
]. |
19770 | 1982 |
"/ very slow fallback |
8634 | 1983 |
^ self sqrt_withAccuracy:self epsilon |
21988 | 1984 |
|
21992 | 1985 |
"Modified (format): / 11-07-2017 / 13:32:02 / cg" |
22115 | 1986 |
"Modified (format): / 26-07-2017 / 12:30:16 / mawalch" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1987 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
1988 |
|
6258 | 1989 |
timesTwoPower:anInteger |
12762 | 1990 |
"Return the receiver multiplied by 2 raised to the power of the argument. |
6258 | 1991 |
For protocol completeness wrt. Squeak and ST80." |
1992 |
||
12762 | 1993 |
anInteger >= 0 ifTrue:[ |
24157 | 1994 |
^ self * (1 bitShift:anInteger) |
12762 | 1995 |
]. |
1996 |
^ self / (1 bitShift:anInteger negated) |
|
6258 | 1997 |
|
1998 |
" |
|
24157 | 1999 |
123 timesTwoPower:0 = 123*1 -> 123 |
2000 |
123 timesTwoPower:1 = 123*2 -> 246 |
|
2001 |
123 timesTwoPower:2 = 123*4 -> 492 |
|
2002 |
123 timesTwoPower:3 = 123*8 -> 984 |
|
12762 | 2003 |
|
2004 |
(2 timesTwoPower: -150) timesTwoPower: 150 -> 2 |
|
24157 | 2005 |
(2 timesTwoPower: 150) timesTwoPower: -150 -> 2 |
2006 |
(2 timesTwoPower: 150) timesTwoPower: -149 -> 4 |
|
6258 | 2007 |
" |
24157 | 2008 |
|
2009 |
"Modified (comment): / 26-05-2019 / 03:26:09 / Claus Gittinger" |
|
3634 | 2010 |
! ! |
2011 |
||
11458 | 2012 |
!Number methodsFor:'measurement values'! |
2013 |
||
2014 |
maxValue |
|
20308 | 2015 |
"the maximum possible value taking me as a measurement with possible error; |
20421 | 2016 |
as I am exact, that's myself" |
11458 | 2017 |
|
2018 |
^ self |
|
2019 |
! |
|
2020 |
||
2021 |
minValue |
|
20308 | 2022 |
"the minimum possible value taking me as a measurement with possible error; |
20421 | 2023 |
as I am exact, that's myself" |
11458 | 2024 |
|
2025 |
^ self |
|
2026 |
! ! |
|
2027 |
||
24322 | 2028 |
|
2029 |
||
2030 |
||
2031 |
||
701 | 2032 |
!Number methodsFor:'printing & storing'! |
2033 |
||
18863 | 2034 |
displayOn:aGCOrStream |
2035 |
"return a string to display the receiver. |
|
2036 |
The output radix is usually 10, but can be changed by setting |
|
2037 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
2038 |
||
2039 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
21536 | 2040 |
"/ old ST80 means: draw-yourself on a GC. |
18863 | 2041 |
(aGCOrStream isStream) ifFalse:[ |
21536 | 2042 |
^ super displayOn:aGCOrStream |
18863 | 2043 |
]. |
2044 |
||
2045 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
|
21536 | 2046 |
self printOn:aGCOrStream |
18863 | 2047 |
] ifFalse:[ |
21536 | 2048 |
self printOn:aGCOrStream base:DefaultDisplayRadix showRadix:true. |
18863 | 2049 |
]. |
2050 |
||
2051 |
" |
|
2052 |
Integer displayRadix:16. 12345 |
|
2053 |
Integer displayRadix:2. 12345 |
|
2054 |
Integer displayRadix:10. 12345 |
|
2055 |
" |
|
21536 | 2056 |
|
2057 |
"Modified (comment): / 22-02-2017 / 16:52:16 / cg" |
|
18863 | 2058 |
! |
2059 |
||
18862 | 2060 |
printOn:aStream |
2061 |
"append a printed description of the receiver to aStream" |
|
2062 |
||
2063 |
self printOn:aStream base:10 |
|
2064 |
||
2065 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
|
2066 |
! |
|
2067 |
||
2068 |
printOn:aStream base:b |
|
2069 |
"return a string representation of the receiver in the specified |
|
2070 |
radix (without the initial XXr)" |
|
2071 |
||
2072 |
^ self printOn:aStream base:b showRadix:false |
|
2073 |
||
2074 |
" |
|
2075 |
10 printOn:Transcript base:3 |
|
2076 |
31 printOn:Transcript base:3 |
|
2077 |
-20 printOn:Transcript base:16 |
|
2078 |
-20 printOn:Transcript base:10 |
|
2079 |
3000 factorial printOn:Transcript base:10 |
|
2080 |
" |
|
2081 |
||
2082 |
"Modified: / 20.1.1998 / 18:05:02 / stefan" |
|
2083 |
"Modified: / 7.9.2001 / 13:52:17 / cg" |
|
2084 |
! |
|
2085 |
||
2086 |
printOn:aStream base:b showRadix:showRadix |
|
2087 |
"the central print method for integer. |
|
2088 |
Must be defined in concrete classes" |
|
2089 |
||
2090 |
self subclassResponsibility |
|
2091 |
! |
|
2092 |
||
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2093 |
printOn:aStream paddedWith:padCharacter to:size base:radix |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2094 |
|s| |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2095 |
|
8301 | 2096 |
radix == 10 ifTrue:[ |
20308 | 2097 |
s := self printString. |
8301 | 2098 |
] ifFalse:[ |
20308 | 2099 |
s := self printStringRadix:radix. |
8301 | 2100 |
]. |
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2101 |
s printOn: aStream leftPaddedTo:size with: padCharacter |
11098 | 2102 |
|
2103 |
" |
|
2104 |
100 printOn:Transcript paddedWith:$0 to:10 base:10. Transcript cr. |
|
2105 |
100 printOn:Transcript paddedWith:$0 to:10 base:16. Transcript cr. |
|
2106 |
100 printOn:Transcript paddedWith:(Character space) to:10 base:16. Transcript cr. |
|
2107 |
100 printOn:Transcript paddedWith:(Character space) to:10 base:2. Transcript cr. |
|
2108 |
" |
|
8301 | 2109 |
! |
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2110 |
|
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2111 |
printOn:aStream thousandsSeparator:thousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2112 |
"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
|
2113 |
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
|
2114 |
|
ac25ee31e086
fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents:
11098
diff
changeset
|
2115 |
|rest| |
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2116 |
|
11594
5440d97af571
#printStringWithThousandsSeparator fix for non-integers
Stefan Vogel <sv@exept.de>
parents:
11526
diff
changeset
|
2117 |
self >= 1000 ifTrue:[ |
20308 | 2118 |
(self // 1000) printOn:aStream thousandsSeparator:thousandsSeparator. |
2119 |
thousandsSeparator printOn:aStream. |
|
2120 |
rest := self \\ 1000. |
|
2121 |
rest < 100 ifTrue:[ |
|
2122 |
aStream nextPut:$0. |
|
2123 |
rest < 10 ifTrue:[ |
|
2124 |
aStream nextPut:$0. |
|
2125 |
]. |
|
2126 |
]. |
|
2127 |
rest printOn:aStream. |
|
2128 |
^ self. |
|
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2129 |
]. |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2130 |
self printOn:aStream. |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2131 |
|
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2132 |
" |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2133 |
swiss style: |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2134 |
1000000 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
20308 | 2135 |
12345678 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2136 |
1234567 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2137 |
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
|
2138 |
123056 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2139 |
12345 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2140 |
1234 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2141 |
123 printOn:Transcript thousandsSeparator:$'. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2142 |
|
20308 | 2143 |
(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
|
2144 |
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
|
2145 |
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
|
2146 |
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
|
2147 |
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
|
2148 |
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
|
2149 |
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
|
2150 |
|
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2151 |
us style: |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2152 |
1000000 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
20308 | 2153 |
12345678 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2154 |
1234567 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2155 |
123456 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2156 |
12345 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2157 |
1234 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2158 |
123 printOn:Transcript thousandsSeparator:$,. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2159 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2160 |
german (european ?) style |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2161 |
1000000 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
20308 | 2162 |
12345678 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2163 |
1234567 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2164 |
123456 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2165 |
12345 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2166 |
1234 printOn:Transcript thousandsSeparator:$.. Transcript cr. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2167 |
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
|
2168 |
" |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2169 |
! |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2170 |
|
11660 | 2171 |
printStringFormat:formatString |
23047 | 2172 |
"Return a printed representation of the receiver as specified by formatString, |
2173 |
which is defined by PrintfScanf." |
|
2174 |
||
2175 |
^ PrintfScanf printf:formatString argument:self. |
|
2176 |
"/ ^ self printfPrintString:formatString |
|
2177 |
||
2178 |
" |
|
2179 |
1.2345 printStringFormat:'%4.2f' -> '1.23' |
|
2180 |
123 printStringFormat:'%4d' -> ' 123' |
|
2181 |
123 printStringFormat:'%5.2f' -> '123.0' |
|
2182 |
" |
|
2183 |
||
2184 |
"Modified (comment): / 03-06-2018 / 09:15:01 / Claus Gittinger" |
|
11660 | 2185 |
! |
2186 |
||
20142 | 2187 |
printStringRadix:base |
2188 |
"return a string representation of the receiver in the specified |
|
2189 |
base; does NOT prepend XXr to the string. |
|
2190 |
See also: radixPrintStringRadix: |
|
20331 | 2191 |
printOn:base:showRadix:" |
20142 | 2192 |
|
2193 |
^ self printStringRadix:base showRadix:false |
|
11098 | 2194 |
|
2195 |
" |
|
20331 | 2196 |
10000000000000000000000000000000000000000000 printStringRadix:16 |
2197 |
-10000000000000000000000000000000000000000000 printStringRadix:16 |
|
11098 | 2198 |
" |
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2199 |
! |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
2200 |
|
18862 | 2201 |
printStringRadix:base showRadix:showRadixBoolean |
23127 | 2202 |
"return a string representation of the receiver in the specified base; |
2203 |
optionally prepend XXr to the string. |
|
18862 | 2204 |
See also: radixPrintStringRadix: |
23127 | 2205 |
printOn:base:showRadix:" |
18862 | 2206 |
|
2207 |
|s| |
|
2208 |
||
2209 |
s := WriteStream on:(String basicNew:20). |
|
2210 |
self printOn:s base:base showRadix:showRadixBoolean. |
|
2211 |
^ s contents |
|
2212 |
||
20142 | 2213 |
" |
2214 |
10000000000000000000000000000000000000000000 printStringRadix:16 showRadix:false |
|
2215 |
10000000000000000000000000000000000000000000 printStringRadix:16 showRadix:true |
|
2216 |
" |
|
2217 |
||
18862 | 2218 |
"Created: / 23-09-2011 / 13:59:19 / cg" |
23127 | 2219 |
"Modified (comment): / 22-06-2018 / 09:56:17 / Claus Gittinger" |
18862 | 2220 |
! |
2221 |
||
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2222 |
printStringWithThousandsSeparator |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2223 |
"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
|
2224 |
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
|
2225 |
|
20866 | 2226 |
^ self printStringWithThousandsSeparator:(UserPreferences current thousandsSeparatorCharacter). |
11096
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2227 |
|
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2228 |
" |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2229 |
1000000 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2230 |
12345678 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2231 |
1234567 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2232 |
123456 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2233 |
12345 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2234 |
1234 printStringWithThousandsSeparator |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2235 |
123 printStringWithThousandsSeparator |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2236 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2237 |
1000000 asFixedPoint printStringWithThousandsSeparator |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2238 |
12345678 asFixedPoint printStringWithThousandsSeparator |
20308 | 2239 |
1234567 asFixedPoint printStringWithThousandsSeparator |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2240 |
123456 asFixedPoint printStringWithThousandsSeparator |
20308 | 2241 |
12345 asFixedPoint printStringWithThousandsSeparator |
2242 |
1234 asFixedPoint printStringWithThousandsSeparator |
|
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2243 |
123 asFixedPoint printStringWithThousandsSeparator |
20308 | 2244 |
((9999999//10000) asFixedPoint:9) printStringWithThousandsSeparator |
20866 | 2245 |
((99999999//10000) asFixedPoint:9) printStringWithThousandsSeparator |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2246 |
" |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2247 |
! |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2248 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2249 |
printStringWithThousandsSeparator:thousandsSeparator |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2250 |
"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
|
2251 |
Notice: |
20308 | 2252 |
americans use comma |
2253 |
germans (europeans ?) use a dot |
|
2254 |
swiss people (business people ?) use a single quote |
|
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2255 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2256 |
Caveat: Should use the separator from the locale here" |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2257 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2258 |
^ String streamContents:[:s | self printOn:s thousandsSeparator:thousandsSeparator]. |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2259 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2260 |
" |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2261 |
Transcript showCR:(1000000 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2262 |
Transcript showCR:(12345678 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2263 |
Transcript showCR:(1234567 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2264 |
Transcript showCR:(123456 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2265 |
Transcript showCR:(12345 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2266 |
Transcript showCR:(1234 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2267 |
Transcript showCR:(123 printStringWithThousandsSeparator:$'). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2268 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2269 |
Transcript showCR:(1000000 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2270 |
Transcript showCR:(12345678 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2271 |
Transcript showCR:(1234567 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2272 |
Transcript showCR:(123456 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2273 |
Transcript showCR:(12345 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2274 |
Transcript showCR:(1234 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2275 |
Transcript showCR:(123 printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2276 |
|
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2277 |
Transcript showCR:((1000000 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2278 |
Transcript showCR:((12345678 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
20308 | 2279 |
Transcript showCR:((1234567 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2280 |
Transcript showCR:((123456 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
20308 | 2281 |
Transcript showCR:((12345 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
2282 |
Transcript showCR:((1234 asFixedPoint:2) printStringWithThousandsSeparator:$,). |
|
11097
5e4961639e41
printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents:
11096
diff
changeset
|
2283 |
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
|
2284 |
" |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2285 |
! |
19d6c993e67f
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents:
11008
diff
changeset
|
2286 |
|
21910 | 2287 |
printfPrintString:formatString |
2288 |
"Return a printed representation of the receiver as specified by formatString, |
|
2289 |
which is defined by printf." |
|
2290 |
||
2291 |
^ PrintfScanf printf:formatString argument:self |
|
2292 |
||
2293 |
" |
|
2294 |
2.0 asQDouble printfPrintString:'%10f' |
|
2295 |
2.0 asQDouble printfPrintString:'%10.8f' |
|
2296 |
2.0 printfPrintString:'%10.8f' |
|
2297 |
" |
|
2298 |
||
2299 |
"Created: / 22-06-2017 / 13:55:22 / cg" |
|
2300 |
! |
|
2301 |
||
18862 | 2302 |
radixPrintStringRadix:radix |
2303 |
"return a string representation of the receiver in the specified |
|
2304 |
base; prepend XXr to the string" |
|
2305 |
||
2306 |
^ self printStringRadix:radix showRadix:true |
|
2307 |
||
2308 |
" |
|
2309 |
31 radixPrintStringRadix:2 |
|
2310 |
31 radixPrintStringRadix:3 |
|
2311 |
31 radixPrintStringRadix:10 |
|
2312 |
31 radixPrintStringRadix:16 |
|
2313 |
31 radixPrintStringRadix:36 |
|
2314 |
" |
|
2315 |
||
2316 |
"Created: / 19-01-1998 / 17:38:00 / stefan" |
|
2317 |
"Modified: / 20-01-1998 / 14:11:03 / stefan" |
|
2318 |
"Modified: / 23-09-2011 / 14:00:02 / cg" |
|
2319 |
! |
|
2320 |
||
701 | 2321 |
storeOn:aStream |
11460 | 2322 |
"append a string for storing the receiver onto the argument, aStream |
2323 |
- since numbers are literals,they store as they print." |
|
701 | 2324 |
|
2325 |
^ self printOn:aStream |
|
2326 |
! |
|
2327 |
||
2328 |
storeString |
|
20308 | 2329 |
"return a string for storing |
701 | 2330 |
- since numbers are literals, they store as they print." |
2331 |
||
2332 |
^ self printString |
|
2333 |
! ! |
|
2334 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2335 |
!Number methodsFor:'taylor series'! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2336 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2337 |
arcSin_withAccuracy:epsilon |
17190 | 2338 |
"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
|
2339 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2340 |
"/ uses taylor series: |
20308 | 2341 |
"/ 1*x^3 1*3 * x^5 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2342 |
"/ arcSin = x + ----- + ---------- + ... |
20308 | 2343 |
"/ 2* 3 2*4 * 5 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2344 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2345 |
|x2 num numf den denf approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2346 |
|
20308 | 2347 |
((self < -1) or:[self > 1]) ifTrue:[ |
2348 |
^ self class |
|
2349 |
raise:#domainErrorSignal |
|
2350 |
receiver:self |
|
2351 |
selector:#arcSin |
|
2352 |
arguments:#() |
|
2353 |
errorString:'bad receiver in arcSin' |
|
8844 | 2354 |
]. |
2355 |
||
2356 |
x2 := self * self. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2357 |
num := x2 * self. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2358 |
approx := self + (num / 6). |
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 |
numf := 3. |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2361 |
denf := 4. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2362 |
den := 2. |
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 |
num := (num * x2) * numf. numf := numf + 2. |
2366 |
den := den * denf. denf := denf + 2. |
|
2367 |
||
2368 |
delta := num / (den * numf). |
|
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 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2374 |
0.5 arcSin 0.523599 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2375 |
0.5q arcSin 0.523598776 |
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 |
0.5q arcSin_withAccuracy:1 0.520833333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2378 |
0.5q arcSin_withAccuracy:0.1 0.520833333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2379 |
0.5q arcSin_withAccuracy:0.01 0.523177083 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2380 |
0.5q arcSin_withAccuracy:0.001 0.523525856 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2381 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2382 |
0.5q arcSin_withAccuracy:1e-20 0.523598776 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2383 |
|
20308 | 2384 |
0.5 asLargeFloat arcSin_withAccuracy:1e-30 -- not yet |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2385 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2386 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2387 |
0.1 arcSin 0.100167 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2388 |
0.1q arcSin 0.100167421 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2389 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2390 |
0.1q arcSin_withAccuracy:1 0.100166667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2391 |
0.1q arcSin_withAccuracy:0.1 0.100166667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2392 |
0.1q arcSin_withAccuracy:0.01 0.100166667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2393 |
0.1q arcSin_withAccuracy:0.001 0.100166667 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2394 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2395 |
0.1q arcSin_withAccuracy:1e-20 0.100167421 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2396 |
|
20308 | 2397 |
0.1 asLargeFloat arcSin_withAccuracy:1e-30 -- not yet |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2398 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2399 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2400 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2401 |
arcTan_withAccuracy:epsilon |
17190 | 2402 |
"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
|
2403 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2404 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2405 |
"/ x^3 x^5 x^7 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2406 |
"/ arcTan(x) = x - --- + --- - --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2407 |
"/ 3 5 7 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2408 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2409 |
|x2 num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2410 |
|
8844 | 2411 |
x2 := self * self. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2412 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2413 |
num := (x2 * self) negated. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2414 |
den := 3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2415 |
approx := self + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2416 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2417 |
[ |
20308 | 2418 |
den := den + 2. |
2419 |
num := (num * x2) negated. |
|
2420 |
||
2421 |
delta := num / den. |
|
2422 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2423 |
] doUntil:[delta abs <= epsilon]. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2424 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2425 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2426 |
" |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2427 |
1.0 arcTan 0.785398 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2428 |
1q arcTan 0.785398163 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2429 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2430 |
1q arcTan_withAccuracy:1 0.666666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2431 |
1q arcTan_withAccuracy:0.1 0.744011544 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2432 |
1q arcTan_withAccuracy:0.01 0.790299653 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2433 |
1q arcTan_withAccuracy:0.001 0.785897165 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2434 |
|
20308 | 2435 |
1q arcTan_withAccuracy:1e-8 0.785398168 |
2436 |
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
|
2437 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2438 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2439 |
0.5 arcTan 0.463648 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2440 |
0.5q arcTan 0.463647609 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2441 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2442 |
0.5q arcTan_withAccuracy:1 0.458333333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2443 |
0.5q arcTan_withAccuracy:0.1 0.458333333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2444 |
0.5q arcTan_withAccuracy:0.01 0.464583333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2445 |
0.5q arcTan_withAccuracy:0.001 0.463684276 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2446 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2447 |
0.5q arcTan_withAccuracy:1e-20 0.463647609 |
20308 | 2448 |
0.5 asLargeFloat arcTan_withAccuracy:1e-30 -- not yet |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2449 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2450 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2451 |
|
20308 | 2452 |
cbrt_withAccuracy:epsilon |
17190 | 2453 |
"compute cubic root of the receiver using a newton approx." |
2454 |
||
22097 | 2455 |
"Use Newton's method (not taylor): |
2456 |
||
2457 |
2*x_n + (a / x_n^2) |
|
2458 |
x_n+1 = --------------- |
|
2459 |
3 |
|
2460 |
||
2461 |
cbrt(a) = x_n |
|
17190 | 2462 |
" |
20308 | 2463 |
|
17190 | 2464 |
|approx| |
2465 |
||
2466 |
self = 0 ifTrue:[ |
|
22097 | 2467 |
^ self |
17190 | 2468 |
]. |
2469 |
||
2470 |
approx := 1. |
|
2471 |
[ |
|
22097 | 2472 |
|lastApprox| |
2473 |
||
2474 |
lastApprox := approx. |
|
2475 |
approx := ((approx * 2) + (self / approx / approx)) / 3. |
|
2476 |
(approx - lastApprox) abs > epsilon |
|
17190 | 2477 |
] whileTrue. |
2478 |
^ approx |
|
2479 |
||
2480 |
" |
|
22097 | 2481 |
8q cbrt 2.0 |
2482 |
8q cbrt_withAccuracy:0.01 2.000004911675504018 |
|
2483 |
8q cbrt_withAccuracy:0.0001 2.000000000012062239 |
|
2484 |
8q cbrt_withAccuracy:0.0000001 2.0 |
|
2485 |
8q cbrt_withAccuracy:0.0000000001 2.0 |
|
2486 |
8q cbrt_withAccuracy:0.000000000001 2.0 |
|
2487 |
8q cbrt_withAccuracy:LongFloat epsilon 2.0 |
|
2488 |
8q asQDouble cbrt_withAccuracy:QDouble epsilon 2.0 |
|
2489 |
||
2490 |
27q cbrt_withAccuracy:0.01 3.000000541064176501 |
|
2491 |
27q cbrt_withAccuracy:LongFloat epsilon 3.0 |
|
2492 |
-27q cbrt_withAccuracy:LongFloat epsilon -3.0 |
|
17190 | 2493 |
|
2494 |
MessageTally spyOn:[ |arg| |
|
22097 | 2495 |
arg := 2 asLongFloat. |
2496 |
1000000 timesRepeat:[ |
|
2497 |
arg cbrt_withAccuracy:0.000000000001 |
|
2498 |
] |
|
17190 | 2499 |
] |
2500 |
Time millisecondsToRun:[ |arg| |
|
22097 | 2501 |
arg := 2 asLongFloat. |
2502 |
1000000 timesRepeat:[ |
|
2503 |
arg cbrt_withAccuracy:0.000000000001 |
|
2504 |
] |
|
17190 | 2505 |
] |
2506 |
" |
|
22097 | 2507 |
|
2508 |
"Modified (comment): / 25-07-2017 / 16:09:22 / cg" |
|
17190 | 2509 |
! |
2510 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2511 |
cos_withAccuracy:epsilon |
17190 | 2512 |
"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
|
2513 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2514 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2515 |
"/ x^2 x^4 x^6 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2516 |
"/ cos(x) = 1 - --- + --- - --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2517 |
"/ 2!! 4!! 6!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2518 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2519 |
|x2 facN num den approx lastApprox| |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2520 |
|
8844 | 2521 |
x2 := self * 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 |
num := x2 negated. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2524 |
den := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2525 |
facN := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2526 |
approx := 1 + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2527 |
lastApprox := 1. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2528 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2529 |
[ (lastApprox - approx) abs > epsilon ] whileTrue:[ |
20308 | 2530 |
facN := facN + 2. |
2531 |
den := den * (facN - 1) * facN. |
|
2532 |
num := (num * x2) negated. |
|
2533 |
lastApprox := approx. |
|
2534 |
approx := approx + (num / den). |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2535 |
]. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2536 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2537 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2538 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2539 |
1.0 cos 0.540302 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2540 |
1.0 asLongFloat cos_withAccuracy:1 0.5 |
20308 | 2541 |
1.0 asLongFloat cos_withAccuracy:0.1 0.541666667 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2542 |
1.0 asLongFloat cos_withAccuracy:0.01 0.540277778 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2543 |
1.0 asLongFloat cos_withAccuracy:0.001 0.540302579 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2544 |
|
20308 | 2545 |
1.0 asLongFloat cos_withAccuracy:1e-40 0.540302306 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2546 |
" |
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 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2549 |
cosh_withAccuracy:epsilon |
17190 | 2550 |
"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
|
2551 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2552 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2553 |
"/ x^2 x^4 x^6 |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2554 |
"/ cosh = x + --- + --- + --- ... |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2555 |
"/ 2!! 4!! 6!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2556 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2557 |
|x2 facN num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2558 |
|
8844 | 2559 |
x2 := self * self. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2560 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2561 |
num := x2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2562 |
den := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2563 |
facN := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2564 |
approx := self + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2565 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2566 |
[ |
22143 | 2567 |
facN := facN + 2. |
2568 |
den := den * (facN - 1) * facN. |
|
2569 |
num := num * x2. |
|
2570 |
||
2571 |
delta := num / den. |
|
2572 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2573 |
] doUntil:[delta <= epsilon]. |
22143 | 2574 |
^ approx + self. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2575 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2576 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2577 |
1.0 cosh 1.54308 |
20308 | 2578 |
1.0q cosh_withAccuracy:1 1.5 |
2579 |
1.0q cosh_withAccuracy:0.1 1.54308 |
|
2580 |
1.0q cosh_withAccuracy:0.01 1.54308 |
|
2581 |
1.0q cosh_withAccuracy:0.001 1.54308 |
|
2582 |
||
2583 |
1.0q cosh_withAccuracy:1e-40 -> 1.543080 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2584 |
" |
22143 | 2585 |
|
2586 |
"Modified: / 01-08-2017 / 14:54:40 / stefan" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2587 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2588 |
|
8634 | 2589 |
epsilon |
20308 | 2590 |
"return the maximum relative spacing of instances of mySelf |
8634 | 2591 |
(i.e. the value-delta of the least significant bit)" |
2592 |
||
2593 |
^ self class epsilon |
|
2594 |
! |
|
2595 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2596 |
exp_withAccuracy:epsilon |
21888 | 2597 |
"compute e^x of the receiver using a taylor series approximation. |
2598 |
This method is only invoked for limitedPrecisionReal classes, which do not compute |
|
2599 |
exp themself (i.e. QDouble)" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2600 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2601 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2602 |
"/ x x^2 x^3 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2603 |
"/ e^x = 1 + --- + --- + --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2604 |
"/ 1!! 2!! 3!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2605 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2606 |
|x2 facN num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2607 |
|
21982 | 2608 |
x2 := self * self. "/ self squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2609 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2610 |
num := x2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2611 |
den := 2. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2612 |
facN := 2. |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2613 |
approx := self + 1 + (num / den). |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2614 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2615 |
[ |
21888 | 2616 |
facN := facN + 1. |
2617 |
den := den * facN. |
|
2618 |
num := num * self. |
|
2619 |
||
2620 |
delta := num / den. |
|
22302 | 2621 |
"/ delta mantissa == 0 ifTrue:[self halt. num / den]. |
2622 |
"/ Transcript showCR:delta. |
|
2623 |
delta isNaN ifTrue:[self halt:'nan when dividing for delta'. num / den]. |
|
2624 |
delta = 0 ifTrue:[self halt:'zero delta']. |
|
21888 | 2625 |
approx := approx + delta. |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2626 |
] doUntil:[delta abs <= epsilon]. |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2627 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2628 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2629 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2630 |
" |
22302 | 2631 |
wolfram: |
2632 |
7.389056098930650227230427460575007813180315570551847324087 |
|
2633 |
||
2634 |
(2 asLargeFloat exp_withAccuracy:1e-100) printfPrintString:'%50.48f' |
|
2635 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2636 |
1.0 exp 2.71828 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2637 |
1q exp 2.71828183 |
20308 | 2638 |
2q exp 7.3890561 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2639 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2640 |
1q exp_withAccuracy:1 2.66666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2641 |
1q exp_withAccuracy:0.1 2.70833333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2642 |
1q exp_withAccuracy:0.01 2.71666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2643 |
1q exp_withAccuracy:0.001 2.71825397 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2644 |
|
21912 | 2645 |
-1 exp 0.367879441171442 |
2646 |
-1q exp_withAccuracy:(1e-60) 0.3678794411714423216 |
|
2647 |
||
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2648 |
2q exp_withAccuracy:LongFloat epsilon 7.3890561 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2649 |
|
21982 | 2650 |
42 asQDouble exp_withAccuracy:QDouble epsilon |
2651 |
42 asQDouble exp_withAccuracy:LongFloat epsilon |
|
2652 |
42 asQDouble exp_withAccuracy:Float epsilon |
|
2653 |
||
2654 |
2 asQDouble exp_withAccuracy:QDouble epsilon 7.38905609893065022723 |
|
2655 |
2 asQDouble exp_withAccuracy:LongFloat epsilon 7.38905609893065022723 |
|
2656 |
2 asQDouble exp_withAccuracy:Float epsilon 7.38905609893065022489 |
|
2657 |
||
2658 |
1 asQDouble exp_withAccuracy:QDouble epsilon 2.71828182845904523536 |
|
2659 |
1 asQDouble exp_withAccuracy:LongFloat epsilon 2.71828182845904523536 |
|
2660 |
1 asQDouble exp_withAccuracy:Float epsilon 2.71828182845904522671 |
|
2661 |
||
20308 | 2662 |
1.0 asLongFloat exp_withAccuracy:1e-40 2.71828183 |
2663 |
||
2664 |
5 exp_withAccuracy:1e-40 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2665 |
(1 exp_withAccuracy:1e-100) asFixedPoint:100 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2666 |
" |
21888 | 2667 |
|
22302 | 2668 |
"Modified: / 10-10-2017 / 16:04:08 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2669 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2670 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2671 |
ln_withAccuracy:epsilon |
17190 | 2672 |
"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
|
2673 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2674 |
"uses taylor series: |
21982 | 2675 |
u^2 u^3 |
2676 |
ln = u - --- + --- ... |
|
2677 |
2 3 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2678 |
where: |
21982 | 2679 |
u = x - 1 and: x < 1 |
2680 |
||
2681 |
Now we use modified taylor, which converges a little faster: |
|
2682 |
||
2683 |
1+y 1 1 1 |
|
2684 |
ln(x) = ln --- = 2y ( - + - y^2 + - y^4 + ....) |
|
2685 |
1-y 1 3 5 |
|
2686 |
||
2687 |
where y = (x-1) / (x+1) and x > 0 |
|
2688 |
||
2689 |
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
|
2690 |
" |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2691 |
|
21982 | 2692 |
|denominator approx y y2 exp delta count| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2693 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2694 |
self <= 0 ifTrue:[ |
21982 | 2695 |
^ self class |
2696 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
|
2697 |
receiver:self |
|
2698 |
selector:#ln |
|
2699 |
arguments:#() |
|
2700 |
errorString:'bad receiver in ln (not strictly positive)' |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2701 |
]. |
20308 | 2702 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2703 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2704 |
y := (self - 1)/(self + 1). |
21982 | 2705 |
exp := y2 := y squared. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2706 |
|
21988 | 2707 |
approx := 1.0. |
2708 |
denominator := 3.0. |
|
21982 | 2709 |
count := 1. |
2710 |
||
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2711 |
[ |
21982 | 2712 |
delta := exp / denominator. |
2713 |
approx := approx + delta. |
|
2714 |
exp := exp * y2. |
|
2715 |
denominator := denominator + 2. |
|
2716 |
||
2717 |
count := count + 1. |
|
2718 |
(count \\ 100) == 0 ifTrue:[ |
|
2719 |
Logger warning:'slow ln-taylor converging...'. |
|
2720 |
]. |
|
2721 |
] doUntil:[delta <= epsilon or:[count > 10000]]. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2722 |
|
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2723 |
^ y * 2 * approx. |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2724 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2725 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2726 |
" |
21982 | 2727 |
2.0 ln 0.693147180559945 |
2728 |
2.0q ln 0.6931471805599453094 |
|
2729 |
||
2730 |
2.0q ln_withAccuracy:1 0.691358025 |
|
2731 |
2.0q ln_withAccuracy:0.1 0.691358025 |
|
2732 |
2.0q ln_withAccuracy:0.01 0.693004115 |
|
2733 |
2.0q ln_withAccuracy:0.0000001 0.69314718 |
|
2734 |
2.0q ln_withAccuracy:1e-10 0.6931471805589163927 |
|
2735 |
2.0q ln_withAccuracy:1e-20 0.6931471805599453094 |
|
2736 |
2.0q ln_withAccuracy:1e-40 0.6931471805599453094 |
|
2737 |
2.0q ln_withAccuracy:2.0q class epsilon 0.6931471805599453094 |
|
2738 |
||
2739 |
(2 ln_withAccuracy:1e-40) -> a fraction |
|
20308 | 2740 |
0 ln_withAccuracy:1e-40 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2741 |
|
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2742 |
(2 ln_withAccuracy:1e-100) asFixedPoint:100 |
21982 | 2743 |
(2 asFixedPoint:200) ln_withAccuracy:(1/(10 raisedTo:200)) |
2744 |
0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335 |
|
2745 |
||
2746 |
(2 asFixedPoint:400) ln_withAccuracy:(1/(10 raisedTo:400)) |
|
2747 |
0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335 01153644979552391204751726815749320651555247341395258829504530070953263666426541042391578149520437404303855008019441706416715186447128399681717845469570262716310645461502572074024816377733896385506953 |
|
2748 |
||
2749 |
(2.0 asQDouble ln_withAccuracy:QDouble epsilon) printfPrintString:'%60.58f' |
|
2750 |
0.69314718055994 52709398341558750792990469129794959648865081' |
|
2751 |
||
2752 |
Wolfram says: |
|
2753 |
0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335 01153644979552391204751726... |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2754 |
" |
21982 | 2755 |
|
21988 | 2756 |
"Modified: / 05-07-2017 / 17:16:24 / cg" |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2757 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2758 |
|
21915 | 2759 |
nthRoot:n withAccuracy:epsilon |
2760 |
"compute nth root of the receiver using a newton approx." |
|
2761 |
||
2762 |
" |
|
22104 | 2763 |
Use Newton's method: |
21915 | 2764 |
|
2765 |
(n-1)*x_n^n + (a / x_n^(n-1)) |
|
2766 |
x_n+1 = ----------------------------- |
|
2767 |
n |
|
2768 |
||
2769 |
rt(n, a) = x_n |
|
2770 |
" |
|
2771 |
||
22287 | 2772 |
|approx delta absDelta prevDelta| |
21915 | 2773 |
|
2774 |
self = 0 ifTrue:[ |
|
2775 |
^ self |
|
2776 |
]. |
|
2777 |
||
22104 | 2778 |
"/ the demanded epsilon may NEVER be smaller than the real representation's epsilon |
2779 |
self assert:(epsilon >= (self class epsilon)). |
|
2780 |
||
2781 |
approx := (self / 2). |
|
22287 | 2782 |
delta := ((self / (approx raisedToInteger:(n-1))) - approx) / n. |
2783 |
absDelta := delta abs. |
|
2784 |
approx := approx + delta. |
|
2785 |
||
21915 | 2786 |
[ |
22287 | 2787 |
prevDelta := absDelta. |
2788 |
||
22104 | 2789 |
delta := ((self / (approx raisedToInteger:(n-1))) - approx) / n. |
22287 | 2790 |
absDelta := delta abs. |
22104 | 2791 |
approx := approx + delta. |
22098 | 2792 |
|
22287 | 2793 |
(absDelta < prevDelta) ifFalse:[ |
2794 |
DomainError raiseRequestErrorString:'no convergence in Newton approx.' |
|
2795 |
]. |
|
2796 |
||
2797 |
(absDelta > epsilon) |
|
21915 | 2798 |
] whileTrue. |
2799 |
^ approx |
|
2800 |
||
22104 | 2801 |
" |
2802 |
8q nthRoot:3 withAccuracy:0.01 2.00000002488636242 |
|
2803 |
8q nthRoot:3 withAccuracy:0.0001 2.00000000000000031 |
|
2804 |
8q nthRoot:3 withAccuracy:0.0000001 2.00000000000000031 |
|
21915 | 2805 |
8q nthRoot:3 withAccuracy:0.0000000001 2.0 |
2806 |
8q nthRoot:3 withAccuracy:0.000000000001 2.0 |
|
2807 |
8q nthRoot:3 withAccuracy:LongFloat epsilon 2.0 |
|
2808 |
||
22104 | 2809 |
27q nthRoot:3 withAccuracy:0.01 3.000000081210202031 |
2810 |
27q nthRoot:3 withAccuracy:LongFloat epsilon 3.0 |
|
2811 |
-27q nthRoot:3 withAccuracy:LongFloat epsilon -3.0 |
|
2812 |
||
2813 |
10000q nthRoot:5 withAccuracy:1e-18 -> 6.309573444801932495 |
|
2814 |
||
2815 |
(10000 asQDouble nthRoot:5) printfPrintString:'%70.68f' |
|
2816 |
6.30957344480193249434360136622343864672945257188228724527729528834741' |
|
2817 |
||
2818 |
actual result (Mathematica): |
|
2819 |
6.309573444801932494343601366223438646729452571882287245277... |
|
21915 | 2820 |
" |
2821 |
||
2822 |
"Created: / 22-06-2017 / 15:51:55 / cg" |
|
22287 | 2823 |
"Modified: / 22-09-2017 / 10:14:59 / cg" |
21915 | 2824 |
! |
2825 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2826 |
sin_withAccuracy:epsilon |
17190 | 2827 |
"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
|
2828 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2829 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2830 |
"/ x^3 x^5 x^7 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2831 |
"/ sin(x) = x - --- + --- - --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2832 |
"/ 3!! 5!! 7!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2833 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2834 |
|x2 facN num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2835 |
|
8844 | 2836 |
x2 := self * self. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2837 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2838 |
num := (x2 * self) negated. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2839 |
den := 2*3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2840 |
facN := 3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2841 |
approx := self + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2842 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2843 |
[ |
20308 | 2844 |
facN := facN + 2. |
2845 |
den := den * (facN - 1) * facN. |
|
2846 |
num := (num * x2) negated. |
|
2847 |
||
2848 |
delta := num / den. |
|
2849 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2850 |
] doUntil:[delta abs <= epsilon]. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2851 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2852 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2853 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2854 |
1.0 sin 0.841471 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2855 |
1.0q sin 0.841470985 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2856 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2857 |
1.0q sin_withAccuracy:1 0.833333333 |
20308 | 2858 |
1.0q sin_withAccuracy:0.1 0.841666667 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2859 |
1.0q sin_withAccuracy:0.01 0.841666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2860 |
1.0q sin_withAccuracy:0.001 0.841468254 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2861 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2862 |
1.0q sin_withAccuracy:1e-40 0.841470985 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2863 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2864 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2865 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2866 |
sinh_withAccuracy:epsilon |
17190 | 2867 |
"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
|
2868 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2869 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2870 |
"/ x^3 x^5 x^7 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2871 |
"/ sinh(x) = x + --- + --- + --- ... |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2872 |
"/ 3!! 5!! 7!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2873 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2874 |
|x2 facN num den approx delta| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2875 |
|
8844 | 2876 |
x2 := self * self. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2877 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2878 |
num := x2 * self. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2879 |
den := 2*3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2880 |
facN := 3. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2881 |
approx := self + (num / den). |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2882 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2883 |
[ |
20308 | 2884 |
facN := facN + 2. |
2885 |
den := den * (facN - 1) * facN. |
|
2886 |
num := num * x2. |
|
2887 |
||
2888 |
delta := num / den. |
|
2889 |
approx := approx + delta. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2890 |
] doUntil:[delta abs <= epsilon]. |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2891 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2892 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2893 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2894 |
1.0 sinh 1.1752 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2895 |
1q sinh 1.17520119 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2896 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2897 |
1q sinh_withAccuracy:1 1.16666667 |
20308 | 2898 |
1q sinh_withAccuracy:0.1 1.175 |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2899 |
1q sinh_withAccuracy:0.01 1.175 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
2900 |
1q sinh_withAccuracy:0.001 1.17519841 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2901 |
|
20308 | 2902 |
1q sinh_withAccuracy:1e-40 1.17520119 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2903 |
" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2904 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
2905 |
|
20308 | 2906 |
sqrt_withAccuracy:epsilon |
21910 | 2907 |
"compute square root of the receiver using newton-raphson/heron algorithm" |
22097 | 2908 |
"Use the Heron algorithm (not Taylor)" |
21910 | 2909 |
|
22096 | 2910 |
|guess| |
2911 |
||
2912 |
false "self isInteger" ifTrue:[ |
|
2913 |
guess := 1 bitShift:(self highBit // 2) |
|
2914 |
] ifFalse:[ |
|
22106 | 2915 |
guess := self / 2.0. |
22096 | 2916 |
]. |
2917 |
^ self sqrt_withAccuracy:epsilon fromInitialGuess:guess |
|
21910 | 2918 |
|
2919 |
" |
|
2920 |
2 sqrt 1.4142135623731 - computed by CPU/FPU |
|
22096 | 2921 |
200000000 sqrt |
21910 | 2922 |
|
2923 |
2q sqrt 1.414213562373095049 - computed by CPU/FPU |
|
2924 |
||
2925 |
2q sqrt_withAccuracy:0.01 1.414215686274509804 - computed by Smalltalk |
|
2926 |
2q sqrt_withAccuracy:0.0001 1.414213562374689911 |
|
2927 |
2q sqrt_withAccuracy:0.0000001 1.414213562373095049 |
|
2928 |
2q sqrt_withAccuracy:0.0000000001 1.414213562373095049 |
|
2929 |
2q sqrt_withAccuracy:0.000000000001 1.414213562373095049 |
|
2930 |
||
2931 |
2q sqrt_withAccuracy:LongFloat epsilon 1.414213562373095049 |
|
2932 |
||
2933 |
(4 sqrt_withAccuracy:Integer epsilon) asFloat |
|
2934 |
||
2935 |
MessageTally spyOn:[ |arg| |
|
2936 |
arg := 2 asLongFloat. |
|
2937 |
1000000 timesRepeat:[ |
|
2938 |
arg sqrt_withAccuracy:0.000000000001 |
|
2939 |
] |
|
2940 |
] |
|
2941 |
Time millisecondsToRun:[ |arg| |
|
2942 |
arg := 2 asLongFloat. |
|
2943 |
1000000 timesRepeat:[ |
|
2944 |
arg sqrt_withAccuracy:0.000000000001 |
|
2945 |
] |
|
2946 |
] |
|
2947 |
" |
|
2948 |
||
22106 | 2949 |
"Modified: / 25-07-2017 / 17:44:46 / cg" |
21910 | 2950 |
! |
2951 |
||
2952 |
sqrt_withAccuracy:epsilon fromInitialGuess:guess |
|
2953 |
"compute square root of the receiver using newton-raphson/heron algorithm" |
|
22097 | 2954 |
|
2955 |
"Use the Heron algorithm (not Taylor): |
|
21910 | 2956 |
|
2957 |
x_n + (a / x_n) |
|
2958 |
x_n+1 = --------------- |
|
2959 |
2 |
|
2960 |
||
2961 |
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
|
2962 |
" |
20308 | 2963 |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
2964 |
|approx| |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
2965 |
|
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
2966 |
self <= 0 ifTrue:[ |
21910 | 2967 |
self = 0 ifTrue:[ |
2968 |
^ self |
|
2969 |
]. |
|
2970 |
^ self class |
|
2971 |
raise:#imaginaryResultSignal |
|
2972 |
receiver:self |
|
2973 |
selector:#sqrt |
|
2974 |
arguments:#() |
|
2975 |
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
|
2976 |
]. |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
2977 |
|
21910 | 2978 |
approx := guess. |
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
2979 |
[ |
21910 | 2980 |
|lastApprox| |
2981 |
||
2982 |
lastApprox := approx. |
|
2983 |
approx := ((self / approx) + approx) / 2. |
|
2984 |
(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
|
2985 |
] whileTrue. |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
2986 |
^ approx |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
2987 |
|
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
2988 |
" |
17190 | 2989 |
2 sqrt 1.4142135623731 |
2990 |
2q sqrt 1.414213562373095049 |
|
2991 |
2q sqrt_withAccuracy:0.01 1.414215686274509804 |
|
2992 |
2q sqrt_withAccuracy:0.0001 1.414213562374689911 |
|
2993 |
2q sqrt_withAccuracy:0.0000001 1.414213562373095049 |
|
2994 |
2q sqrt_withAccuracy:0.0000000001 1.414213562373095049 |
|
2995 |
2q sqrt_withAccuracy:0.000000000001 1.414213562373095049 |
|
2996 |
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
|
2997 |
|
21916 | 2998 |
(2 asQDouble sqrt_withAccuracy:LongFloat epsilon) printfPrintString:'%70.68f' |
2999 |
1.41421356237309504880168872420969807856967187537723400156101313309966 |
|
3000 |
||
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3001 |
(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
|
3002 |
|
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3003 |
MessageTally spyOn:[ |arg| |
21910 | 3004 |
arg := 2 asLongFloat. |
3005 |
1000000 timesRepeat:[ |
|
3006 |
arg sqrt_withAccuracy:0.000000000001 |
|
3007 |
] |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3008 |
] |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3009 |
Time millisecondsToRun:[ |arg| |
21910 | 3010 |
arg := 2 asLongFloat. |
3011 |
1000000 timesRepeat:[ |
|
3012 |
arg sqrt_withAccuracy:0.000000000001 |
|
3013 |
] |
|
7729
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3014 |
] |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3015 |
" |
21910 | 3016 |
|
3017 |
"Created: / 22-06-2017 / 13:59:48 / cg" |
|
22097 | 3018 |
"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
|
3019 |
! |
bbd20db09d1b
Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents:
7468
diff
changeset
|
3020 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3021 |
tan_withAccuracy:epsilon |
17190 | 3022 |
"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
|
3023 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3024 |
"/ uses taylor series: |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3025 |
"/ 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
|
3026 |
"/ tan = x + 1*--- + 2*--- + 17*--- + 62*----... ----------------------------------... |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3027 |
"/ 3 15 315 2835 (2n)!! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3028 |
"/ where Bi is the ith bernoulli number. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3029 |
|
22543 | 3030 |
|factors idx x2 num t approx lastApprox delta nFactors| |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3031 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3032 |
"/ (1 to:20) collect:[:n| |num den| |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3033 |
"/ 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
|
3034 |
"/ den := (2*n) factorial. |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3035 |
"/ num / den |
20308 | 3036 |
"/ ] |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3037 |
factors := #( |
22543 | 3038 |
(1 3) |
3039 |
(2 15) |
|
3040 |
(17 315) |
|
3041 |
(62 2835) |
|
3042 |
(1382 155925) |
|
3043 |
(21844 6081075) |
|
3044 |
(929569 638512875) |
|
3045 |
(6404582 10854718875) |
|
3046 |
(443861162 1856156927625) |
|
3047 |
(18888466084 194896477400625) |
|
3048 |
(113927491862 2900518163668125) |
|
3049 |
(58870668456604 3698160658676859375) |
|
3050 |
(8374643517010684 1298054391195577640625) |
|
3051 |
(689005380505609448 263505041412702261046875) |
|
3052 |
(129848163681107301953 122529844256906551386796875) |
|
3053 |
(1736640792209901647222 4043484860477916195764296875) |
|
3054 |
(418781231495293038913922 2405873491984360136479756640625) |
|
3055 |
(56518638202982204522669764 801155872830791925447758961328125) |
|
3056 |
(32207686319158956594455462 1126482925555250126673224649609375)). |
|
3057 |
||
3058 |
nFactors := factors size. |
|
8844 | 3059 |
x2 := self * self. |
8203 | 3060 |
num := x2 * self. "/ = x^3 |
3061 |
approx := self + (num / 3). "/ do the first iteration |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3062 |
lastApprox := self. |
8203 | 3063 |
idx := 2. |
3064 |
[ |
|
22543 | 3065 |
idx > nFactors ifTrue:[ |
3066 |
ArithmeticError raiseErrorString:'too many iterations'. |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3067 |
"/ Not enough bernoulli numbers for now... |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3068 |
"/ |tempNum tempDen| |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3069 |
"/ tempNum := 2 raisedTo:(2*idx). |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3070 |
"/ tempNum := tempNum * (tempNum-1) * ((2*idx) bernoulli). |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3071 |
"/ tempDen := (2*idx) factorial. |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3072 |
"/ t := Array with:tempNum with:tempDen. |
22543 | 3073 |
]. |
3074 |
t := factors at:idx. |
|
3075 |
idx := idx + 1. |
|
3076 |
num := num * x2. |
|
3077 |
||
3078 |
delta := num * t first / t second. |
|
3079 |
approx := approx + delta. |
|
3080 |
delta abs > epsilon |
|
3081 |
] whileTrue. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3082 |
^ approx |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3083 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3084 |
" |
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3085 |
0.5 tan 0.546302 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3086 |
0.5q tan 0.54630249 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3087 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3088 |
0.5q tan_withAccuracy:1 0.541666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3089 |
0.5q tan_withAccuracy:0.1 0.541666667 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3090 |
0.5q tan_withAccuracy:0.01 0.545833333 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3091 |
0.5q tan_withAccuracy:0.001 0.54625496 |
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3092 |
0.5q tan_withAccuracy:1e-15 0.54630249 |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3093 |
|
10556
fb4ea1df1f1d
Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents:
10550
diff
changeset
|
3094 |
0.5q tan_withAccuracy:1e-40 -- too many iterations |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3095 |
" |
22543 | 3096 |
|
3097 |
"Modified: / 15-02-2018 / 18:53:37 / stefan" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3098 |
! ! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3099 |
|
701 | 3100 |
!Number methodsFor:'testing'! |
3101 |
||
17224 | 3102 |
even |
3103 |
"return true if the receiver is divisible by 2." |
|
3104 |
||
3105 |
self fractionPart = 0 ifTrue:[ |
|
21982 | 3106 |
^ (self / 2) fractionPart = 0 |
17224 | 3107 |
]. |
3108 |
||
3109 |
"this will raise an error" |
|
3110 |
^ super even |
|
3111 |
||
3112 |
" |
|
21982 | 3113 |
2 even |
3114 |
2.0 even |
|
3115 |
3.0 even |
|
3116 |
2.4 even |
|
3117 |
(5/3) even |
|
3118 |
||
3119 |
2 asFraction even |
|
3120 |
(2 asFixedPoint:5) even |
|
3121 |
2 asFloatE even |
|
3122 |
2 asFloatD even |
|
3123 |
2 asFloatQ even |
|
3124 |
2 asFloatQD even |
|
3125 |
||
3126 |
(2.1 asFloatQD - (QDouble readFrom:'2.1')) fractionPart |
|
17224 | 3127 |
" |
21982 | 3128 |
|
3129 |
"Modified (comment): / 03-07-2017 / 14:14:46 / cg" |
|
17224 | 3130 |
! |
3131 |
||
5360 | 3132 |
isDivisibleBy:aNumber |
3133 |
"return true, if the receiver can be divided by the argument, aNumber without a remainder. |
|
3134 |
Notice, that the result is only worth trusting, if the receiver is an integer." |
|
3135 |
||
3136 |
aNumber = 0 ifTrue: [^ false]. |
|
3137 |
aNumber isInteger ifFalse: [^ false]. |
|
3138 |
^ (self \\ aNumber) = 0 |
|
3139 |
||
3140 |
" |
|
20308 | 3141 |
3 isDivisibleBy:2 |
5360 | 3142 |
4 isDivisibleBy:2 |
20308 | 3143 |
4.0 isDivisibleBy:2 |
3144 |
4.5 isDivisibleBy:4.5 |
|
3145 |
4.5 isDivisibleBy:1.0 |
|
5360 | 3146 |
" |
3147 |
! |
|
3148 |
||
6143 | 3149 |
isNaN |
3150 |
"return true, if the receiver is an invalid float (NaN - not a number)." |
|
3151 |
||
3152 |
^ false |
|
6155 | 3153 |
|
3154 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
6143 | 3155 |
! |
3156 |
||
701 | 3157 |
isNumber |
3158 |
"return true, if the receiver is a kind of number" |
|
3159 |
||
3160 |
^ true |
|
3161 |
! |
|
3162 |
||
19136 | 3163 |
isPerfectSquare |
3164 |
"return true if I am a perfect square. |
|
3165 |
That is a number for which the square root is an integer." |
|
3166 |
||
3167 |
self truncated = self ifFalse:[^ false]. |
|
3168 |
^ self asInteger isPerfectSquare |
|
3169 |
||
3170 |
" |
|
3171 |
0 isPerfectSquare |
|
3172 |
0.0 isPerfectSquare |
|
3173 |
3 isPerfectSquare |
|
3174 |
3.0 isPerfectSquare |
|
3175 |
4 isPerfectSquare |
|
3176 |
4.0 isPerfectSquare |
|
3177 |
9 isPerfectSquare |
|
3178 |
9.0 isPerfectSquare |
|
3179 |
" |
|
3180 |
! |
|
3181 |
||
6087 | 3182 |
isReal |
3183 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
3184 |
true is returned here - the method is redefined from Object." |
|
3185 |
||
3186 |
^ true |
|
3187 |
! |
|
3188 |
||
701 | 3189 |
isZero |
1557 | 3190 |
"return true, if the receiver is zero" |
3191 |
||
701 | 3192 |
^ self = 0 |
1557 | 3193 |
|
3194 |
"Modified: 18.7.1996 / 12:40:49 / cg" |
|
701 | 3195 |
! ! |
3196 |
||
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3197 |
!Number methodsFor:'tracing'! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3198 |
|
4682 | 3199 |
traceInto:aRequestor level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3200 |
"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
|
3201 |
|
4682 | 3202 |
^ aRequestor traceNumber:self level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3203 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3204 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3205 |
! ! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
3206 |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3207 |
!Number methodsFor:'trigonometric'! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3208 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3209 |
arcCos |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3210 |
"return the arccosine of the receiver (in radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3211 |
|
19769 | 3212 |
"/ if I am not a Float (or a less general lpReal), |
3213 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3214 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3215 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3216 |
|f| |
3217 |
||
3218 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3219 |
^ f arcCos. |
|
3220 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3221 |
]. |
19769 | 3222 |
"/ slow fallback |
8844 | 3223 |
^ (self class pi / 2) - self arcSin |
21988 | 3224 |
|
3225 |
"Modified: / 05-07-2017 / 17:24:32 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3226 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3227 |
|
19768 | 3228 |
arcCosech |
3229 |
"return the inverse hyperbolic cosecant of the receiver." |
|
19769 | 3230 |
"caveat: misnomer; should be called aCosech or arCosech" |
3231 |
||
3232 |
"/ if I am not a Float (or a less general lpReal), |
|
3233 |
"/ retry after converting to float |
|
19768 | 3234 |
(self isLimitedPrecisionReal not |
3235 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 3236 |
|f| |
3237 |
||
3238 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3239 |
^ f arcCosech. |
|
3240 |
]. |
|
19768 | 3241 |
]. |
19769 | 3242 |
"/ slow fallback |
19768 | 3243 |
^ ((1 + ((self*self)+1) sqrt) / self) ln |
21988 | 3244 |
|
3245 |
"Modified: / 05-07-2017 / 17:24:56 / cg" |
|
19768 | 3246 |
! |
3247 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3248 |
arcCosh |
19768 | 3249 |
"return the inverse hyperbolic cosine of the receiver." |
19769 | 3250 |
"caveat: misnomer; should be called aCosh or arCosh" |
3251 |
||
3252 |
"/ if I am not a Float (or a less general lpReal), |
|
3253 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3254 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3255 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3256 |
|f| |
3257 |
||
3258 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3259 |
^ f arcCosh. |
|
3260 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3261 |
]. |
19769 | 3262 |
"/ slow fallback |
8844 | 3263 |
^ (self + (self*self-1) sqrt) ln. |
21988 | 3264 |
|
3265 |
"Modified: / 05-07-2017 / 17:25:03 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3266 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3267 |
|
19768 | 3268 |
arcCoth |
3269 |
"return the inverse hyperbolic cotangent of the receiver." |
|
19769 | 3270 |
"caveat: misnomer; should be called aCoth or arCoth" |
3271 |
||
3272 |
"/ if I am not a Float (or a less general lpReal), |
|
3273 |
"/ retry after converting to float |
|
19768 | 3274 |
(self isLimitedPrecisionReal not |
3275 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 3276 |
|f| |
3277 |
||
3278 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3279 |
^ f arcCoth. |
|
3280 |
]. |
|
19768 | 3281 |
]. |
19769 | 3282 |
"/ slow fallback |
19768 | 3283 |
^ ((self+1) / (self-1)) ln / 2 |
21988 | 3284 |
|
3285 |
"Modified: / 05-07-2017 / 17:25:14 / cg" |
|
19768 | 3286 |
! |
3287 |
||
3288 |
arcSech |
|
3289 |
"return the inverse hyperbolic secant of the receiver." |
|
19769 | 3290 |
"caveat: misnomer; should be called aSech or arSech" |
3291 |
||
3292 |
"/ if I am not a Float (or a less general lpReal), |
|
3293 |
"/ retry after converting to float |
|
19768 | 3294 |
(self isLimitedPrecisionReal not |
3295 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 3296 |
|f| |
3297 |
||
3298 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3299 |
^ f arcSech. |
|
3300 |
]. |
|
19768 | 3301 |
]. |
19769 | 3302 |
"/ slow fallback |
19768 | 3303 |
^ ((1 + (1-(self*self)) sqrt) / self) ln |
21988 | 3304 |
|
3305 |
"Modified: / 05-07-2017 / 17:25:22 / cg" |
|
19768 | 3306 |
! |
3307 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3308 |
arcSin |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3309 |
"return the arcsine of the receiver (in radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3310 |
|
19769 | 3311 |
"/ if I am not a Float (or a less general lpReal), |
3312 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3313 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3314 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3315 |
|f| |
3316 |
||
3317 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3318 |
^ f arcSin. |
|
3319 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3320 |
]. |
19769 | 3321 |
"/ very slow fallback |
8634 | 3322 |
^ self arcSin_withAccuracy:self epsilon |
21988 | 3323 |
|
3324 |
"Modified: / 05-07-2017 / 17:25:30 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3325 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3326 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3327 |
arcSinh |
19768 | 3328 |
"return the inverse hyperbolic sine of the receiver." |
19769 | 3329 |
"caveat: misnomer; should be called aSinh or arSinh" |
3330 |
||
3331 |
"/ if I am not a Float (or a less general lpReal), |
|
3332 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3333 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3334 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3335 |
|f| |
3336 |
||
3337 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3338 |
^ f arcSinh. |
|
3339 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3340 |
]. |
19769 | 3341 |
"/ slow fallback |
8844 | 3342 |
^ ( self + (self*self+1) sqrt ) ln |
22730 | 3343 |
"/ ^ self arcSinh_withAccuracy:self epsilon |
21988 | 3344 |
|
3345 |
"Modified: / 05-07-2017 / 17:25:38 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3346 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3347 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3348 |
arcTan |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3349 |
"return the arctangent of the receiver (as radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3350 |
|
19769 | 3351 |
"/ if I am not a Float (or a less general lpReal), |
3352 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3353 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3354 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3355 |
|f| |
3356 |
||
3357 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3358 |
^ f arcTan. |
|
3359 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3360 |
]. |
19769 | 3361 |
"/ very slow fallback |
8634 | 3362 |
^ self arcTan_withAccuracy:self epsilon |
21988 | 3363 |
|
3364 |
"Modified: / 05-07-2017 / 17:25:46 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3365 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3366 |
|
11488 | 3367 |
arcTan2:x |
3368 |
"return atan2(self,x) (as radians)" |
|
3369 |
||
19769 | 3370 |
"/ if I am not a Float (or a less general lpReal), |
3371 |
"/ retry after converting to float |
|
11488 | 3372 |
(self isLimitedPrecisionReal not |
3373 |
or:[self generality < 1.0 generality]) ifTrue:[ |
|
21988 | 3374 |
|f| |
3375 |
||
3376 |
(f := self asFloat) isFinite ifTrue:[ |
|
3377 |
^ f arcTan2:x. |
|
3378 |
]. |
|
11488 | 3379 |
]. |
19769 | 3380 |
"/ very slow fallback |
11488 | 3381 |
^ self arcTan2_withAccuracy:self epsilon x:x |
21988 | 3382 |
|
3383 |
"Modified: / 05-07-2017 / 17:26:16 / cg" |
|
11488 | 3384 |
! |
3385 |
||
22727 | 3386 |
arcTan:denominator |
3387 |
"Evaluate the four quadrant arc tangent of the argument denominator (x) and the receiver (y)." |
|
3388 |
||
3389 |
|t| |
|
3390 |
||
3391 |
(self isZero) ifTrue: [ |
|
3392 |
(denominator strictlyPositive) |
|
3393 |
ifTrue: [ ^ 0 ] |
|
3394 |
ifFalse: [ ^ self class pi ] |
|
3395 |
]. |
|
3396 |
(denominator isZero) ifTrue: [ |
|
3397 |
(self strictlyPositive) |
|
3398 |
ifTrue: [ ^ self class halfpi ] |
|
3399 |
ifFalse: [ ^ self class halfpiNegative ] |
|
3400 |
]. |
|
3401 |
t := (self / denominator) arcTan. |
|
3402 |
(denominator strictlyPositive) |
|
3403 |
ifTrue: [ ^ t ] |
|
3404 |
ifFalse: [ ^ t + self class pi ] |
|
3405 |
||
3406 |
"Created: / 07-06-2007 / 21:10:32 / cg" |
|
3407 |
"Modified: / 11-06-2007 / 12:58:34 / cg" |
|
3408 |
! |
|
3409 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3410 |
arcTanh |
19768 | 3411 |
"return the inverse hyperbolic tangent of the receiver." |
19769 | 3412 |
"caveat: misnomer; should be called aTanh or arTanh" |
20308 | 3413 |
|
19769 | 3414 |
"/ if I am not a Float (or a less general lpReal), |
3415 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3416 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3417 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3418 |
|f| |
3419 |
||
3420 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3421 |
^ f arcTanh. |
|
3422 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3423 |
]. |
19769 | 3424 |
"/ slow fallback |
19768 | 3425 |
^ ((1 + self) / (1 - self)) ln / 2 |
3426 |
"/ s^ ((1 + self) ln / 2) - ((1 - self) ln / 2) |
|
21988 | 3427 |
|
3428 |
"Modified: / 05-07-2017 / 17:26:27 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3429 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3430 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3431 |
cos |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3432 |
"return the cosine of the receiver (interpreted as radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3433 |
|
19769 | 3434 |
"/ if I am not a Float (or a less general lpReal), |
3435 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3436 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3437 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3438 |
|f| |
3439 |
||
3440 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3441 |
^ f cos. |
|
3442 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3443 |
]. |
19769 | 3444 |
"/ very slow fallback |
8634 | 3445 |
^ self cos_withAccuracy:self epsilon |
21988 | 3446 |
|
3447 |
"Modified: / 05-07-2017 / 17:26:33 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3448 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3449 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3450 |
cosh |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3451 |
"return the hyperbolic cosine of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3452 |
|
19769 | 3453 |
"/ if I am not a Float (or a less general lpReal), |
3454 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3455 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3456 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3457 |
|f| |
3458 |
||
3459 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3460 |
^ f cosh. |
|
3461 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3462 |
]. |
19769 | 3463 |
"/ very slow fallback |
8634 | 3464 |
^ self cosh_withAccuracy:self epsilon |
21988 | 3465 |
|
3466 |
"Modified: / 05-07-2017 / 17:26:39 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3467 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3468 |
|
8844 | 3469 |
cot |
3470 |
"return the cotangent of the receiver" |
|
3471 |
||
3472 |
^ 1 / self tan |
|
3473 |
! |
|
3474 |
||
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3475 |
sin |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3476 |
"return the sine of the receiver (interpreted as radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3477 |
|
19769 | 3478 |
"/ if I am not a Float (or a less general lpReal), |
3479 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3480 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3481 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3482 |
|f| |
3483 |
||
3484 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3485 |
^ f sin. |
|
3486 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3487 |
]. |
8634 | 3488 |
^ self sin_withAccuracy:self epsilon |
21988 | 3489 |
|
3490 |
"Modified: / 05-07-2017 / 17:26:47 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3491 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3492 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3493 |
sinh |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3494 |
"return the hyperbolic sine of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3495 |
|
19769 | 3496 |
"/ if I am not a Float (or a less general lpReal), |
3497 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3498 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3499 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3500 |
|f| |
3501 |
||
3502 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3503 |
^ f sinh. |
|
3504 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3505 |
]. |
8634 | 3506 |
^ self sinh_withAccuracy:self epsilon |
21988 | 3507 |
|
3508 |
" |
|
3509 |
10 sinh -> 11013.23287470339338 |
|
3510 |
(10 exp - (-10 exp)) / 2 -> 11013.23287470339338 |
|
3511 |
" |
|
3512 |
||
3513 |
"Modified (comment): / 05-07-2017 / 17:32:27 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3514 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3515 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3516 |
tan |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3517 |
"return the tangens of the receiver (interpreted as radians)" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3518 |
|
19769 | 3519 |
"/ slow fallback |
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3520 |
^ self sin / self cos |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3521 |
! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3522 |
|
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3523 |
tanh |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3524 |
"return the hyperbolic tangens of the receiver" |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3525 |
|
21988 | 3526 |
"/ tanh is: |
3527 |
"/ sinh(x) |
|
3528 |
"/ ------- |
|
3529 |
"/ cosh(x) |
|
3530 |
"/ |
|
3531 |
"/ which is: |
|
3532 |
"/ (exp(x) - exp(-x)) / 2 |
|
3533 |
"/ ---------------------- |
|
3534 |
"/ (exp(x) + exp(-x)) / 2 |
|
3535 |
||
3536 |
|exp nexp| |
|
3537 |
||
19769 | 3538 |
"/ if I am not a Float (or a less general lpReal), |
3539 |
"/ retry after converting to float |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3540 |
(self isLimitedPrecisionReal not |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3541 |
or:[self generality < 1.0 generality]) ifTrue:[ |
21988 | 3542 |
|f| |
3543 |
||
3544 |
(f := self asLongFloat) isFinite ifTrue:[ |
|
3545 |
^ f tanh. |
|
3546 |
]. |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3547 |
]. |
21988 | 3548 |
|
19769 | 3549 |
"/ very slow fallback |
21988 | 3550 |
"/ assuming that if we arrive here, the stuff is taylor computed manually anyway, |
3551 |
"/ the question is if taylor-for-exp converges faster than tailor-for-sin/cos |
|
3552 |
"/ So it may be faster to: |
|
3553 |
"/ ^ self sinh / self cosh |
|
3554 |
||
3555 |
exp := self exp. |
|
3556 |
nexp := self negated exp. |
|
3557 |
||
3558 |
^ (exp - nexp) / (exp + nexp) |
|
3559 |
||
3560 |
"Modified (comment): / 05-07-2017 / 17:34:59 / cg" |
|
7449
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3561 |
! ! |
192789c6e1c2
fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents:
7388
diff
changeset
|
3562 |
|
24158 | 3563 |
!Number methodsFor:'trigonometric (degrees)'! |
3564 |
||
3565 |
degreeCos |
|
3566 |
"Return the cosine of the receiver taken as an angle in degrees." |
|
3567 |
||
3568 |
^ self degreesToRadians cos |
|
3569 |
! |
|
3570 |
||
3571 |
degreeSin |
|
3572 |
"Return the sine of the receiver taken as an angle in degrees." |
|
3573 |
||
3574 |
^ self degreesToRadians sin |
|
3575 |
! |
|
3576 |
||
3577 |
degreeTan |
|
3578 |
"Return the cosine of the receiver taken as an angle in degrees." |
|
3579 |
||
3580 |
^ self degreesToRadians tan |
|
3581 |
! ! |
|
3582 |
||
5552 | 3583 |
!Number methodsFor:'truncation & rounding'! |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3584 |
|
4895 | 3585 |
detentBy: detent atMultiplesOf: grid snap: snap |
20308 | 3586 |
"Map all values that are within detent/2 of any multiple of grid |
3587 |
to that multiple. |
|
3588 |
Otherwise, if snap is true, return self, meaning that the values |
|
3589 |
in the dead zone will never be returned. |
|
4895 | 3590 |
If snap is false, then expand the range between dead zones |
20308 | 3591 |
so that it covers the range between multiples of the grid, |
4895 | 3592 |
and scale the value by that factor." |
3593 |
||
3594 |
| r1 r2 | |
|
3595 |
||
3596 |
r1 := self roundTo: grid. "Nearest multiple of grid" |
|
3597 |
(self roundTo: detent) = r1 ifTrue: [^ r1]. "Snap to that multiple..." |
|
3598 |
snap ifTrue: [^ self]. "...or return self" |
|
3599 |
||
3600 |
r2 := self < r1 "Nearest end of dead zone" |
|
20308 | 3601 |
ifTrue: [r1 - (detent asFloat/2)] |
3602 |
ifFalse: [r1 + (detent asFloat/2)]. |
|
4895 | 3603 |
|
3604 |
"Scale values between dead zones to fill range between multiples" |
|
3605 |
^ r1 + ((self - r2) * grid asFloat / (grid - detent)) |
|
3606 |
||
3607 |
" |
|
20308 | 3608 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: true] |
4895 | 3609 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: false] |
20308 | 3610 |
(3.9 to: 4.1 by: 0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: true] |
4895 | 3611 |
(-3.9 to: -4.1 by: -0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: false] |
3612 |
" |
|
3613 |
! |
|
3614 |
||
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3615 |
fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3616 |
"return a float with value from digits after the decimal point. |
7356 | 3617 |
i.e. the receiver minus its truncated value, |
3618 |
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
|
3619 |
|
7356 | 3620 |
^ self - self truncated |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3621 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3622 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3623 |
1234.56789 fractionPart |
20308 | 3624 |
1.2345e6 fractionPart |
3625 |
||
3626 |
1.6 asLongFloat fractionPart + 1.6 asLongFloat truncated |
|
3627 |
-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
|
3628 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3629 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3630 |
"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
|
3631 |
"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
|
3632 |
! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3633 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3634 |
integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3635 |
"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
|
3636 |
(i.e. the truncated value)" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3637 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3638 |
^ self truncated asFloat |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3639 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3640 |
" |
20308 | 3641 |
1234.56789 integerPart |
3642 |
1.2345e6 integerPart |
|
3643 |
12.5 integerPart |
|
3644 |
-12.5 integerPart |
|
3645 |
(5/3) integerPart |
|
3646 |
(-5/3) integerPart |
|
3647 |
(5/3) truncated |
|
3648 |
(-5/3) truncated |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3649 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3650 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3651 |
"Created: / 28.10.1998 / 17:14:56 / cg" |
6154 | 3652 |
"Modified: / 5.11.2001 / 17:54:22 / cg" |
23323 | 3653 |
! |
3654 |
||
3655 |
rounded:n |
|
3656 |
"Answer the float rounded with n digits of precision" |
|
3657 |
||
3658 |
| mult | |
|
3659 |
||
3660 |
mult := 10 raisedTo: n. |
|
3661 |
^ (((self * mult) rounded) asFloat / mult). |
|
3662 |
||
3663 |
" |
|
3664 |
7 rounded:2 |
|
3665 |
7.1 rounded:2 |
|
3666 |
7.2345 rounded:2 |
|
3667 |
7.2385 rounded:2 |
|
3668 |
7.2341 rounded:3 |
|
3669 |
7.2345 rounded:3 |
|
3670 |
7.2348 rounded:3 |
|
3671 |
" |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3672 |
! ! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
3673 |
|
24322 | 3674 |
|
1892 | 3675 |
!Number class methodsFor:'documentation'! |
701 | 3676 |
|
3677 |
version |
|
18850 | 3678 |
^ '$Header$' |
12112
949f9713c4a5
changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents:
11944
diff
changeset
|
3679 |
! |
949f9713c4a5
changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents:
11944
diff
changeset
|
3680 |
|
949f9713c4a5
changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents:
11944
diff
changeset
|
3681 |
version_CVS |
18850 | 3682 |
^ '$Header$' |
701 | 3683 |
! ! |
15078 | 3684 |