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