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