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