author | Stefan Vogel <sv@exept.de> |
Wed, 08 May 2019 15:04:54 +0200 | |
changeset 24121 | d0148c842873 |
parent 23402 | 50a54f696d5e |
child 24155 | d2c7144d7899 |
permissions | -rw-r--r-- |
16636
42a6cf3b51ba
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16444
diff
changeset
|
1 |
"{ Encoding: utf8 }" |
42a6cf3b51ba
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16444
diff
changeset
|
2 |
|
55 | 3 |
" |
4 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
21861 | 5 |
All Rights Reserved |
55 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
15 |
|
17380 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
55 | 18 |
Number subclass:#LimitedPrecisionReal |
21896 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Magnitude-Numbers' |
|
55 | 23 |
! |
24 |
||
88 | 25 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
21861 | 30 |
All Rights Reserved |
55 | 31 |
|
88 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
55 | 40 |
|
88 | 41 |
documentation |
42 |
" |
|
21771 | 43 |
Abstract superclass for any-precision floating point numbers (i.e. IEEE floats and doubles). |
92 | 44 |
|
21811 | 45 |
Short summary for beginners (find details in wikipedia): |
46 |
======================================================== |
|
47 |
||
48 |
Floating point numbers are represented with a mantissa and an exponent, and the number's value is: |
|
49 |
mantissa * (2 raisedTo: exponent) |
|
50 |
with (1 > mantissa >= 0) and exponent adjusted as required for the mantissa to be in that range |
|
51 |
(so called ''normalized'') |
|
52 |
||
53 |
therefore, |
|
54 |
13 asFloat mantissa -> 0.8125 |
|
55 |
13 asFloat exponent -> 4 |
|
56 |
0.8125 * (2 raisedTo:4) -> 13 |
|
57 |
||
58 |
and: |
|
59 |
104 asFloat mantissa -> 0.8125 |
|
60 |
104 asFloat exponent -> 7 |
|
61 |
0.8125 * (2 raisedTo:7) -> 104 |
|
62 |
||
63 |
and: |
|
64 |
0.1 mantissa -> 0.8 |
|
65 |
0.1 exponent -> -3 |
|
66 |
0.8 * (2 raisedTo:-3) -> 0.1 |
|
67 |
||
68 |
however: |
|
69 |
(1 / 3.0) mantissa -> 0.666666666666667 |
|
70 |
(1 / 3.0) exponent -> -1 |
|
71 |
0.666666666666667 * (2 raisedTo:-3) -> 0.1 |
|
72 |
||
73 |
||
74 |
Danger in using Floats: |
|
75 |
======================= |
|
76 |
||
77 |
Beginners seem to forget (or never learn?) that flt. point numbers are always APPROXIMATIONs of some value. |
|
22288 | 78 |
You may never ever use them when exact results are neeed (i.e. when computing money!!) |
21811 | 79 |
Take a look at the FixedPoint class for that. |
80 |
See also 'Float comparison' below. |
|
81 |
||
82 |
||
83 |
The Float/Double confusion in ST/X: |
|
84 |
=================================== |
|
85 |
||
86 |
Due to historic reasons, ST/X's Floats are what Doubles are in VisualWorks. |
|
87 |
||
21771 | 88 |
The reason is that in some Smalltalks, double floats are called Float, and no single float exists (VSE, V'Age), |
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
89 |
whereas in others, there are both Float and Double classes (VisualWorks). |
21811 | 90 |
In order to allow code from both families to be loaded into ST/X without a missing class error, and without |
91 |
loosing precision, we decided to use IEEE doubles as the internal representation of Float |
|
92 |
and make Double an alias to it. |
|
93 |
This should work for either family (except for the unexpected additional precision in some cases). |
|
94 |
||
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
95 |
If you really only want single precision floating point numbers, use ShortFloat instances. |
21771 | 96 |
But be aware that there is usually no advantage (neither in memory usage, due to memory alignment restrictions, |
97 |
nor in speed), as these days, the CPUs are just as fast doing double precision operations. |
|
21811 | 98 |
(There might be a noticable difference when doing bulk operations, and you should consider using FloatArray for those). |
99 |
||
100 |
||
101 |
Hardware supported precisions |
|
102 |
============================= |
|
1295 | 103 |
|
21811 | 104 |
The only really portable sizes are IEEE-single and IEEE-double floats (i.e. ShortFloat and Float instances). |
105 |
These are supported on all architectures. |
|
106 |
Some do provide an extended precision floating pnt. number, |
|
22288 | 107 |
however, the downside is that CPU-architects did not agree on a common format and precision: |
108 |
some use 80 bits, others 96 and others even 128. |
|
21811 | 109 |
See the comments in the LongFloat class for more details. |
110 |
We recommend using Float (i.e. IEEE doubles) unless absolutely required, |
|
111 |
and care for machine dependencies in the code otherwise. |
|
21842 | 112 |
For higher precision needs, you may also try the new QDouble class, which gives you >200bits (60digits) |
113 |
of precision on all machines (at a noticable performance price, though). |
|
21811 | 114 |
|
115 |
||
7405 | 116 |
Range and Precision of Storage Formats: |
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
117 |
======================================= |
7405 | 118 |
|
21771 | 119 |
Format | Class | Array Class | Bits / Significant | Smallest Pos Number | Largest Pos Number | Significant Digits |
16636
42a6cf3b51ba
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16444
diff
changeset
|
120 |
| | | (Binary) | | | (Decimal) |
21771 | 121 |
-------+------------+-----------------+---------------------+---------------------+--------------------+-------------------- |
21802 | 122 |
half | -- | HalfFloatArray | 16 / 11 | 6.10.... x 10−5 | 6.55... x 10+5 | 3.3 |
21771 | 123 |
-------+------------+-----------------+---------------------+---------------------+--------------------+-------------------- |
21802 | 124 |
single | ShortFloat | FloatArray | 32 / 24 | 1.175... x 10-38 | 3.402... x 10+38 | 6-9 |
21771 | 125 |
-------+------------+-----------------+---------------------+---------------------+--------------------+-------------------- |
21802 | 126 |
double | Float | DoubleArray | 64 / 53 | 2.225... x 10-308 | 1.797... x 10+308 | 15-17 |
21771 | 127 |
-------+------------+-----------------+---------------------+---------------------+--------------------+-------------------- |
21802 | 128 |
double | LongFloat | -- | 128 / 113 | 3.362... x 10-4932 | 1.189... x 10+4932 | 33-36 |
16636
42a6cf3b51ba
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16444
diff
changeset
|
129 |
ext | | | | | | |
42a6cf3b51ba
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16444
diff
changeset
|
130 |
(SPARC)| | | | | | |
21771 | 131 |
-------+ | |---------------------+---------------------+--------------------+-------------------- |
21802 | 132 |
double | | | 96 / 64 | 3.362... x 10-4932 | 1.189... x 10+4932 | 18-21 |
16636
42a6cf3b51ba
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16444
diff
changeset
|
133 |
ext | | | | | | |
42a6cf3b51ba
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16444
diff
changeset
|
134 |
(x86) | | | | | | |
21771 | 135 |
-------+------------+-----------------+---------------------+---------------------+--------------------+-------------------- |
21802 | 136 |
-- | QDouble | -- | 256 / 212 | 2.225... x 10-308 | 1.797... x 10+308 | >=60 |
137 |
-------+------------+-----------------+---------------------+---------------------+--------------------+-------------------- |
|
16636
42a6cf3b51ba
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16444
diff
changeset
|
138 |
-- | LargeFloat | -- | arbitrary | arbitrarily small | arbitrarily large | arbitrary |
21771 | 139 |
-------+------------+-----------------+---------------------+---------------------+--------------------+-------------------- |
7405 | 140 |
|
21802 | 141 |
HalfFloats are only supported in fixed array containers. |
142 |
This was added for OpenGL and other graphic libraries which allow for texture, |
|
143 |
and vertex data to be passed quickly in that format (see http://www.opengl.org/wiki/Small_Float_Formats). |
|
144 |
||
21771 | 145 |
Long- and LargeFloat are not supported as array containers. |
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
146 |
These formats are seldom used for bulk data. |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
147 |
|
21811 | 148 |
QDoubles are special soft floats; slower in performance, but providing 4 times the precision of regular doubles. |
149 |
||
21842 | 150 |
To see the differences in precision: |
151 |
||
152 |
'%60.58f' printf:{ 1 asShortFloat exp } -> '2.718281828459045*090795598298427648842334747314453125' (32 bits) |
|
153 |
'%60.58f' printf:{ 1 asFloat exp } -> '2.718281828459045*090795598298427648842334747314453125' (64 bits) |
|
154 |
'%60.58f' printf:{ 1 asLongFloat exp } -> '2.718281828459045235*4281681079939403389289509505033493041992' (only 80 valid bits on x86) |
|
155 |
||
22288 | 156 |
'%60.58f' printf:{ 1 asQDouble exp } -> '2.71828182845904523536028747135266249775724709369995957496698' (>200 bits) |
21842 | 157 |
|
158 |
correct value is: 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746 |
|
159 |
||
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
160 |
Bulk Containers: |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
161 |
================ |
21771 | 162 |
If you have a vector or matrix (and especially: large ones) of floating point numbers, the well known |
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
163 |
Array is a very inperformant choice. The reason is that it keeps pointers to each of its elements, and each element |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
164 |
(if it is a float) is itself stored somewhere in the object memory. |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
165 |
Thus, there is both a space overhead (every float object has an object header, for class and other information), and |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
166 |
also a performance overhead (extra indirection, cache misses and alignment inefficiencies). |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
167 |
For this, the bulk numeric containers are provided, which keep the elements unboxed and properly aligned. |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
168 |
Use them for matrices and large numeric vectors. They also provide some optimized bulk operation methods, |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
169 |
such as adding, multiplying etc. |
22787 | 170 |
Take a look at FloatArray, DoubleArray, HalfFloatArray etc. |
21771 | 171 |
|
21811 | 172 |
|
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
173 |
Comparing Floats: |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
174 |
================= |
22787 | 175 |
Due to rounding errors (usually on the last bit(s)), you shalt not compare two floating point numbers |
176 |
using the #= operator. For example, the value 0.1 cannot be represented as a sum of powers-of-two fractions, |
|
21811 | 177 |
and will therefore always be an approximation with a half bit error in the last bit of the mantissa. |
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
178 |
Usually, the print functions take this into consideration and return a (faked) '0.1'. |
22787 | 179 |
However, this half bit error may accumulate, for example, when multiplying that by 0.1 then by 100, |
180 |
the error may get large enough to be no longer pushed under the rug by the print function, |
|
181 |
and you will get '0.9999999999999' from it. |
|
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
182 |
|
22787 | 183 |
Also, comparing against a proper 1.0 (which is representable as an exact power of 2), |
184 |
you will get a false result. |
|
185 |
i.e. (0.1 * 0.1 * 100 ~= 1.0) and (0.1 * 0.1 * 100 - 1.0) ~= 0.0 |
|
21771 | 186 |
This often confuses non-computer scientists (and occasionally even some of those). |
22787 | 187 |
|
188 |
For this, you should always provide an epsilon value, when comparing two non-integer numbers. |
|
189 |
The epsilon value is the distance you accept two number to be apart to be still considered equal. |
|
190 |
Effectively the epsilon says are those nearer than this epsilon?. |
|
191 |
||
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
192 |
Now we could say is the delta between two numbers smaller than 0.00001, |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
193 |
and get a reasonable answer for big numbers. But what if we compare two tiny numbers? |
22787 | 194 |
Then a reasonable epsilon must also be much smaller!! |
195 |
||
17206
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
196 |
Actually, the epsilon should always be computed dynamically depending on the two values compared. |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
197 |
That is what the #isAlmostEqualTo:nEpsilon: method does for you. It does not take an absolute epsilon, |
83666440217a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16781
diff
changeset
|
198 |
but instead the number of distinct floating point numbers that the two compared floats may be apart. |
22787 | 199 |
That is: the number of actually representable numbers between those two. |
200 |
Effectively, that is the difference between the two mantissas, |
|
201 |
when the numbers are scaled to the same exponent, taking the number of mantissa bits into account. |
|
7405 | 202 |
|
1295 | 203 |
[author:] |
204 |
Claus Gittinger |
|
1893 | 205 |
|
206 |
[see also:] |
|
207 |
Fraction FixedPoint |
|
88 | 208 |
" |
209 |
! ! |
|
55 | 210 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
211 |
!LimitedPrecisionReal class methodsFor:'instance creation'! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
212 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
213 |
fromInteger:anInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
214 |
"return a float with anInteger's value. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
215 |
Since floats have a limited precision, you usually loose bits when doing this |
21811 | 216 |
with a large integer |
217 |
(i.e. when numDigits is above the flt. pnt number's precision) |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
218 |
(see Float decimalPrecision, LongFloat decimalPrecision." |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
219 |
|
9406 | 220 |
|newFloat sign absVal completeAbsVal delta mask| |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
221 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
222 |
newFloat := self zero. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
223 |
sign := anInteger sign. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
224 |
(sign ~~ 0) ifTrue:[ |
9406 | 225 |
"be careful to do round to nearest even floating point value - |
226 |
thanks to Nicolas Cellier for this code" |
|
227 |
||
228 |
absVal := anInteger abs. |
|
229 |
delta := absVal highBit - self precision. |
|
230 |
delta > 0 ifTrue: [ |
|
231 |
completeAbsVal := absVal. |
|
232 |
"eliminate insignificant/trailing bits" |
|
22212 | 233 |
absVal := absVal rightShift: delta. |
9406 | 234 |
"inexact := trailingBits ~= 0. |
235 |
Round to nearest even" |
|
236 |
(completeAbsVal bitAt:delta) ~= 0 ifTrue:[ |
|
237 |
mask := (1 bitShift:delta-1) - 1. |
|
238 |
((completeAbsVal bitAnd:mask) ~= 0 or:[absVal odd]) ifTrue:[ |
|
239 |
absVal := absVal + 1 |
|
240 |
]. |
|
241 |
]. |
|
242 |
] ifFalse: [ |
|
243 |
delta := 0 |
|
244 |
]. |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
245 |
absVal digitLength to:1 by:-1 do:[:i | |
9406 | 246 |
newFloat := (newFloat * 256) + (absVal digitByteAt:i). |
247 |
]. |
|
248 |
delta ~~ 0 ifTrue: [ |
|
249 |
newFloat := newFloat timesTwoPower:delta. |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
250 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
251 |
(sign < 0) ifTrue:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
252 |
newFloat := newFloat negated |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
253 |
] |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
254 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
255 |
^ newFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
256 |
|
9406 | 257 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
258 |
" |
9406 | 259 |
ShortFloat fromInteger:2 |
260 |
12345678901234567890 asShortFloat |
|
261 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
262 |
1234567890 asFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
263 |
1234567890 asFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
264 |
-1234567890 asFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
265 |
|
9406 | 266 |
12345678901234567890 asFloat storeString |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
267 |
12345678901234567890 asFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
268 |
-12345678901234567890 asFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
269 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
270 |
12345678901234567890 asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
271 |
12345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
272 |
-12345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
273 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
274 |
123456789012345678901234567890 asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
275 |
123456789012345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
276 |
-123456789012345678901234567890 asLongFloat asInteger |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
277 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
278 |
1234567890123456789012345678901234567890 asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
279 |
1234567890123456789012345678901234567890 asLongFloat asInteger |
9406 | 280 |
-1234567890123456789012345678901234567890 asLongFloat asInteger |
281 |
||
282 |
'this test is on 65 bits'. |
|
283 |
self assert: 16r1FFFFFFFFFFFF0801 asDouble ~= 16r1FFFFFFFFFFFF0800 asDouble. |
|
284 |
'this test is on 64 bits'. |
|
285 |
self assert: 16r1FFFFFFFFFFFF0802 asDouble ~= 16r1FFFFFFFFFFFF0800 asDouble. |
|
286 |
'nearest even is upper'. |
|
287 |
self assert: 16r1FFFFFFFFFFF1F800 asDouble = 16r1FFFFFFFFFFF20000 asDouble. |
|
288 |
'nearest even is lower'. |
|
289 |
self assert: 16r1FFFFFFFFFFFF0800 asDouble = 16r1FFFFFFFFFFFF0000 asDouble. |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
290 |
" |
21811 | 291 |
|
22212 | 292 |
"Modified: / 25-08-2017 / 12:34:44 / cg" |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
293 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
294 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
295 |
fromLimitedPrecisionReal:anLPReal |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
296 |
"return a float with anLPReals value. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
297 |
You might loose bits when doing this. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
298 |
Slow fallback." |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
299 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
300 |
|fract| |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
301 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
302 |
anLPReal isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
303 |
anLPReal isNaN ifTrue:[^ self NaN]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
304 |
anLPReal negative ifTrue:[^ self negativeInfinity]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
305 |
^ self infinity |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
306 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
307 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
308 |
fract := anLPReal asTrueFraction. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
309 |
^ (self fromInteger:fract numerator) / (self fromInteger:fract denominator) |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
310 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
311 |
|
9406 | 312 |
fromNumerator:numerator denominator:denominator |
313 |
"Create a limited precision real from a Rational. |
|
314 |
This version will answer the nearest flotaing point value, |
|
315 |
according to IEEE 754 round to nearest even default mode" |
|
316 |
||
16444 | 317 |
|a b q r exponent floatExponent n ha hb hq q1 eMin precision nSubHq| |
9406 | 318 |
|
319 |
a := numerator abs. |
|
320 |
b := denominator abs. |
|
321 |
ha := a highBit. |
|
322 |
hb := b highBit. |
|
323 |
||
324 |
precision := self precision. |
|
325 |
||
326 |
"If both numerator and denominator are represented exactly in floating point number, |
|
327 |
then fastest thing to do is to use hardwired float division" |
|
328 |
(ha <= precision and:[hb <= precision]) ifTrue: [ |
|
16444 | 329 |
^ (self coerce:numerator) / (self coerce:denominator) |
9406 | 330 |
]. |
331 |
||
332 |
"Try and obtain a mantissa with 1 bit excess precision. |
|
333 |
First guess is rough, we might get one more bit or one less" |
|
334 |
exponent := ha - hb - precision - 1. |
|
335 |
exponent > 0 |
|
336 |
ifTrue: [b := b bitShift: exponent] |
|
22212 | 337 |
ifFalse: [a := a rightShift: exponent]. |
9406 | 338 |
q := a quo: b. |
339 |
r := a - (q * b). |
|
340 |
hq := q highBit. |
|
341 |
||
342 |
"check for gradual underflow, in which case we should use less bits" |
|
343 |
floatExponent := exponent + hq - 1. |
|
344 |
eMin := 2 - (1 bitShift: (self numBitsInExponent - 1)). |
|
345 |
n := floatExponent >= eMin |
|
346 |
ifTrue: [precision + 1] |
|
347 |
ifFalse: [precision + 1 + floatExponent - eMin]. |
|
348 |
||
349 |
nSubHq := n - hq. |
|
350 |
||
351 |
nSubHq < 0 ifTrue: [ |
|
352 |
exponent := exponent + hq - n. |
|
353 |
r := (q bitAnd: (1 bitShift:nSubHq) - 1) * b + r. |
|
354 |
q := q bitShift:nSubHq |
|
355 |
] ifFalse:[nSubHq > 0 ifTrue: [ |
|
356 |
exponent := exponent + hq - n. |
|
357 |
q1 := (r bitShift:nSubHq) quo: b. |
|
358 |
q := (q bitShift:nSubHq) bitAnd: q1. |
|
359 |
r := r - (q1 * b) |
|
360 |
]]. |
|
361 |
||
362 |
"check if we should round upward. |
|
363 |
The case of exact half (q bitAnd: 1) isZero not & (r isZero) |
|
364 |
will be handled by self fromInteger: conversion" |
|
365 |
((q bitAnd:1) ~~ 0 and:[r ~~ 0]) ifTrue:[ |
|
366 |
q := q + 1 |
|
367 |
]. |
|
368 |
||
369 |
(numerator negative xor:denominator negative) ifTrue: [ |
|
370 |
q := q negated. |
|
371 |
]. |
|
372 |
||
16444 | 373 |
^ (self coerce:q) timesTwoPower:exponent |
9406 | 374 |
|
375 |
" |
|
376 |
Time millisecondsToRun:[ |
|
377 |
1000000 timesRepeat:[ |
|
378 |
Float fromNumerator:12345678901234567890 denominator:987654321 |
|
379 |
]. |
|
380 |
] |
|
381 |
||
382 |
|fraction| |
|
383 |
fraction := 12345678901234567890//987654321. |
|
384 |
Time millisecondsToRun:[ |
|
385 |
1000000 timesRepeat:[ |
|
386 |
fraction asFloat |
|
387 |
]. |
|
388 |
] |
|
389 |
" |
|
22212 | 390 |
|
391 |
"Modified: / 25-08-2017 / 12:34:55 / cg" |
|
9406 | 392 |
! |
393 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
394 |
new:aNumber |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
395 |
"catch this message - not allowed for floats/doubles" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
396 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
397 |
self error:'Floats/Doubles cannot be created with new:' |
16444 | 398 |
! |
399 |
||
400 |
readFrom:aStringOrStream onError:exceptionBlock |
|
401 |
"read a float from a string" |
|
402 |
||
403 |
|num| |
|
404 |
||
21270 | 405 |
num := super readFrom:aStringOrStream onError:[^ exceptionBlock value]. |
16444 | 406 |
^ self coerce:num |
407 |
||
408 |
" |
|
409 |
Float readFrom:'.1' |
|
410 |
Float readFrom:'0.1' |
|
411 |
Float readFrom:'0' |
|
412 |
||
413 |
ShortFloat readFrom:'.1' |
|
414 |
ShortFloat readFrom:'0.1' |
|
415 |
ShortFloat readFrom:'0' |
|
416 |
||
417 |
LongFloat readFrom:'.1' |
|
418 |
LongFloat readFrom:'0.1' |
|
419 |
LongFloat readFrom:'0' |
|
21270 | 420 |
|
421 |
LimitedPrecisionReal readFrom:'bla' onError:nil |
|
422 |
Float readFrom:'bla' onError:nil |
|
423 |
ShortFloat readFrom:'bla' onError:nil |
|
16444 | 424 |
" |
425 |
||
426 |
"Created: / 07-01-1998 / 16:17:19 / cg" |
|
427 |
"Modified: / 23-11-2010 / 14:35:41 / cg" |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
428 |
! ! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
429 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
430 |
!LimitedPrecisionReal class methodsFor:'class initialization'! |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
431 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
432 |
initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
433 |
"initialize ANSI compliant float globals" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
434 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
435 |
Smalltalk at:#FloatE put:ShortFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
436 |
Smalltalk at:#FloatD put:Float. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
437 |
Smalltalk at:#FloatQ put:LongFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
438 |
|
21896 | 439 |
"/ not defined in ANSI, but consistent in its naming |
440 |
Smalltalk at:#FloatQD put:QDouble. |
|
441 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
442 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
443 |
self initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
444 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
445 |
|
21896 | 446 |
"Created: / 07-09-2001 / 14:02:45 / cg" |
447 |
"Modified (comment): / 21-06-2017 / 13:59:08 / cg" |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
448 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
449 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
450 |
!LimitedPrecisionReal class methodsFor:'constants & defaults'! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
451 |
|
7390 | 452 |
NaN |
21825 | 453 |
"return the constant NaN (not a Number) in my representation." |
7390 | 454 |
|
18281 | 455 |
|zero| |
456 |
||
457 |
zero := self zero. |
|
458 |
^ zero uncheckedDivide:zero. |
|
7390 | 459 |
|
460 |
" |
|
461 |
ShortFloat NaN |
|
462 |
Float NaN |
|
463 |
LongFloat NaN |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
464 |
LargeFloat NaN |
7390 | 465 |
" |
21825 | 466 |
|
467 |
"Modified (comment): / 16-06-2017 / 11:03:28 / cg" |
|
7390 | 468 |
! |
469 |
||
16663 | 470 |
computeEpsilon |
471 |
"return the maximum relative spacing" |
|
472 |
||
22852 | 473 |
^ (self coerce:self radix) raisedToInteger:(1 - self precision). |
16663 | 474 |
|
475 |
" |
|
21946 | 476 |
Float radix |
477 |
Float precision |
|
478 |
||
16663 | 479 |
Float computeEpsilon |
21946 | 480 |
ShortFloat computeEpsilon |
16663 | 481 |
LongFloat computeEpsilon |
21946 | 482 |
QDouble epsilon |
16663 | 483 |
" |
21946 | 484 |
|
485 |
"Modified (comment): / 03-07-2017 / 12:12:06 / cg" |
|
22852 | 486 |
"Modified: / 10-05-2018 / 01:19:25 / stefan" |
16663 | 487 |
! |
488 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
489 |
emax |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
490 |
"return the largest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
491 |
|
17655
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
492 |
^ (1 << (self numBitsInExponent-1)) - 1 |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
493 |
|
17655
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
494 |
" |
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
495 |
Float emax |
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
496 |
ShortFloat emax |
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
497 |
" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
498 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
499 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
500 |
emin |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
501 |
"return the smallest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
502 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
503 |
^ self subclassResponsibility |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
504 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
505 |
" |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
506 |
Float emin |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
507 |
ShortFloat emin |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
508 |
" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
509 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
510 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
511 |
epsilon |
21802 | 512 |
"return the maximum relative spacing of instances of mySelf |
513 |
(i.e. the value-delta of the least significant bit)" |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
514 |
|
9406 | 515 |
^ self radix asFloat raisedToInteger:(1 - self precision) |
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
516 |
|
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
517 |
" |
22851 | 518 |
Float epsilon -> 2.22044604925031E-16 |
519 |
ShortFloat epsilon -> 1.192093e-07 |
|
520 |
LongFloat epsilon -> 1.084202172485504434E-19 |
|
521 |
QDouble epsilon -> 1.21543267145725E-63 |
|
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
522 |
" |
21802 | 523 |
|
524 |
"Modified (comment): / 12-06-2017 / 18:52:31 / cg" |
|
22851 | 525 |
"Modified (comment): / 10-05-2018 / 01:08:48 / stefan" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
526 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
527 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
528 |
fmax |
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
529 |
"The largest value allowed by instances of this class." |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
530 |
|
22499 | 531 |
|radix radixReal| |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
532 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
533 |
radix := self radix. |
22499 | 534 |
radixReal := (self fromInteger: radix). |
21807 | 535 |
^ ((self fromInteger: 1) - |
22499 | 536 |
(radixReal timesTwoPower: self precision negated - 1)) * radix |
537 |
* (radixReal timesTwoPower: self emax - 1) |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
538 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
539 |
" |
20978 | 540 |
Float fmax |
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
541 |
ShortFloat fmax |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
542 |
LongFloat fmax |
21807 | 543 |
QDouble fmax |
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
544 |
" |
20978 | 545 |
|
21807 | 546 |
"Modified (comment): / 14-06-2017 / 19:17:39 / cg" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
547 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
548 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
549 |
fmin |
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
550 |
"The smallest value allowed by instances of this class." |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
551 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
552 |
^ self subclassResponsibility |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
553 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
554 |
|
7390 | 555 |
infinity |
21861 | 556 |
"return an instance of myself which represents positive infinity" |
7390 | 557 |
|
558 |
^ self unity uncheckedDivide:self zero |
|
559 |
||
560 |
" |
|
561 |
ShortFloat infinity |
|
562 |
Float infinity |
|
563 |
LongFloat infinity |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
564 |
LargeFloat infinity |
7390 | 565 |
" |
21861 | 566 |
|
567 |
"Modified (comment): / 20-06-2017 / 13:46:52 / cg" |
|
7390 | 568 |
! |
569 |
||
19359 | 570 |
maxSmallInteger |
22610 | 571 |
"answer the largest possible SmallInteger value as instance of myself" |
19359 | 572 |
|
573 |
^ self fromInteger:SmallInteger maxVal. |
|
574 |
||
575 |
" |
|
21861 | 576 |
Float maxSmallInteger. |
577 |
LongFloat maxSmallInteger. |
|
578 |
ShortFloat maxSmallInteger. |
|
579 |
QDouble maxSmallInteger. |
|
19359 | 580 |
" |
21861 | 581 |
|
582 |
"Modified (comment): / 20-06-2017 / 14:07:27 / cg" |
|
22610 | 583 |
"Modified (comment): / 14-03-2018 / 20:11:28 / mawalch" |
19359 | 584 |
! |
585 |
||
7390 | 586 |
negativeInfinity |
21861 | 587 |
"return an instance of myself which represents negative infinity" |
7390 | 588 |
|
589 |
^ self unity negated uncheckedDivide:self zero |
|
590 |
||
591 |
" |
|
592 |
ShortFloat negativeInfinity |
|
593 |
Float negativeInfinity |
|
594 |
LongFloat negativeInfinity |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
595 |
LargeFloat negativeInfinity |
7390 | 596 |
" |
21861 | 597 |
|
598 |
"Modified (comment): / 20-06-2017 / 13:46:42 / cg" |
|
7356 | 599 |
! ! |
600 |
||
601 |
!LimitedPrecisionReal class methodsFor:'queries'! |
|
602 |
||
603 |
decimalPrecision |
|
604 |
"return the number of valid decimal digits" |
|
605 |
||
16663 | 606 |
^ ((self precision + 1) / (10.0 log:self radix)) ceiling "use ceiling, since the last decimal digit contains some precision" |
7356 | 607 |
|
608 |
" |
|
609 |
ShortFloat decimalPrecision |
|
610 |
Float decimalPrecision |
|
611 |
LongFloat decimalPrecision |
|
612 |
" |
|
613 |
! |
|
614 |
||
21825 | 615 |
defaultPrintPrecision |
616 |
"return the number of decimal digits printed by default" |
|
617 |
||
618 |
^ 6 |
|
619 |
||
620 |
" |
|
621 |
ShortFloat defaultPrintPrecision |
|
622 |
Float defaultPrintPrecision |
|
623 |
LongFloat defaultPrintPrecision |
|
624 |
" |
|
625 |
||
626 |
"Created: / 17-06-2017 / 02:56:43 / cg" |
|
627 |
! |
|
628 |
||
12120 | 629 |
denormalized |
630 |
"Return whether the instances of this class can |
|
631 |
represent values in denormalized format." |
|
632 |
||
633 |
^ true |
|
634 |
! |
|
635 |
||
20058 | 636 |
hasSharedInstances |
637 |
"return true if this class has shared instances, that is, instances |
|
638 |
with the same value are identical. |
|
639 |
Although not really shared, floats should be treated |
|
640 |
so, to be independent of the implementation of the arithmetic methods." |
|
641 |
||
642 |
^ true |
|
643 |
||
644 |
||
645 |
! |
|
646 |
||
8892 | 647 |
isAbstract |
11225 | 648 |
"Return if this class is an abstract class. |
649 |
True is returned for LimitedPrecisionReal here; false for subclasses." |
|
650 |
||
8892 | 651 |
^ self == LimitedPrecisionReal |
652 |
||
653 |
" |
|
654 |
1.0 class isAbstract |
|
655 |
" |
|
656 |
! |
|
657 |
||
17221 | 658 |
isIEEEFormat |
659 |
"return true, if this machine represents floats in IEEE format. |
|
660 |
Currently, no support is provided for non-ieee machines |
|
661 |
to convert their floats into this (which is only relevant, |
|
662 |
if such a machine wants to send floats as binary to some other |
|
663 |
machine). |
|
664 |
Machines with non-IEEE format are VAXed and IBM370-type systems |
|
665 |
(among others). Today, most systems use IEEE format floats." |
|
666 |
||
667 |
%{ |
|
668 |
#ifdef __s390__ |
|
669 |
RETURN(false); |
|
670 |
#endif |
|
671 |
%}. |
|
672 |
||
673 |
^ true "/ this may be a lie |
|
674 |
! |
|
675 |
||
7356 | 676 |
numBitsInExponent |
677 |
"return the number of bits in the exponent" |
|
678 |
||
679 |
^ self subclassResponsibility |
|
680 |
! |
|
681 |
||
682 |
numBitsInIntegerPart |
|
16781
4619d7da013a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16731
diff
changeset
|
683 |
"answer the number of bits in the integer part of the mantissa. |
4619d7da013a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16731
diff
changeset
|
684 |
Most floating point formats are normalized to get rid of the extra |
4619d7da013a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16731
diff
changeset
|
685 |
bit." |
4619d7da013a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16731
diff
changeset
|
686 |
|
7356 | 687 |
^ 0 |
688 |
! |
|
689 |
||
690 |
numBitsInMantissa |
|
691 |
"return the number of bits in the mantissa |
|
692 |
(typically 1 less than the precision due to the hidden bit)" |
|
693 |
||
694 |
^ self subclassResponsibility |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
695 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
696 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
697 |
precision |
7356 | 698 |
"return the number of valid mantissa bits" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
699 |
|
7356 | 700 |
^ self numBitsInMantissa + 1 - self numBitsInIntegerPart |
21802 | 701 |
|
702 |
" |
|
703 |
HalfFloat precision |
|
704 |
ShortFloat precision |
|
705 |
Float precision |
|
706 |
LongFloat precision |
|
707 |
QDouble precision |
|
708 |
" |
|
709 |
||
710 |
"Modified (comment): / 12-06-2017 / 18:47:54 / cg" |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
711 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
712 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
713 |
radix |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
714 |
"return the radix (base)" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
715 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
716 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
717 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
718 |
"Created: / 7.9.2001 / 14:08:20 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
719 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
720 |
|
55 | 721 |
!LimitedPrecisionReal methodsFor:'accessing'! |
722 |
||
723 |
at:index |
|
724 |
"redefined to prevent access to individual bytes in a real." |
|
725 |
||
726 |
self error:'not allowed for floats/doubles' |
|
727 |
! |
|
728 |
||
729 |
at:index put:aValue |
|
730 |
"redefined to prevent access to individual bytes in a real" |
|
731 |
||
732 |
self error:'not allowed for floats/doubles' |
|
733 |
! ! |
|
734 |
||
735 |
!LimitedPrecisionReal methodsFor:'arithmetic'! |
|
736 |
||
701 | 737 |
* aNumber |
11727 | 738 |
"return the product of the receiver and the argument." |
701 | 739 |
|
1200 | 740 |
"/ as soon as Float are float & Double are doubles, |
741 |
"/ use: |
|
742 |
"/ ^ aNumber productFromDouble:self asDouble |
|
743 |
||
744 |
^ aNumber productFromFloat:self asFloat |
|
745 |
||
746 |
"Modified: 17.4.1996 / 12:35:36 / cg" |
|
701 | 747 |
! |
748 |
||
55 | 749 |
+ aNumber |
750 |
"return the sum of the receiver and the argument, aNumber" |
|
751 |
||
1200 | 752 |
"/ as soon as Float are float & Double are doubles, |
753 |
"/ use: |
|
754 |
"/ ^ aNumber sumFromDouble:self asDouble |
|
755 |
||
756 |
^ aNumber sumFromFloat:self asFloat |
|
757 |
||
758 |
"Modified: 17.4.1996 / 12:35:55 / cg" |
|
55 | 759 |
! |
760 |
||
761 |
- aNumber |
|
762 |
"return the difference of the receiver and the argument, aNumber" |
|
763 |
||
1200 | 764 |
"/ as soon as Float are float & Double are doubles, |
765 |
"/ use: |
|
766 |
"/ ^ aNumber differenceFromDouble:self asDouble |
|
767 |
||
768 |
^ aNumber differenceFromFloat:self asFloat |
|
769 |
||
770 |
"Modified: 17.4.1996 / 12:36:07 / cg" |
|
55 | 771 |
! |
772 |
||
773 |
/ aNumber |
|
774 |
"return the quotient of the receiver and the argument, aNumber" |
|
775 |
||
776 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
7141
033f2c26d8e6
replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents:
6890
diff
changeset
|
777 |
^ ZeroDivide raiseRequestWith:thisContext. |
55 | 778 |
]. |
1200 | 779 |
"/ as soon as Float are float & Double are doubles, |
780 |
"/ use: |
|
781 |
"/ ^ aNumber quotientFromDouble:self asDouble |
|
782 |
||
783 |
^ aNumber quotientFromFloat:self asFloat |
|
784 |
||
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
3898
diff
changeset
|
785 |
"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
|
786 |
"Modified: / 26.7.1999 / 10:46:11 / stefan" |
55 | 787 |
! |
788 |
||
789 |
// aNumber |
|
790 |
"return the integer quotient of dividing the receiver by aNumber with |
|
1882 | 791 |
truncation towards negative infinity." |
55 | 792 |
|
793 |
^ (self / aNumber) floor asInteger |
|
1882 | 794 |
|
795 |
"Modified: 5.11.1996 / 11:45:37 / cg" |
|
7356 | 796 |
! |
797 |
||
798 |
ceiling |
|
7398 | 799 |
^ self asTrueFraction ceiling |
7356 | 800 |
! |
801 |
||
802 |
floor |
|
7398 | 803 |
^ self asTrueFraction floor |
9406 | 804 |
! |
805 |
||
806 |
timesTwoPower:anInteger |
|
807 |
"multiply self by a power of two. |
|
808 |
Implementation takes care of preserving class and avoiding overflow/underflow |
|
809 |
Thanks to Nicolas Cellier for this code" |
|
810 |
||
12761 | 811 |
|half two h1 h2| |
9406 | 812 |
|
16444 | 813 |
two := self coerce:2.0. |
9406 | 814 |
|
815 |
anInteger abs highBit >= (self class numBitsInExponent - 2) ifTrue:[ |
|
816 |
half := anInteger // 2. |
|
12761 | 817 |
|
818 |
anInteger even ifTrue:[ |
|
819 |
"/ halves are equal |
|
820 |
h2 := h1 := (two raisedToInteger: half). |
|
821 |
] ifFalse:[ |
|
822 |
h1 := (two raisedToInteger: half). |
|
823 |
anInteger > 0 ifTrue:[ |
|
824 |
h2 := h1 * two. |
|
825 |
] ifFalse:[ |
|
826 |
h2 := (two raisedToInteger: anInteger - half). |
|
827 |
]. |
|
828 |
]. |
|
829 |
^ (self * h1) * h2 |
|
9406 | 830 |
] ifFalse: [ |
831 |
^ self * (two raisedToInteger: anInteger). |
|
832 |
] |
|
833 |
||
12758 | 834 |
" |
835 |
(1 asShortFloat timesTwoPower: 3) class = ShortFloat. |
|
836 |
(1 asLongFloat timesTwoPower: 1024). |
|
837 |
(1 asFloat timesTwoPower: -1024) timesTwoPower: 1024. |
|
12761 | 838 |
|
839 |
(2.0 asShortFloat timesTwoPower: -150) timesTwoPower: 150 |
|
840 |
(2.0 asLongFloat timesTwoPower: -150) timesTwoPower: 150 |
|
841 |
(2.0 asFloat timesTwoPower: -150) timesTwoPower: 150 |
|
842 |
||
843 |
(2.0 asShortFloat timesTwoPower: -149) timesTwoPower: 149 |
|
844 |
(2.0 asLongFloat timesTwoPower: -149) timesTwoPower: 149 |
|
845 |
(2.0 asFloat timesTwoPower: -149) timesTwoPower: 149 |
|
846 |
||
847 |
Time millisecondsToRun:[ |
|
848 |
1000000 timesRepeat:[ |
|
849 |
(2.0 timesTwoPower: 150) |
|
850 |
] |
|
851 |
] |
|
12758 | 852 |
" |
55 | 853 |
! ! |
854 |
||
17221 | 855 |
!LimitedPrecisionReal methodsFor:'bytes access'! |
856 |
||
857 |
digitBytes |
|
858 |
"answer the float's digit bytes im IEEE format. |
|
859 |
Use the native machine byte ordering." |
|
860 |
||
861 |
|sz "{ Class:SmallInteger }" |
|
862 |
bytes| |
|
863 |
||
864 |
sz := self basicSize. |
|
865 |
bytes := ByteArray new:sz. |
|
866 |
1 to:sz do:[:i| |
|
867 |
bytes at:i put:(self basicAt:i). |
|
868 |
]. |
|
869 |
^ bytes |
|
870 |
||
871 |
" |
|
872 |
Float pi digitBytes |
|
873 |
ShortFloat pi digitBytes |
|
874 |
" |
|
875 |
! |
|
876 |
||
877 |
digitBytesMSB:msb |
|
878 |
"answer the float's digit bytes im IEEE format. |
|
879 |
If msb == true, use MSB byte order, otherwise LSB byte order." |
|
880 |
||
881 |
|sz "{ Class:SmallInteger }" |
|
882 |
bytes| |
|
883 |
||
884 |
self class isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
885 |
||
886 |
sz := self basicSize. |
|
887 |
bytes := ByteArray new:sz. |
|
888 |
||
889 |
(UninterpretedBytes isBigEndian == msb) ifTrue:[ |
|
890 |
1 to:sz do:[:i| |
|
891 |
bytes at:i put:(self basicAt:i). |
|
892 |
]. |
|
893 |
] ifFalse:[ |
|
894 |
"reverse the bytes" |
|
895 |
1 to:sz do:[:i| |
|
896 |
bytes at:sz-i+1 put:(self basicAt:i). |
|
897 |
]. |
|
898 |
]. |
|
899 |
^ bytes |
|
900 |
||
901 |
" |
|
902 |
Float pi digitBytesMSB:false |
|
903 |
Float pi digitBytesMSB:true |
|
904 |
ShortFloat pi digitBytesMSB:false |
|
905 |
ShortFloat pi digitBytesMSB:true |
|
906 |
" |
|
907 |
! ! |
|
908 |
||
5238 | 909 |
!LimitedPrecisionReal methodsFor:'coercing & converting'! |
55 | 910 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
911 |
asFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
912 |
^ Float fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
913 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
914 |
|
55 | 915 |
asFraction |
5883 | 916 |
"Answer a rational number (Integer or Fraction) representing the receiver. |
917 |
This conversion uses the continued fraction method to approximate |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
918 |
a floating point number. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
919 |
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
|
920 |
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
|
921 |
" |
5883 | 922 |
|
923 |
|num1 denom1 num2 denom2 int frac newD temp limit| |
|
55 | 924 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
925 |
self isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
926 |
"/ ^ self asMetaNumber. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
927 |
^ self class |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
928 |
raise:#domainErrorSignal |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
929 |
receiver:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
930 |
selector:#asFraction |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
931 |
arguments:#() |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
932 |
errorString:'Cannot represent non-finite number as fraction' |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
933 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
934 |
|
7394 | 935 |
limit := 10 raisedToInteger:self defaultNumberOfDigits. |
5883 | 936 |
num1 := self truncated. |
937 |
denom1 := 1. "The first of two alternating denominators" |
|
938 |
num2 := 1. "The second numerator" |
|
939 |
denom2 := 0. "The second denominator--will update" |
|
940 |
int := num1. "The integer part of self" |
|
941 |
frac := self fractionPart. |
|
942 |
[frac = 0] whileFalse:[ |
|
943 |
newD := 1.0 / frac. |
|
944 |
int := newD truncated. |
|
945 |
frac := newD fractionPart. "save the fractional part for next time" |
|
946 |
temp := num2. "old numerator and save it" |
|
947 |
num2 := num1. |
|
948 |
num1 := num1 * int + temp. "Update first numerator" |
|
949 |
temp := denom2. "old denominator and save it" |
|
950 |
denom2 := denom1. |
|
951 |
denom1 := int * denom1 + temp. "Update first denominator" |
|
952 |
limit < denom1 ifTrue:[ |
|
953 |
"Is ratio past float precision? If so, pick which of the two ratios to use" |
|
954 |
num2 = 0.0 ifTrue:[ |
|
955 |
"Is second denominator 0?" |
|
956 |
^ Fraction numerator:num1 denominator:denom1 |
|
957 |
]. |
|
958 |
^ Fraction numerator:num2 denominator:denom2 |
|
959 |
] |
|
3060 | 960 |
]. |
961 |
||
5883 | 962 |
"If fractional part is zero, return the first ratio" |
963 |
denom1 = 1 ifTrue:[ |
|
964 |
"Am i really an Integer?" |
|
965 |
^ num1 "Yes, return Integer result" |
|
3060 | 966 |
]. |
5883 | 967 |
"Otherwise return Fraction result" |
968 |
^ Fraction numerator:num1 denominator:denom1 |
|
55 | 969 |
|
3060 | 970 |
" |
5883 | 971 |
1.1 asFraction |
972 |
1.2 asFraction |
|
973 |
0.3 asFraction |
|
7394 | 974 |
0.5 asFraction |
975 |
(1/5) asFloat asFraction |
|
5883 | 976 |
(1/8) asFloat asFraction |
977 |
(1/13) asFloat asFraction |
|
16663 | 978 |
(1/10) asFloat asFraction |
979 |
(1/10) asFloat asTrueFraction asFixedPoint scale:20 |
|
980 |
3.14159 asFixedPoint scale:20 |
|
981 |
3.14159 storeString |
|
982 |
3.14159 asFraction asFloat storeString |
|
7394 | 983 |
1.3 asFraction |
984 |
1.0 asFraction |
|
985 |
1E6 asFraction |
|
986 |
1E-6 asFraction |
|
3060 | 987 |
" |
2793 | 988 |
|
3060 | 989 |
"Modified: / 25.10.1997 / 16:41:19 / cg" |
55 | 990 |
! |
991 |
||
992 |
asInteger |
|
993 |
"return an integer with same value - might truncate" |
|
994 |
||
3185 | 995 |
|max maxF| |
55 | 996 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
997 |
self isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
998 |
"/ ^ self asMetaNumber. |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
999 |
^ self class |
7418
521174de046d
Nan asInteger raises DomainError
Claus Gittinger <cg@exept.de>
parents:
7405
diff
changeset
|
1000 |
raise:#domainErrorSignal |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
1001 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
1002 |
selector:#asInteger |
6195 | 1003 |
arguments:#() |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1004 |
errorString:'Cannot represent non-finite number as integer' |
2388 | 1005 |
]. |
1006 |
||
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1007 |
self abs < 2e16 ifTrue:[ |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1008 |
"/ NOTICE: this must be redefined in float |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1009 |
"/ subclasses to handle the smallinteger range; |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1010 |
"/ i.e. this may only be invoked for reals |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1011 |
"/ which are NOT within the smallInt range. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1012 |
"/ otherwise, endless recursion is the consequence. |
3426 | 1013 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1014 |
max := SmallInteger maxVal // 2 + 1. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1015 |
maxF := max asFloat. |
3426 | 1016 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1017 |
^ (self quo:maxF) * max + (self rem:maxF) truncated |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1018 |
]. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1019 |
^ self asTrueFraction |
55 | 1020 |
|
3137 | 1021 |
" |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1022 |
12345.0 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1023 |
1e15 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1024 |
1e33 asInteger asFloat |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1025 |
1e303 asInteger asFloat |
3137 | 1026 |
" |
2388 | 1027 |
|
6195 | 1028 |
"Modified: / 16.11.2001 / 14:15:33 / cg" |
701 | 1029 |
! |
1030 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1031 |
asLargeFloat |
23054 | 1032 |
"return a large float with (approximately) my value. |
1033 |
If the largeFloat class is not present, a regular float is returned" |
|
21802 | 1034 |
|
23056 | 1035 |
^ (LargeFloat ? LongFloat ? Float) fromLimitedPrecisionReal:self |
21802 | 1036 |
|
1037 |
"Modified (comment): / 12-06-2017 / 20:57:05 / cg" |
|
23056 | 1038 |
"Modified: / 07-06-2018 / 16:28:03 / Claus Gittinger" |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1039 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1040 |
|
7378 | 1041 |
asLimitedPrecisionReal |
1042 |
"return a float of any precision with same value" |
|
1043 |
||
1044 |
^ self |
|
1045 |
! |
|
1046 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1047 |
asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1048 |
^ LongFloat fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1049 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1050 |
|
5883 | 1051 |
asRational |
1052 |
"Answer a Rational number--Integer or Fraction--representing the receiver. |
|
1053 |
Same as asFraction fro st-80 compatibility." |
|
1054 |
||
1055 |
^ self asFraction |
|
1056 |
||
1057 |
" |
|
1058 |
1.1 asRational |
|
1059 |
1.2 asRational |
|
1060 |
0.3 asRational |
|
1061 |
0.5 asRational |
|
1062 |
(1/5) asFloat asRational |
|
1063 |
(1/8) asFloat asRational |
|
1064 |
(1/13) asFloat asRational |
|
1065 |
3.14159 asRational |
|
1066 |
3.14159 asRational asFloat |
|
1067 |
1.3 asRational |
|
1068 |
1.0 asRational |
|
1069 |
" |
|
1070 |
! |
|
1071 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1072 |
asShortFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1073 |
^ ShortFloat fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1074 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1075 |
|
7356 | 1076 |
asTrueFraction |
1077 |
"Answer a fraction or integer that EXACTLY represents self, |
|
1078 |
an any-precision IEEE floating point number, consisting of: |
|
1079 |
numMantissaBits bits of normalized mantissa (i.e. with hidden leading 1-bit) |
|
1080 |
optional numExtraBits between mantissa and exponent (normalized flag for ext-real) |
|
1081 |
numExponentBits bits of 2s complement exponent |
|
1082 |
1 sign bit. |
|
1083 |
Taken from Floats asTrueFraction" |
|
1084 |
||
1085 |
|shifty sign expPart exp fraction fractionPart result zeroBitsCount |
|
1086 |
numBytes numBits numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
1087 |
biasExponent numIntegerBits| |
|
1088 |
||
1089 |
self isFinite ifFalse:[ |
|
18281 | 1090 |
^ self asMetaNumber |
1091 |
"/ ^ self class |
|
1092 |
"/ raise:#domainErrorSignal |
|
1093 |
"/ receiver:self |
|
1094 |
"/ selector:#asTrueFraction |
|
1095 |
"/ arguments:#() |
|
1096 |
"/ errorString:'Cannot represent non-finite float as a fraction'. |
|
7356 | 1097 |
]. |
1098 |
||
1099 |
"Extract the bits of an IEEE anySize float " |
|
1100 |
numBytes := self basicSize. |
|
16781
4619d7da013a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16731
diff
changeset
|
1101 |
numIntegerBits := self class numBitsInIntegerPart. |
7356 | 1102 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
1103 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
1104 |
numBits := numBitsInMantissa + numBitsInExponent. "not (numBytes * 8 - 1) -- there could be 80 float bits in 96 overall bits" |
|
1105 |
biasExponent := maskExponent bitShift:-1. |
|
1106 |
||
1107 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
1108 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
1109 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
1110 |
] ifFalse:[ |
|
1111 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
1112 |
]. |
|
1113 |
||
1114 |
" Extract the sign and the biased exponent " |
|
1115 |
sign := (shifty bitAt:numBits+1) == 0 ifTrue: [1] ifFalse: [-1]. |
|
22212 | 1116 |
expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent. |
7356 | 1117 |
|
1118 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
1119 |
fractionPart := shifty bitAnd:maskMantissa. |
|
1120 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
701 | 1121 |
|
7356 | 1122 |
numIntegerBits == 0 ifTrue:[ |
1123 |
" Replace omitted leading 1 in fraction (Notice: quadIEEE format does not do this)" |
|
1124 |
fraction := fractionPart bitOr: (maskMantissa + 1). |
|
1125 |
] ifFalse:[ |
|
1126 |
fraction := fractionPart. |
|
1127 |
]. |
|
1128 |
||
7390 | 1129 |
"Unbias exponent" |
7356 | 1130 |
exp := biasExponent - expPart + (numBitsInMantissa - numIntegerBits). |
1200 | 1131 |
|
7390 | 1132 |
" Form the result. When exp>fractionalPrecision, the exponent is adjusted by |
7356 | 1133 |
the number of trailing zero bits in the fraction to minimize |
7390 | 1134 |
the (huge) time otherwise spent in #gcd: of fraction handling code." |
7356 | 1135 |
exp negative ifTrue: [ |
22212 | 1136 |
result := sign * (fraction rightShift: exp) |
7356 | 1137 |
] ifFalse:[ |
1138 |
zeroBitsCount := fraction lowBit - 1. |
|
1139 |
exp := exp - zeroBitsCount. |
|
1140 |
exp <= 0 ifTrue: [ |
|
1141 |
zeroBitsCount := zeroBitsCount + exp. |
|
22212 | 1142 |
result := sign * (fraction rightShift:zeroBitsCount) |
7356 | 1143 |
] ifFalse: [ |
1144 |
result := Fraction |
|
22212 | 1145 |
numerator: (sign * (fraction rightShift: zeroBitsCount)) |
7356 | 1146 |
denominator: (1 bitShift:exp) |
1147 |
] |
|
1148 |
]. |
|
1200 | 1149 |
|
7356 | 1150 |
"Low cost validation omitted after extensive testing" |
1151 |
"(result asFloat = self) ifFalse: [self error: 'asTrueFraction validation failed']." |
|
1152 |
||
1153 |
^ result |
|
1154 |
||
1155 |
" |
|
7390 | 1156 |
0.3 asFloat asTrueFraction |
7356 | 1157 |
0.3 asShortFloat asTrueFraction |
7390 | 1158 |
0.3 asLongFloat asTrueFraction |
1159 |
||
1160 |
1.25 asTrueFraction |
|
7480 | 1161 |
1.25 asShortFloat asTrueFraction |
7390 | 1162 |
0.25 asTrueFraction |
1163 |
-0.25 asTrueFraction |
|
1164 |
3e37 asTrueFraction |
|
1165 |
||
18281 | 1166 |
LongFloat NaN asTrueFraction |
1167 |
LongFloat infinity asTrueFraction |
|
1168 |
LongFloat negativeInfinity asTrueFraction |
|
7390 | 1169 |
" |
22212 | 1170 |
|
1171 |
"Modified: / 25-08-2017 / 12:34:08 / cg" |
|
701 | 1172 |
! ! |
1173 |
||
1174 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
1175 |
||
1176 |
< aNumber |
|
1177 |
"return true, if the argument is greater" |
|
1178 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1179 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1180 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1181 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 1182 |
! ! |
1183 |
||
1184 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
1185 |
||
1186 |
deepCopy |
|
1187 |
"return a deep copy of myself |
|
1188 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1189 |
||
1190 |
^ self |
|
1191 |
! |
|
1192 |
||
10946 | 1193 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
701 | 1194 |
"return a deep copy of myself |
1195 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1196 |
||
1197 |
^ self |
|
1198 |
! |
|
1199 |
||
1200 |
shallowCopy |
|
1201 |
"return a shallow copy of the receiver" |
|
1202 |
||
1203 |
^ self |
|
1204 |
! |
|
1205 |
||
1206 |
simpleDeepCopy |
|
1207 |
"return a deep copy of the receiver |
|
1208 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1209 |
||
1210 |
^ self |
|
55 | 1211 |
! ! |
1212 |
||
1213 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
1214 |
||
1215 |
differenceFromFraction:aFraction |
|
213 | 1216 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 1217 |
|
1218 |
|d| |
|
1219 |
||
1220 |
d := aFraction denominator. |
|
1221 |
^ (aFraction numerator - (self * d)) / d |
|
1222 |
! |
|
1223 |
||
1224 |
productFromFraction:aFraction |
|
213 | 1225 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 1226 |
|
1227 |
^ self * aFraction numerator / aFraction denominator |
|
1228 |
! |
|
1229 |
||
1230 |
quotientFromFraction:aFraction |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1231 |
"Return the quotient of the argument, aFraction and the receiver. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1232 |
Sent when aFraction does not know how to divide by the receiver." |
55 | 1233 |
|
1234 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 1235 |
! |
1236 |
||
1237 |
sumFromFraction:aFraction |
|
1238 |
"sent when a fraction does not know how to add the receiver, a float" |
|
1239 |
||
1240 |
|d| |
|
1241 |
||
1242 |
d := aFraction denominator. |
|
1243 |
^ (self * d + aFraction numerator) / d |
|
10680 | 1244 |
! |
1245 |
||
1246 |
sumFromTimestamp:aTimestamp |
|
1247 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
1248 |
after aTimestamp" |
|
1249 |
||
1250 |
^ aTimestamp addMilliseconds:(self * 1000) truncated. |
|
1251 |
||
1252 |
" |
|
1253 |
Timestamp now sumFromTimestamp:aTimestamp |
|
1254 |
100.0 sumFromTimestamp:Timestamp now |
|
1255 |
||
1256 |
|t1 t2| |
|
1257 |
t1 := Timestamp now. |
|
1258 |
t2 := 1.5 sumFromTimestamp:t1. |
|
1259 |
t1 inspect. t2 inspect. |
|
1260 |
" |
|
701 | 1261 |
! ! |
1262 |
||
16731 | 1263 |
|
23402 | 1264 |
!LimitedPrecisionReal methodsFor:'printing'! |
1265 |
||
1266 |
printStringScientific |
|
1267 |
"return a 'user friendly' scientific printString. |
|
1268 |
Notice: this returns a Text object with emphasis. |
|
1269 |
Also: the returned string is not meant to be read back - purely for GUIs" |
|
1270 |
||
1271 |
|s idx| |
|
1272 |
||
1273 |
s := PrintfScanf printf:'%e' argument:self. |
|
1274 |
idx := s indexOfAny:'eE'. |
|
1275 |
idx == 0 ifTrue:[^ s]. |
|
1276 |
^ (s copyTo:idx-1) , '×10' , ((s copyFrom:idx+1) asText emphasisAllAdd:#superscript) |
|
1277 |
||
1278 |
" |
|
1279 |
1.23e14 printStringScientific |
|
1280 |
" |
|
1281 |
||
1282 |
"Created: / 05-10-2018 / 13:38:44 / Claus Gittinger" |
|
1283 |
! ! |
|
22851 | 1284 |
|
22305 | 1285 |
!LimitedPrecisionReal methodsFor:'queries'! |
701 | 1286 |
|
22305 | 1287 |
decimalPrecision |
1288 |
"Answer how many digits of accuracy this class supports" |
|
1200 | 1289 |
|
22305 | 1290 |
^ self class decimalPrecision |
1200 | 1291 |
|
22305 | 1292 |
" |
1293 |
1.0 asFloat decimalPrecision |
|
1294 |
1.0 asLongFloat decimalPrecision |
|
1295 |
1.0 asShortFloat decimalPrecision |
|
1296 |
1.0 asQDouble decimalPrecision |
|
1297 |
1.0 asLargeFloat decimalPrecision |
|
1298 |
" |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1299 |
|
22305 | 1300 |
"Created: / 10-10-2017 / 15:46:43 / cg" |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1301 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1302 |
|
5883 | 1303 |
defaultNumberOfDigits |
7356 | 1304 |
"Answer how many digits of accuracy this class supports" |
1305 |
||
16663 | 1306 |
^ self class decimalPrecision |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1307 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1308 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1309 |
Float new defaultNumberOfDigits |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1310 |
LongFloat new defaultNumberOfDigits |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1311 |
ShortFloat new defaultNumberOfDigits |
21853 | 1312 |
QDouble new defaultNumberOfDigits |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1313 |
" |
21853 | 1314 |
|
1315 |
"Modified (comment): / 20-06-2017 / 12:58:12 / cg" |
|
1316 |
! |
|
1317 |
||
1318 |
exponent |
|
1319 |
"extract a normalized float's exponent. |
|
1320 |
This is a fallback for systems which do not provide frexp in their math lib, |
|
1321 |
als also for error reporting (NaN or Inf). |
|
1322 |
The returned value depends on the float-representation of |
|
1323 |
the underlying machine and is therefore highly unportable. |
|
1324 |
This is not for general use. |
|
1325 |
This assumes that the mantissa is normalized to |
|
1326 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
|
1327 |
||
1328 |
|shifty expPart exp numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
1329 |
biasExponent numIntegerBits fractionPart| |
|
1330 |
||
1331 |
(self isNaN or:[self isInfinite]) ifTrue:[ |
|
1332 |
^ self class |
|
1333 |
raise:#domainErrorSignal |
|
1334 |
receiver:self |
|
1335 |
selector:#exponent |
|
1336 |
arguments:#() |
|
1337 |
errorString:'bad receiver (Inf or NaN) in exponent' |
|
1338 |
]. |
|
1339 |
||
1340 |
"Extract the bits of an IEEE anySize float " |
|
1341 |
numBytes := self basicSize. |
|
1342 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
1343 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
1344 |
numIntegerBits := self class numBitsInIntegerPart. |
|
1345 |
biasExponent := maskExponent bitShift:-1. |
|
1346 |
||
1347 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
1348 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
1349 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
1350 |
] ifFalse:[ |
|
1351 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
1352 |
]. |
|
1353 |
||
1354 |
" Extract the sign and the biased exponent " |
|
22212 | 1355 |
expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent. |
21853 | 1356 |
|
1357 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
1358 |
fractionPart := shifty bitAnd:maskMantissa. |
|
1359 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
1360 |
||
1361 |
exp := expPart - biasExponent + 1. |
|
1362 |
^ exp |
|
1363 |
||
1364 |
" |
|
1365 |
0.3 asFloat exponent |
|
1366 |
0.3 asShortFloat exponent |
|
1367 |
0.3 asLongFloat exponent |
|
1368 |
||
1369 |
0.0 exponent2 0 |
|
1370 |
1.0 exponent2 1 |
|
1371 |
2.0 exponent2 2 |
|
1372 |
3.0 exponent2 2 |
|
1373 |
4.0 exponent2 3 |
|
1374 |
0.5 exponent2 0 |
|
1375 |
0.4 exponent2 -1 |
|
1376 |
0.25 exponent2 -1 |
|
1377 |
0.00000011111 exponent2 -23 |
|
1378 |
" |
|
1379 |
||
22212 | 1380 |
"Modified: / 25-08-2017 / 12:34:23 / cg" |
21853 | 1381 |
! |
1382 |
||
1383 |
fractionalPart |
|
1384 |
"This has been renamed to #fractionPart for ST80 compatibility. |
|
1385 |
||
1386 |
extract the after-decimal fraction part. |
|
1387 |
the floats value is |
|
1388 |
float truncated + float fractionalPart" |
|
1389 |
||
1390 |
<resource:#obsolete> |
|
1391 |
||
1392 |
self obsoleteMethodWarning:'please use #fractionPart'. |
|
1393 |
^ self fractionPart |
|
1394 |
||
1395 |
"Modified: / 28.10.1998 / 17:10:12 / cg" |
|
1396 |
"Created: / 28.10.1998 / 17:10:32 / cg" |
|
1397 |
! |
|
1398 |
||
1399 |
mantissa |
|
1400 |
"extract a normalized float's mantissa. |
|
1401 |
This is a fallback for systems which do not provide frexp in their math lib, |
|
1402 |
als also for error reporting (NaN or Inf)." |
|
1403 |
||
1404 |
|shifty expPart numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
1405 |
biasExponent numIntegerBits fractionPart| |
|
1406 |
||
1407 |
(self isNaN or:[self isInfinite]) ifTrue:[ |
|
1408 |
^ self class |
|
1409 |
raise:#domainErrorSignal |
|
1410 |
receiver:self |
|
1411 |
selector:#mantissa |
|
1412 |
arguments:#() |
|
1413 |
errorString:'bad receiver (Inf or NaN) in mantissa' |
|
1414 |
]. |
|
1415 |
||
1416 |
"Extract the bits of an IEEE anySize float " |
|
1417 |
numBytes := self basicSize. |
|
1418 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
1419 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
1420 |
numIntegerBits := self class numBitsInIntegerPart. |
|
1421 |
biasExponent := maskExponent bitShift:-1. |
|
1422 |
||
1423 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
1424 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
1425 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
1426 |
] ifFalse:[ |
|
1427 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
1428 |
]. |
|
1429 |
||
1430 |
" Extract the sign and the biased exponent " |
|
22212 | 1431 |
expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent. |
21853 | 1432 |
|
1433 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
1434 |
fractionPart := shifty bitAnd:maskMantissa. |
|
1435 |
^ fractionPart. |
|
1436 |
||
1437 |
" |
|
1438 |
0.3 asFloat mantissa |
|
1439 |
0.3 asShortFloat mantissa |
|
1440 |
0.3 asLongFloat mantissa |
|
1441 |
0.3 asQDouble mantissa |
|
1442 |
" |
|
1443 |
||
1444 |
"Created: / 20-06-2017 / 11:36:51 / cg" |
|
22212 | 1445 |
"Modified: / 25-08-2017 / 12:34:34 / cg" |
5883 | 1446 |
! |
1447 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1448 |
precision |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1449 |
"return the number of valid mantissa bits. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1450 |
Should be redefined in classes which allow per-instance precision specification" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1451 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1452 |
^ self class precision |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1453 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1454 |
|
5557 | 1455 |
size |
1456 |
"redefined since reals are kludgy (ByteArry)" |
|
1457 |
||
1458 |
^ 0 |
|
1459 |
! ! |
|
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
1460 |
|
22851 | 1461 |
|
701 | 1462 |
!LimitedPrecisionReal methodsFor:'testing'! |
1463 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1464 |
isFinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1465 |
^ self subclassResponsibility |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1466 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1467 |
"Created: / 7.1.1998 / 12:02:06 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1468 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1469 |
|
6185 | 1470 |
isFloat |
1471 |
"return true, if the receiver is some kind of floating point number; |
|
1472 |
false is returned here. |
|
1473 |
Same as #isLimitedPrecisionReal, but a better name ;-)" |
|
1474 |
||
1475 |
^ true |
|
1476 |
||
1477 |
"Created: / 14.11.2001 / 14:57:55 / cg" |
|
1478 |
! |
|
1479 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1480 |
isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1481 |
"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
|
1482 |
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
|
1483 |
however, inline C-code could produce them ..." |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1484 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1485 |
^ (self isFinite or:[self isNaN]) not. |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1486 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1487 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1488 |
1.0 isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1489 |
(0.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1490 |
(1.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1491 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1492 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1493 |
"Modified: / 7.1.1998 / 12:01:30 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1494 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1495 |
|
6086
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1496 |
isLimitedPrecisionReal |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1497 |
"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
|
1498 |
true is returned here - the method is redefined from Object." |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1499 |
|
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1500 |
^ true |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1501 |
! |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1502 |
|
2388 | 1503 |
isNaN |
1504 |
^ self subclassResponsibility |
|
1505 |
||
1506 |
"Modified: 12.2.1997 / 16:45:27 / cg" |
|
1507 |
! |
|
1508 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1509 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1510 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1511 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1512 |
^ (self isZero) and:[self printString first == $-] |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1513 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1514 |
" |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1515 |
0.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1516 |
-0.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1517 |
-1.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1518 |
1.0 asLongFloat isNegativeZero |
7356 | 1519 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1520 |
0.0 asLargeFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1521 |
-0.0 asLargeFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1522 |
" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1523 |
" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1524 |
0.0 asLongFloat isZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1525 |
-0.0 asLongFloat isZero |
701 | 1526 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1527 |
0.0 > 0 |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1528 |
-0.0 < 0 |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1529 |
" |
701 | 1530 |
! |
1531 |
||
5357 | 1532 |
numberOfBits |
1533 |
"return the size (in bits) of the real; |
|
1534 |
typically, this is 64 for Floats and 32 for ShortFloats, |
|
1535 |
but who knows ..." |
|
1536 |
||
1537 |
self subclassResponsibility |
|
1538 |
! |
|
1539 |
||
701 | 1540 |
positive |
18854 | 1541 |
"return true if the receiver is greater or equal to zero (not negative)" |
701 | 1542 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1543 |
^ self sign >= 0 |
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1544 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1545 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
17380 | 1546 |
! |
1547 |
||
1548 |
sign |
|
1549 |
"return the sign of the receiver (-1, 0 or 1)" |
|
1550 |
||
1551 |
self strictlyPositive ifTrue:[^ 1]. |
|
1552 |
self negative ifTrue:[^ -1]. |
|
1553 |
^ 0 |
|
1554 |
||
1555 |
" |
|
1556 |
-1.0 sign |
|
1557 |
-0.0 sign |
|
1558 |
1.0 sign |
|
1559 |
0.0 sign |
|
1560 |
" |
|
55 | 1561 |
! ! |
1562 |
||
7356 | 1563 |
!LimitedPrecisionReal methodsFor:'truncation & rounding'! |
1564 |
||
1565 |
ceilingAsFloat |
|
1566 |
^ self coerce:(self ceiling). |
|
1567 |
||
1568 |
" |
|
1569 |
0.4 asLongFloat ceilingAsFloat |
|
1570 |
" |
|
1571 |
! |
|
1572 |
||
1573 |
floorAsFloat |
|
1574 |
^ self coerce:(self floor). |
|
1575 |
||
1576 |
" |
|
1577 |
0.4 asLongFloat floorAsFloat |
|
1578 |
" |
|
1579 |
! |
|
1580 |
||
1581 |
roundedAsFloat |
|
1582 |
self negative ifTrue:[ ^ (self - 0.5) ceilingAsFloat ]. |
|
1583 |
^ (self + 0.5) floorAsFloat |
|
7398 | 1584 |
! |
1585 |
||
1586 |
truncatedAsFloat |
|
1587 |
^ self coerce:(self truncated). |
|
1588 |
||
1589 |
" |
|
1590 |
0.4 asLongFloat truncatedAsFloat |
|
1591 |
" |
|
22786 | 1592 |
! |
1593 |
||
1594 |
truncatedToPrecision |
|
1595 |
"truncates to the precision of the float. |
|
1596 |
This is slightly different from truncated. |
|
1597 |
Taking for example 1e32, |
|
1598 |
the printed representation will be 1e32, |
|
1599 |
but the actual value, when truncating to an integer |
|
1600 |
would be 100000003318135351409612647563264. |
|
1601 |
||
1602 |
This is due to the inaccuracy in the least significant bits, |
|
1603 |
and the way the print-converter compensates for this. |
|
1604 |
This method tries to generate an integer value which corresponds |
|
1605 |
to what is seen in the float's printString. |
|
1606 |
||
1607 |
Here, a slow fallback (generating and rescanning the printString) |
|
1608 |
is provided, which should work on any float number. |
|
1609 |
Specialized versions in subclasses may be added for more performance |
|
1610 |
(however, this is probably only used rarely)" |
|
1611 |
||
1612 |
|pow10 printString intVal s sign nFract ch expSign exp pI| |
|
1613 |
||
1614 |
pow10 := #(10 100 1000 10000 100000 1000000 10000000 100000000 1000000000). |
|
1615 |
||
1616 |
printString := self printString. |
|
1617 |
intVal := 0. |
|
1618 |
sign := 1. |
|
1619 |
nFract := 0. |
|
1620 |
exp := 0. |
|
1621 |
||
1622 |
"/ fetch the mantissa |
|
1623 |
s := printString readStream. |
|
1624 |
s peek == $- ifTrue:[ |
|
1625 |
sign := -1. |
|
1626 |
s next. |
|
1627 |
]. |
|
1628 |
intVal := s nextDecimalInteger. |
|
1629 |
s peek == $. ifTrue:[ |
|
1630 |
s next. |
|
1631 |
[(ch := s peek) notNil and:[ch isDigit]] whileTrue:[ |
|
1632 |
intVal := intVal * 10 + (s next digitValue). |
|
1633 |
nFract := nFract + 1. |
|
1634 |
]. |
|
1635 |
]. |
|
1636 |
('eEdDqQ' includes:s peek) ifTrue:[ |
|
1637 |
expSign := 1. |
|
1638 |
s next. |
|
1639 |
(ch := s peek) == $- ifTrue:[ |
|
1640 |
expSign := -1. |
|
1641 |
s next. |
|
1642 |
] ifFalse:[ |
|
1643 |
ch == $+ ifTrue:[ |
|
1644 |
s next. |
|
1645 |
]. |
|
1646 |
]. |
|
1647 |
exp := s nextDecimalInteger. |
|
1648 |
exp := exp * expSign. |
|
1649 |
]. |
|
1650 |
exp := exp - nFract. |
|
1651 |
exp < 0 ifTrue:[ |
|
1652 |
[exp < 0] whileTrue:[ |
|
1653 |
pI := (exp negated) min:pow10 size. |
|
1654 |
intVal := intVal / (pow10 at:pI). |
|
1655 |
exp := exp + pI. |
|
1656 |
]. |
|
1657 |
intVal := intVal asInteger. |
|
1658 |
] ifFalse:[ |
|
1659 |
[exp > 0] whileTrue:[ |
|
1660 |
pI := exp min:pow10 size. |
|
1661 |
intVal := intVal * (pow10 at:pI). |
|
1662 |
exp := exp - pI. |
|
1663 |
]. |
|
1664 |
]. |
|
1665 |
^ intVal |
|
1666 |
||
1667 |
" |
|
1668 |
1e32 asShortFloat truncated |
|
1669 |
1e32 asShortFloat truncatedToPrecision |
|
1670 |
1.234e10 asShortFloat truncatedToPrecision |
|
1671 |
1234e-1 asShortFloat truncatedToPrecision |
|
1672 |
||
1673 |
1e32 truncated |
|
1674 |
1e32 truncatedToPrecision |
|
1675 |
1.234e10 truncatedToPrecision |
|
1676 |
1234e-1 truncatedToPrecision |
|
1677 |
||
1678 |
1e32 asLongFloat truncated |
|
1679 |
1e32 asLongFloat truncatedToPrecision |
|
1680 |
1.234e10 asLongFloat truncatedToPrecision |
|
1681 |
1234e-1 asLongFloat truncatedToPrecision |
|
1682 |
" |
|
7356 | 1683 |
! ! |
1684 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1685 |
!LimitedPrecisionReal methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1686 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1687 |
acceptVisitor:aVisitor with:aParameter |
16731 | 1688 |
"dispatch for visitor pattern; send #visitFloat:with: to aVisitor." |
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1689 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1690 |
^ aVisitor visitFloat:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1691 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1692 |
|
701 | 1693 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 1694 |
|
701 | 1695 |
version |
18834 | 1696 |
^ '$Header$' |
12120 | 1697 |
! |
1698 |
||
1699 |
version_CVS |
|
18834 | 1700 |
^ '$Header$' |
55 | 1701 |
! ! |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1702 |
|
16405 | 1703 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
1704 |
LimitedPrecisionReal initialize! |