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