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