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