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