author | Stefan Vogel <sv@exept.de> |
Tue, 29 Jun 2004 15:20:35 +0200 | |
changeset 8422 | c206b4711b6e |
parent 8395 | 6bd97113cb4c |
child 8892 | 5d05a7f150a5 |
permissions | -rw-r--r-- |
55 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
213 | 3 |
All Rights Reserved |
55 | 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 |
" |
|
12 |
||
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
14 |
|
55 | 15 |
Number subclass:#LimitedPrecisionReal |
1200 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-Numbers' |
|
55 | 20 |
! |
21 |
||
88 | 22 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
213 | 27 |
All Rights Reserved |
55 | 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 |
! |
|
55 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
7405 | 40 |
Abstract superclass for any-precision floating point numbers (i.e. Float and Double). |
92 | 41 |
|
42 |
Due to historic reasons, ST/X's Floats are what Doubles are in ST-80. |
|
43 |
This may change soon (implementing LPReal is a first step towards this). |
|
1295 | 44 |
|
7405 | 45 |
Range and Precision of Storage Formats: |
46 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
47 |
Format | Class | Significant Digits (Binary) | Smallest Pos Number | Largest Pos Number | Significant Digits (Decimal) |
7405 | 48 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
49 |
single | ShortFloat | 24 | 1.175... 10-38 | 3.402... 10+38 | 6-9 |
|
50 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
|
51 |
double | Float | 53 | 2.225... 10-308 | 1.797... 10+308 | 15-17 |
|
52 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
|
53 |
double | LongFloat | 113 | 3.362... 10-4932 | 1.189... 10+4932 | 33-36 |
|
54 |
ext | | | | | |
|
55 |
(SPARC)| | | | | |
|
56 |
-------+ |-----------------------------+---------------------+--------------------+------------------------------ |
|
57 |
double | | 64 | 3.362... 10-4932 | 1.189... 10+4932 | 18-21 |
|
58 |
ext | | | | | |
|
59 |
(x86) | | | | | |
|
60 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
61 |
-- | LargeFloat | arbitrary | arbitrarily small | arbitrarily large | arbitrary |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
62 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
7405 | 63 |
|
64 |
||
1295 | 65 |
[author:] |
66 |
Claus Gittinger |
|
1893 | 67 |
|
68 |
[see also:] |
|
69 |
Fraction FixedPoint |
|
88 | 70 |
" |
71 |
! ! |
|
55 | 72 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
73 |
!LimitedPrecisionReal class methodsFor:'instance creation'! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
74 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
75 |
fromInteger:anInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
76 |
"return a float with anInteger's value. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
77 |
Since floats have a limited precision, you usually loose bits when doing this |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
78 |
(see Float decimalPrecision, LongFloat decimalPrecision." |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
79 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
80 |
|newFloat sign absVal| |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
81 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
82 |
newFloat := self zero. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
83 |
sign := anInteger sign. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
84 |
absVal := anInteger abs. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
85 |
(sign ~~ 0) ifTrue:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
86 |
absVal digitLength to:1 by:-1 do:[:i | |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
87 |
newFloat := (newFloat * 256.0) + (absVal digitByteAt:i) asFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
88 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
89 |
(sign < 0) ifTrue:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
90 |
newFloat := newFloat negated |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
91 |
] |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
92 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
93 |
^ newFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
94 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
95 |
" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
96 |
1234567890 asFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
97 |
1234567890 asFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
98 |
-1234567890 asFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
99 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
100 |
12345678901234567890 asFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
101 |
12345678901234567890 asFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
102 |
-12345678901234567890 asFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
103 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
104 |
12345678901234567890 asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
105 |
12345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
106 |
-12345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
107 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
108 |
123456789012345678901234567890 asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
109 |
123456789012345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
110 |
-123456789012345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
111 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
112 |
1234567890123456789012345678901234567890 asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
113 |
1234567890123456789012345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
114 |
-1234567890123456789012345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
115 |
" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
116 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
117 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
118 |
fromLimitedPrecisionReal:anLPReal |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
119 |
"return a float with anLPReals value. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
120 |
You might loose bits when doing this. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
121 |
Slow fallback." |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
122 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
123 |
|fract| |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
124 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
125 |
anLPReal isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
126 |
anLPReal isNaN ifTrue:[^ self NaN]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
127 |
anLPReal negative ifTrue:[^ self negativeInfinity]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
128 |
^ self infinity |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
129 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
130 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
131 |
fract := anLPReal asTrueFraction. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
132 |
^ (self fromInteger:fract numerator) / (self fromInteger:fract denominator) |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
133 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
134 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
135 |
new:aNumber |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
136 |
"catch this message - not allowed for floats/doubles" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
137 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
138 |
self error:'Floats/Doubles cannot be created with new:' |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
139 |
! ! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
140 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
141 |
!LimitedPrecisionReal class methodsFor:'class initialization'! |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
142 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
143 |
initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
144 |
"initialize ANSI compliant float globals" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
145 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
146 |
Smalltalk at:#FloatE put:ShortFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
147 |
Smalltalk at:#FloatD put:Float. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
148 |
Smalltalk at:#FloatQ put:LongFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
149 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
150 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
151 |
self initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
152 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
153 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
154 |
"Created: / 7.9.2001 / 14:02:45 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
155 |
"Modified: / 7.9.2001 / 14:02:54 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
156 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
157 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
158 |
!LimitedPrecisionReal class methodsFor:'constants & defaults'! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
159 |
|
7390 | 160 |
NaN |
161 |
"return the constant NaN (not a Number)." |
|
162 |
||
163 |
^ self zero uncheckedDivide:self zero |
|
164 |
||
165 |
" |
|
166 |
ShortFloat NaN |
|
167 |
Float NaN |
|
168 |
LongFloat NaN |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
169 |
LargeFloat NaN |
7390 | 170 |
" |
171 |
! |
|
172 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
173 |
e |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
174 |
"return the closest approximation of the irrational number e" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
175 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
176 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
177 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
178 |
"Modified: / 7.9.2001 / 14:05:02 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
179 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
180 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
181 |
emax |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
182 |
"return the largest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
183 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
184 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
185 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
186 |
"Created: / 7.9.2001 / 14:05:28 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
187 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
188 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
189 |
emin |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
190 |
"return the smallest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
191 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
192 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
193 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
194 |
"Created: / 7.9.2001 / 14:05:35 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
195 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
196 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
197 |
epsilon |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
198 |
"return the maximum relative spacing" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
199 |
|
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
200 |
^ self radix asFloat raisedTo:(1 - self precision) |
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
201 |
|
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
202 |
" |
7356 | 203 |
ShortFloat epsilon |
204 |
Float epsilon |
|
205 |
LongFloat epsilon |
|
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
206 |
" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
207 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
208 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
209 |
fmax |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
210 |
"return the largest value allowed" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
211 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
212 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
213 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
214 |
"Created: / 7.9.2001 / 14:06:56 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
215 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
216 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
217 |
fmin |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
218 |
"return the minimum value allowed" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
219 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
220 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
221 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
222 |
"Created: / 7.9.2001 / 14:07:06 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
223 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
224 |
|
7390 | 225 |
infinity |
226 |
"return a float representing infinity" |
|
227 |
||
228 |
^ self unity uncheckedDivide:self zero |
|
229 |
||
230 |
" |
|
231 |
ShortFloat infinity |
|
232 |
Float infinity |
|
233 |
LongFloat infinity |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
234 |
LargeFloat infinity |
7390 | 235 |
" |
236 |
! |
|
237 |
||
238 |
negativeInfinity |
|
239 |
"return a float representing negative infinity" |
|
240 |
||
241 |
^ self unity negated uncheckedDivide:self zero |
|
242 |
||
243 |
" |
|
244 |
ShortFloat negativeInfinity |
|
245 |
Float negativeInfinity |
|
246 |
LongFloat negativeInfinity |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
247 |
LargeFloat negativeInfinity |
7390 | 248 |
" |
249 |
! |
|
250 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
251 |
pi |
7356 | 252 |
"return the closest approximation of the irrational number pi" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
253 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
254 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
255 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
256 |
"Created: / 7.9.2001 / 14:07:35 / cg" |
7356 | 257 |
! ! |
258 |
||
259 |
!LimitedPrecisionReal class methodsFor:'queries'! |
|
260 |
||
261 |
decimalPrecision |
|
262 |
"return the number of valid decimal digits" |
|
263 |
||
264 |
^ (self precision / (10.0 log:self radix)) floor |
|
265 |
||
266 |
" |
|
267 |
ShortFloat decimalPrecision |
|
268 |
Float decimalPrecision |
|
269 |
LongFloat decimalPrecision |
|
270 |
" |
|
271 |
! |
|
272 |
||
273 |
numBitsInExponent |
|
274 |
"return the number of bits in the exponent" |
|
275 |
||
276 |
^ self subclassResponsibility |
|
277 |
! |
|
278 |
||
279 |
numBitsInIntegerPart |
|
280 |
^ 0 |
|
281 |
! |
|
282 |
||
283 |
numBitsInMantissa |
|
284 |
"return the number of bits in the mantissa |
|
285 |
(typically 1 less than the precision due to the hidden bit)" |
|
286 |
||
287 |
^ self subclassResponsibility |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
288 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
289 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
290 |
precision |
7356 | 291 |
"return the number of valid mantissa bits" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
292 |
|
7356 | 293 |
^ self numBitsInMantissa + 1 - self numBitsInIntegerPart |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
294 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
295 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
296 |
radix |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
297 |
"return the radix (base)" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
298 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
299 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
300 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
301 |
"Created: / 7.9.2001 / 14:08:20 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
302 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
303 |
|
55 | 304 |
!LimitedPrecisionReal methodsFor:'accessing'! |
305 |
||
306 |
at:index |
|
307 |
"redefined to prevent access to individual bytes in a real." |
|
308 |
||
309 |
self error:'not allowed for floats/doubles' |
|
310 |
! |
|
311 |
||
312 |
at:index put:aValue |
|
313 |
"redefined to prevent access to individual bytes in a real" |
|
314 |
||
315 |
self error:'not allowed for floats/doubles' |
|
316 |
! ! |
|
317 |
||
318 |
!LimitedPrecisionReal methodsFor:'arithmetic'! |
|
319 |
||
701 | 320 |
* aNumber |
321 |
"return the product of the receiver and the argument, aNumber" |
|
322 |
||
1200 | 323 |
"/ as soon as Float are float & Double are doubles, |
324 |
"/ use: |
|
325 |
"/ ^ aNumber productFromDouble:self asDouble |
|
326 |
||
327 |
^ aNumber productFromFloat:self asFloat |
|
328 |
||
329 |
"Modified: 17.4.1996 / 12:35:36 / cg" |
|
701 | 330 |
! |
331 |
||
55 | 332 |
+ aNumber |
333 |
"return the sum of the receiver and the argument, aNumber" |
|
334 |
||
1200 | 335 |
"/ as soon as Float are float & Double are doubles, |
336 |
"/ use: |
|
337 |
"/ ^ aNumber sumFromDouble:self asDouble |
|
338 |
||
339 |
^ aNumber sumFromFloat:self asFloat |
|
340 |
||
341 |
"Modified: 17.4.1996 / 12:35:55 / cg" |
|
55 | 342 |
! |
343 |
||
344 |
- aNumber |
|
345 |
"return the difference of the receiver and the argument, aNumber" |
|
346 |
||
1200 | 347 |
"/ as soon as Float are float & Double are doubles, |
348 |
"/ use: |
|
349 |
"/ ^ aNumber differenceFromDouble:self asDouble |
|
350 |
||
351 |
^ aNumber differenceFromFloat:self asFloat |
|
352 |
||
353 |
"Modified: 17.4.1996 / 12:36:07 / cg" |
|
55 | 354 |
! |
355 |
||
356 |
/ aNumber |
|
357 |
"return the quotient of the receiver and the argument, aNumber" |
|
358 |
||
359 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
7141
033f2c26d8e6
replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents:
6890
diff
changeset
|
360 |
^ ZeroDivide raiseRequestWith:thisContext. |
55 | 361 |
]. |
1200 | 362 |
"/ as soon as Float are float & Double are doubles, |
363 |
"/ use: |
|
364 |
"/ ^ aNumber quotientFromDouble:self asDouble |
|
365 |
||
366 |
^ aNumber quotientFromFloat:self asFloat |
|
367 |
||
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
3898
diff
changeset
|
368 |
"Modified: / 17.4.1996 / 12:36:21 / cg" |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
3898
diff
changeset
|
369 |
"Modified: / 26.7.1999 / 10:46:11 / stefan" |
55 | 370 |
! |
371 |
||
372 |
// aNumber |
|
373 |
"return the integer quotient of dividing the receiver by aNumber with |
|
1882 | 374 |
truncation towards negative infinity." |
55 | 375 |
|
376 |
^ (self / aNumber) floor asInteger |
|
1882 | 377 |
|
378 |
"Modified: 5.11.1996 / 11:45:37 / cg" |
|
7356 | 379 |
! |
380 |
||
381 |
ceiling |
|
7398 | 382 |
^ self asTrueFraction ceiling |
7356 | 383 |
! |
384 |
||
385 |
floor |
|
7398 | 386 |
^ self asTrueFraction floor |
55 | 387 |
! ! |
388 |
||
5238 | 389 |
!LimitedPrecisionReal methodsFor:'coercing & converting'! |
55 | 390 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
391 |
asFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
392 |
^ Float fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
393 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
394 |
|
55 | 395 |
asFraction |
5883 | 396 |
"Answer a rational number (Integer or Fraction) representing the receiver. |
397 |
This conversion uses the continued fraction method to approximate |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
398 |
a floating point number. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
399 |
In contrast to #asTrueFraction, which returns exactly the value of the float, |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
400 |
this rounds in the last significant bit of the floating point number. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
401 |
" |
5883 | 402 |
|
403 |
|num1 denom1 num2 denom2 int frac newD temp limit| |
|
55 | 404 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
405 |
self isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
406 |
"/ ^ self asMetaNumber. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
407 |
^ self class |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
408 |
raise:#domainErrorSignal |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
409 |
receiver:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
410 |
selector:#asFraction |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
411 |
arguments:#() |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
412 |
errorString:'Cannot represent non-finite number as fraction' |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
413 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
414 |
|
7394 | 415 |
limit := 10 raisedToInteger:self defaultNumberOfDigits. |
5883 | 416 |
num1 := self truncated. |
417 |
denom1 := 1. "The first of two alternating denominators" |
|
418 |
num2 := 1. "The second numerator" |
|
419 |
denom2 := 0. "The second denominator--will update" |
|
420 |
int := num1. "The integer part of self" |
|
421 |
frac := self fractionPart. |
|
422 |
[frac = 0] whileFalse:[ |
|
423 |
newD := 1.0 / frac. |
|
424 |
int := newD truncated. |
|
425 |
frac := newD fractionPart. "save the fractional part for next time" |
|
426 |
temp := num2. "old numerator and save it" |
|
427 |
num2 := num1. |
|
428 |
num1 := num1 * int + temp. "Update first numerator" |
|
429 |
temp := denom2. "old denominator and save it" |
|
430 |
denom2 := denom1. |
|
431 |
denom1 := int * denom1 + temp. "Update first denominator" |
|
432 |
limit < denom1 ifTrue:[ |
|
433 |
"Is ratio past float precision? If so, pick which of the two ratios to use" |
|
434 |
num2 = 0.0 ifTrue:[ |
|
435 |
"Is second denominator 0?" |
|
436 |
^ Fraction numerator:num1 denominator:denom1 |
|
437 |
]. |
|
438 |
^ Fraction numerator:num2 denominator:denom2 |
|
439 |
] |
|
3060 | 440 |
]. |
441 |
||
5883 | 442 |
"If fractional part is zero, return the first ratio" |
443 |
denom1 = 1 ifTrue:[ |
|
444 |
"Am i really an Integer?" |
|
445 |
^ num1 "Yes, return Integer result" |
|
3060 | 446 |
]. |
5883 | 447 |
"Otherwise return Fraction result" |
448 |
^ Fraction numerator:num1 denominator:denom1 |
|
55 | 449 |
|
3060 | 450 |
" |
5883 | 451 |
1.1 asFraction |
452 |
1.2 asFraction |
|
453 |
0.3 asFraction |
|
7394 | 454 |
0.5 asFraction |
455 |
(1/5) asFloat asFraction |
|
5883 | 456 |
(1/8) asFloat asFraction |
457 |
(1/13) asFloat asFraction |
|
458 |
3.14159 asFraction |
|
459 |
3.14159 asFraction asFloat |
|
7394 | 460 |
1.3 asFraction |
461 |
1.0 asFraction |
|
462 |
1E6 asFraction |
|
463 |
1E-6 asFraction |
|
3060 | 464 |
" |
2793 | 465 |
|
3060 | 466 |
"Modified: / 25.10.1997 / 16:41:19 / cg" |
55 | 467 |
! |
468 |
||
469 |
asInteger |
|
470 |
"return an integer with same value - might truncate" |
|
471 |
||
3185 | 472 |
|max maxF| |
55 | 473 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
474 |
self isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
475 |
"/ ^ self asMetaNumber. |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
476 |
^ self class |
7418
521174de046d
Nan asInteger raises DomainError
Claus Gittinger <cg@exept.de>
parents:
7405
diff
changeset
|
477 |
raise:#domainErrorSignal |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
478 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
479 |
selector:#asInteger |
6195 | 480 |
arguments:#() |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
481 |
errorString:'Cannot represent non-finite number as integer' |
2388 | 482 |
]. |
483 |
||
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
484 |
self abs < 2e16 ifTrue:[ |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
485 |
"/ NOTICE: this must be redefined in float |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
486 |
"/ subclasses to handle the smallinteger range; |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
487 |
"/ i.e. this may only be invoked for reals |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
488 |
"/ which are NOT within the smallInt range. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
489 |
"/ otherwise, endless recursion is the consequence. |
3426 | 490 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
491 |
max := SmallInteger maxVal // 2 + 1. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
492 |
maxF := max asFloat. |
3426 | 493 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
494 |
^ (self quo:maxF) * max + (self rem:maxF) truncated |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
495 |
]. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
496 |
^ self asTrueFraction |
55 | 497 |
|
3137 | 498 |
" |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
499 |
12345.0 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
500 |
1e15 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
501 |
1e33 asInteger asFloat |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
502 |
1e303 asInteger asFloat |
3137 | 503 |
" |
2388 | 504 |
|
6195 | 505 |
"Modified: / 16.11.2001 / 14:15:33 / cg" |
701 | 506 |
! |
507 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
508 |
asLargeFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
509 |
^ LargeFloat fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
510 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
511 |
|
7378 | 512 |
asLimitedPrecisionReal |
513 |
"return a float of any precision with same value" |
|
514 |
||
515 |
^ self |
|
516 |
! |
|
517 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
518 |
asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
519 |
^ LongFloat fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
520 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
521 |
|
5883 | 522 |
asRational |
523 |
"Answer a Rational number--Integer or Fraction--representing the receiver. |
|
524 |
Same as asFraction fro st-80 compatibility." |
|
525 |
||
526 |
^ self asFraction |
|
527 |
||
528 |
" |
|
529 |
1.1 asRational |
|
530 |
1.2 asRational |
|
531 |
0.3 asRational |
|
532 |
0.5 asRational |
|
533 |
(1/5) asFloat asRational |
|
534 |
(1/8) asFloat asRational |
|
535 |
(1/13) asFloat asRational |
|
536 |
3.14159 asRational |
|
537 |
3.14159 asRational asFloat |
|
538 |
1.3 asRational |
|
539 |
1.0 asRational |
|
540 |
" |
|
541 |
! |
|
542 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
543 |
asShortFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
544 |
^ ShortFloat fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
545 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
546 |
|
7356 | 547 |
asTrueFraction |
548 |
"Answer a fraction or integer that EXACTLY represents self, |
|
549 |
an any-precision IEEE floating point number, consisting of: |
|
550 |
numMantissaBits bits of normalized mantissa (i.e. with hidden leading 1-bit) |
|
551 |
optional numExtraBits between mantissa and exponent (normalized flag for ext-real) |
|
552 |
numExponentBits bits of 2s complement exponent |
|
553 |
1 sign bit. |
|
554 |
Taken from Floats asTrueFraction" |
|
555 |
||
556 |
|shifty sign expPart exp fraction fractionPart result zeroBitsCount |
|
557 |
numBytes numBits numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
558 |
biasExponent numIntegerBits| |
|
559 |
||
560 |
self isFinite ifFalse:[ |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
561 |
"/ ^ self asMetaNumber |
7356 | 562 |
^ self class |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
563 |
raise:#domainErrorSignal |
7356 | 564 |
receiver:self |
565 |
selector:#asTrueFraction |
|
566 |
arguments:#() |
|
567 |
errorString:'Cannot represent non-finite float as a fraction'. |
|
568 |
]. |
|
569 |
||
570 |
"Extract the bits of an IEEE anySize float " |
|
571 |
numBytes := self basicSize. |
|
572 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
573 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
574 |
numIntegerBits := self class numBitsInIntegerPart. |
|
575 |
numBits := numBitsInMantissa + numBitsInExponent. "not (numBytes * 8 - 1) -- there could be 80 float bits in 96 overall bits" |
|
576 |
biasExponent := maskExponent bitShift:-1. |
|
577 |
||
578 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
579 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
580 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
581 |
] ifFalse:[ |
|
582 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
583 |
]. |
|
584 |
||
585 |
" Extract the sign and the biased exponent " |
|
586 |
sign := (shifty bitAt:numBits+1) == 0 ifTrue: [1] ifFalse: [-1]. |
|
587 |
expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent. |
|
588 |
||
589 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
590 |
fractionPart := shifty bitAnd:maskMantissa. |
|
591 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
701 | 592 |
|
7356 | 593 |
numIntegerBits == 0 ifTrue:[ |
594 |
" Replace omitted leading 1 in fraction (Notice: quadIEEE format does not do this)" |
|
595 |
fraction := fractionPart bitOr: (maskMantissa + 1). |
|
596 |
] ifFalse:[ |
|
597 |
fraction := fractionPart. |
|
598 |
]. |
|
599 |
||
7390 | 600 |
"Unbias exponent" |
7356 | 601 |
exp := biasExponent - expPart + (numBitsInMantissa - numIntegerBits). |
1200 | 602 |
|
7390 | 603 |
" Form the result. When exp>fractionalPrecision, the exponent is adjusted by |
7356 | 604 |
the number of trailing zero bits in the fraction to minimize |
7390 | 605 |
the (huge) time otherwise spent in #gcd: of fraction handling code." |
7356 | 606 |
exp negative ifTrue: [ |
7480 | 607 |
result := sign * (fraction bitShift: exp negated) |
7356 | 608 |
] ifFalse:[ |
609 |
zeroBitsCount := fraction lowBit - 1. |
|
610 |
exp := exp - zeroBitsCount. |
|
611 |
exp <= 0 ifTrue: [ |
|
612 |
zeroBitsCount := zeroBitsCount + exp. |
|
7480 | 613 |
result := sign * (fraction bitShift:zeroBitsCount negated) |
7356 | 614 |
] ifFalse: [ |
615 |
result := Fraction |
|
7480 | 616 |
numerator: (sign * (fraction bitShift: zeroBitsCount negated)) |
7356 | 617 |
denominator: (1 bitShift:exp) |
618 |
] |
|
619 |
]. |
|
1200 | 620 |
|
7356 | 621 |
"Low cost validation omitted after extensive testing" |
622 |
"(result asFloat = self) ifFalse: [self error: 'asTrueFraction validation failed']." |
|
623 |
||
624 |
^ result |
|
625 |
||
626 |
" |
|
7390 | 627 |
0.3 asFloat asTrueFraction |
7356 | 628 |
0.3 asShortFloat asTrueFraction |
7390 | 629 |
0.3 asLongFloat asTrueFraction |
630 |
||
631 |
1.25 asTrueFraction |
|
7480 | 632 |
1.25 asShortFloat asTrueFraction |
7390 | 633 |
0.25 asTrueFraction |
634 |
-0.25 asTrueFraction |
|
635 |
3e37 asTrueFraction |
|
636 |
||
637 |
Float NaN asTrueFraction -> error |
|
638 |
Float infinity asTrueFraction -> error |
|
639 |
Float negativeInfinity asTrueFraction -> error |
|
640 |
" |
|
641 |
! |
|
642 |
||
643 |
exponent |
|
644 |
"extract a normalized floats exponent. |
|
645 |
The returned value depends on the float-representation of |
|
646 |
the underlying machine and is therefore highly unportable. |
|
647 |
This is not for general use. |
|
648 |
This assumes that the mantissa is normalized to |
|
649 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
650 |
||
651 |
|shifty expPart exp numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
652 |
biasExponent numIntegerBits fractionPart| |
|
7356 | 653 |
|
7390 | 654 |
"Extract the bits of an IEEE anySize float " |
655 |
numBytes := self basicSize. |
|
656 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
657 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
658 |
numIntegerBits := self class numBitsInIntegerPart. |
|
659 |
biasExponent := maskExponent bitShift:-1. |
|
660 |
||
661 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
662 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
663 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
664 |
] ifFalse:[ |
|
665 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
666 |
]. |
|
667 |
||
668 |
" Extract the sign and the biased exponent " |
|
669 |
expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent. |
|
670 |
||
671 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
672 |
fractionPart := shifty bitAnd:maskMantissa. |
|
673 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
674 |
||
675 |
exp := expPart - biasExponent + 1. |
|
676 |
^ exp |
|
677 |
||
678 |
" |
|
679 |
0.3 asFloat exponent |
|
680 |
0.3 asShortFloat exponent |
|
681 |
0.3 asLongFloat exponent |
|
682 |
||
683 |
0.0 exponent2 0 |
|
684 |
1.0 exponent2 1 |
|
685 |
2.0 exponent2 2 |
|
686 |
3.0 exponent2 2 |
|
687 |
4.0 exponent2 3 |
|
688 |
0.5 exponent2 0 |
|
689 |
0.4 exponent2 -1 |
|
690 |
0.25 exponent2 -1 |
|
691 |
0.00000011111 exponent2 -23 |
|
7356 | 692 |
" |
701 | 693 |
! |
694 |
||
3898 | 695 |
fractionalPart |
696 |
"This has been renamed to #fractionPart for ST80 compatibility. |
|
697 |
||
698 |
extract the after-decimal fraction part. |
|
699 |
the floats value is |
|
700 |
float truncated + float fractionalPart" |
|
701 |
||
5868 | 702 |
<resource:#obsolete> |
703 |
||
3898 | 704 |
self obsoleteMethodWarning:'please use #fractionPart'. |
705 |
^ self fractionPart |
|
706 |
||
707 |
"Modified: / 28.10.1998 / 17:10:12 / cg" |
|
708 |
"Created: / 28.10.1998 / 17:10:32 / cg" |
|
709 |
! |
|
710 |
||
7356 | 711 |
i |
712 |
"return a complex number, with the receiver as imaginary part, 0 as real part" |
|
713 |
||
714 |
^ Complex |
|
715 |
real:0 |
|
716 |
imaginary:self |
|
717 |
||
718 |
" |
|
719 |
3.0i |
|
720 |
(1+1i) |
|
721 |
" |
|
701 | 722 |
! ! |
723 |
||
724 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
725 |
||
726 |
< aNumber |
|
727 |
"return true, if the argument is greater" |
|
728 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
729 |
"/ as soon as Float are float & Double are doubles, |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
730 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
731 |
"/ ^ aNumber lessFromDouble:self asDouble |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
732 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
733 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
734 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
735 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 736 |
! ! |
737 |
||
738 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
739 |
||
740 |
deepCopy |
|
741 |
"return a deep copy of myself |
|
742 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
743 |
||
744 |
^ self |
|
745 |
! |
|
746 |
||
747 |
deepCopyUsing:aDictionary |
|
748 |
"return a deep copy of myself |
|
749 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
750 |
||
751 |
^ self |
|
752 |
! |
|
753 |
||
754 |
shallowCopy |
|
755 |
"return a shallow copy of the receiver" |
|
756 |
||
757 |
^ self |
|
758 |
! |
|
759 |
||
760 |
simpleDeepCopy |
|
761 |
"return a deep copy of the receiver |
|
762 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
763 |
||
764 |
^ self |
|
55 | 765 |
! ! |
766 |
||
767 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
768 |
||
769 |
differenceFromFraction:aFraction |
|
213 | 770 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 771 |
|
772 |
|d| |
|
773 |
||
774 |
d := aFraction denominator. |
|
775 |
^ (aFraction numerator - (self * d)) / d |
|
776 |
! |
|
777 |
||
778 |
productFromFraction:aFraction |
|
213 | 779 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 780 |
|
781 |
^ self * aFraction numerator / aFraction denominator |
|
782 |
! |
|
783 |
||
784 |
quotientFromFraction:aFraction |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
785 |
"Return the quotient of the argument, aFraction and the receiver. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
786 |
Sent when aFraction does not know how to divide by the receiver." |
55 | 787 |
|
788 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 789 |
! |
790 |
||
791 |
sumFromFraction:aFraction |
|
792 |
"sent when a fraction does not know how to add the receiver, a float" |
|
793 |
||
794 |
|d| |
|
795 |
||
796 |
d := aFraction denominator. |
|
797 |
^ (self * d + aFraction numerator) / d |
|
798 |
! ! |
|
799 |
||
800 |
!LimitedPrecisionReal methodsFor:'printing & storing'! |
|
801 |
||
802 |
printOn:aStream |
|
803 |
"append a printed representation of the receiver to |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
804 |
the argument, aStream. |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
805 |
|
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
806 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
807 |
#printOn: as basic print mechanism." |
701 | 808 |
|
809 |
aStream nextPutAll:self printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
810 |
|
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
811 |
"Modified: / 20.1.1998 / 14:10:46 / stefan" |
1200 | 812 |
! |
813 |
||
814 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
815 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
816 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
817 |
#printOn: as basic print mechanism." |
1200 | 818 |
|
819 |
^ self subclassResponsibility |
|
820 |
||
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
821 |
"Created: / 17.4.1996 / 12:12:20 / cg" |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
822 |
"Modified: / 20.1.1998 / 14:10:47 / stefan" |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
823 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
824 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
825 |
storeOn:aStream |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
826 |
"append a printed representation of the receiver to |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
827 |
the argument, aStream. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
828 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
829 |
LimitedPrecisonReal and its subclasses use #storeString instead of |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
830 |
#storeOn: as basic store mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
831 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
832 |
aStream nextPutAll:self storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
833 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
834 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
835 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
836 |
"return a printed representation of the receiver |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
837 |
LimitedPrecisonReal and its subclasses use #storeString instead of |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
838 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
839 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
840 |
^ self subclassResponsibility |
701 | 841 |
! ! |
842 |
||
5557 | 843 |
!LimitedPrecisionReal methodsFor:'queries'! |
844 |
||
5883 | 845 |
defaultNumberOfDigits |
7356 | 846 |
"Answer how many digits of accuracy this class supports" |
847 |
||
848 |
^ self class decimalPrecision - 1 |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
849 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
850 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
851 |
Float new defaultNumberOfDigits |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
852 |
LongFloat new defaultNumberOfDigits |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
853 |
ShortFloat new defaultNumberOfDigits |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
854 |
" |
5883 | 855 |
! |
856 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
857 |
precision |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
858 |
"return the number of valid mantissa bits. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
859 |
Should be redefined in classes which allow per-instance precision specification" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
860 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
861 |
^ self class precision |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
862 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
863 |
|
5557 | 864 |
size |
865 |
"redefined since reals are kludgy (ByteArry)" |
|
866 |
||
867 |
^ 0 |
|
868 |
! ! |
|
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
869 |
|
701 | 870 |
!LimitedPrecisionReal methodsFor:'testing'! |
871 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
872 |
isFinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
873 |
^ self subclassResponsibility |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
874 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
875 |
"Created: / 7.1.1998 / 12:02:06 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
876 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
877 |
|
6185 | 878 |
isFloat |
879 |
"return true, if the receiver is some kind of floating point number; |
|
880 |
false is returned here. |
|
881 |
Same as #isLimitedPrecisionReal, but a better name ;-)" |
|
882 |
||
883 |
^ true |
|
884 |
||
885 |
"Created: / 14.11.2001 / 14:57:55 / cg" |
|
886 |
! |
|
887 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
888 |
isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
889 |
"return true, if the receiver is an infinite float (Inf). |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
890 |
These are not created by ST/X float operations (they raise an exception); |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
891 |
however, inline C-code could produce them ..." |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
892 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
893 |
^ (self isFinite or:[self isNaN]) not. |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
894 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
895 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
896 |
1.0 isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
897 |
(0.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
898 |
(1.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
899 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
900 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
901 |
"Modified: / 7.1.1998 / 12:01:30 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
902 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
903 |
|
6086
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
904 |
isLimitedPrecisionReal |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
905 |
"return true, if the receiver is some kind of limited precision real (i.e. floating point) number; |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
906 |
true is returned here - the method is redefined from Object." |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
907 |
|
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
908 |
^ true |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
909 |
! |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
910 |
|
2388 | 911 |
isNaN |
912 |
^ self subclassResponsibility |
|
913 |
||
914 |
"Modified: 12.2.1997 / 16:45:27 / cg" |
|
915 |
! |
|
916 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
917 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
918 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
919 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
920 |
^ (self isZero) and:[self printString first == $-] |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
921 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
922 |
" |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
923 |
0.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
924 |
-0.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
925 |
-1.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
926 |
1.0 asLongFloat isNegativeZero |
7356 | 927 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
928 |
0.0 asLargeFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
929 |
-0.0 asLargeFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
930 |
" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
931 |
" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
932 |
0.0 asLongFloat isZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
933 |
-0.0 asLongFloat isZero |
701 | 934 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
935 |
0.0 > 0 |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
936 |
-0.0 < 0 |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
937 |
" |
701 | 938 |
! |
939 |
||
5357 | 940 |
numberOfBits |
941 |
"return the size (in bits) of the real; |
|
942 |
typically, this is 64 for Floats and 32 for ShortFloats, |
|
943 |
but who knows ..." |
|
944 |
||
945 |
self subclassResponsibility |
|
946 |
! |
|
947 |
||
701 | 948 |
positive |
949 |
"return true if the receiver is greater or equal to zero" |
|
950 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
951 |
^ self sign >= 0 |
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
952 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
953 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
55 | 954 |
! ! |
955 |
||
7356 | 956 |
!LimitedPrecisionReal methodsFor:'truncation & rounding'! |
957 |
||
958 |
ceilingAsFloat |
|
959 |
^ self coerce:(self ceiling). |
|
960 |
||
961 |
" |
|
962 |
0.4 asLongFloat ceilingAsFloat |
|
963 |
" |
|
964 |
! |
|
965 |
||
966 |
floorAsFloat |
|
967 |
^ self coerce:(self floor). |
|
968 |
||
969 |
" |
|
970 |
0.4 asLongFloat floorAsFloat |
|
971 |
" |
|
972 |
! |
|
973 |
||
974 |
roundedAsFloat |
|
975 |
self negative ifTrue:[ ^ (self - 0.5) ceilingAsFloat ]. |
|
976 |
^ (self + 0.5) floorAsFloat |
|
7398 | 977 |
! |
978 |
||
979 |
truncatedAsFloat |
|
980 |
^ self coerce:(self truncated). |
|
981 |
||
982 |
" |
|
983 |
0.4 asLongFloat truncatedAsFloat |
|
984 |
" |
|
7356 | 985 |
! ! |
986 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
987 |
!LimitedPrecisionReal methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
988 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
989 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
990 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
991 |
^ aVisitor visitFloat:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
992 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
993 |
|
701 | 994 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 995 |
|
701 | 996 |
version |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
997 |
^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.61 2004-06-29 13:20:30 stefan Exp $' |
55 | 998 |
! ! |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
999 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
1000 |
LimitedPrecisionReal initialize! |