author | Claus Gittinger <cg@exept.de> |
Mon, 26 Aug 2019 18:16:06 +0200 | |
changeset 24643 | 4953a421be84 |
parent 24437 | 6af740056b8a |
child 24705 | 4caa65eee05e |
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:[ |
24289 | 225 |
"be careful to round to nearest even floating point value - |
9406 | 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" |
24289 | 293 |
"Modified (format): / 08-06-2019 / 02:17:43 / Claus Gittinger" |
7447
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 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
296 |
fromLimitedPrecisionReal:anLPReal |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
297 |
"return a float with anLPReals value. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
298 |
You might loose bits when doing this. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
299 |
Slow fallback." |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
300 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
301 |
|fract| |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
302 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
303 |
anLPReal isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
304 |
anLPReal isNaN ifTrue:[^ self NaN]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
305 |
anLPReal negative ifTrue:[^ self negativeInfinity]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
306 |
^ self infinity |
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 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
309 |
fract := anLPReal asTrueFraction. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
310 |
^ (self fromInteger:fract numerator) / (self fromInteger:fract denominator) |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
311 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
312 |
|
9406 | 313 |
fromNumerator:numerator denominator:denominator |
314 |
"Create a limited precision real from a Rational. |
|
315 |
This version will answer the nearest flotaing point value, |
|
316 |
according to IEEE 754 round to nearest even default mode" |
|
317 |
||
16444 | 318 |
|a b q r exponent floatExponent n ha hb hq q1 eMin precision nSubHq| |
9406 | 319 |
|
320 |
a := numerator abs. |
|
321 |
b := denominator abs. |
|
322 |
ha := a highBit. |
|
323 |
hb := b highBit. |
|
324 |
||
325 |
precision := self precision. |
|
326 |
||
327 |
"If both numerator and denominator are represented exactly in floating point number, |
|
328 |
then fastest thing to do is to use hardwired float division" |
|
329 |
(ha <= precision and:[hb <= precision]) ifTrue: [ |
|
16444 | 330 |
^ (self coerce:numerator) / (self coerce:denominator) |
9406 | 331 |
]. |
332 |
||
333 |
"Try and obtain a mantissa with 1 bit excess precision. |
|
334 |
First guess is rough, we might get one more bit or one less" |
|
335 |
exponent := ha - hb - precision - 1. |
|
336 |
exponent > 0 |
|
337 |
ifTrue: [b := b bitShift: exponent] |
|
22212 | 338 |
ifFalse: [a := a rightShift: exponent]. |
9406 | 339 |
q := a quo: b. |
340 |
r := a - (q * b). |
|
341 |
hq := q highBit. |
|
342 |
||
343 |
"check for gradual underflow, in which case we should use less bits" |
|
344 |
floatExponent := exponent + hq - 1. |
|
345 |
eMin := 2 - (1 bitShift: (self numBitsInExponent - 1)). |
|
346 |
n := floatExponent >= eMin |
|
347 |
ifTrue: [precision + 1] |
|
348 |
ifFalse: [precision + 1 + floatExponent - eMin]. |
|
349 |
||
350 |
nSubHq := n - hq. |
|
351 |
||
352 |
nSubHq < 0 ifTrue: [ |
|
353 |
exponent := exponent + hq - n. |
|
354 |
r := (q bitAnd: (1 bitShift:nSubHq) - 1) * b + r. |
|
355 |
q := q bitShift:nSubHq |
|
356 |
] ifFalse:[nSubHq > 0 ifTrue: [ |
|
357 |
exponent := exponent + hq - n. |
|
358 |
q1 := (r bitShift:nSubHq) quo: b. |
|
359 |
q := (q bitShift:nSubHq) bitAnd: q1. |
|
360 |
r := r - (q1 * b) |
|
361 |
]]. |
|
362 |
||
363 |
"check if we should round upward. |
|
364 |
The case of exact half (q bitAnd: 1) isZero not & (r isZero) |
|
365 |
will be handled by self fromInteger: conversion" |
|
366 |
((q bitAnd:1) ~~ 0 and:[r ~~ 0]) ifTrue:[ |
|
367 |
q := q + 1 |
|
368 |
]. |
|
369 |
||
370 |
(numerator negative xor:denominator negative) ifTrue: [ |
|
371 |
q := q negated. |
|
372 |
]. |
|
373 |
||
16444 | 374 |
^ (self coerce:q) timesTwoPower:exponent |
9406 | 375 |
|
376 |
" |
|
377 |
Time millisecondsToRun:[ |
|
378 |
1000000 timesRepeat:[ |
|
379 |
Float fromNumerator:12345678901234567890 denominator:987654321 |
|
380 |
]. |
|
381 |
] |
|
382 |
||
383 |
|fraction| |
|
384 |
fraction := 12345678901234567890//987654321. |
|
385 |
Time millisecondsToRun:[ |
|
386 |
1000000 timesRepeat:[ |
|
387 |
fraction asFloat |
|
388 |
]. |
|
389 |
] |
|
390 |
" |
|
22212 | 391 |
|
392 |
"Modified: / 25-08-2017 / 12:34:55 / cg" |
|
9406 | 393 |
! |
394 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
395 |
new:aNumber |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
396 |
"catch this message - not allowed for floats/doubles" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
397 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
398 |
self error:'Floats/Doubles cannot be created with new:' |
16444 | 399 |
! |
400 |
||
401 |
readFrom:aStringOrStream onError:exceptionBlock |
|
402 |
"read a float from a string" |
|
403 |
||
404 |
|num| |
|
405 |
||
21270 | 406 |
num := super readFrom:aStringOrStream onError:[^ exceptionBlock value]. |
16444 | 407 |
^ self coerce:num |
408 |
||
409 |
" |
|
410 |
Float readFrom:'.1' |
|
411 |
Float readFrom:'0.1' |
|
412 |
Float readFrom:'0' |
|
413 |
||
414 |
ShortFloat readFrom:'.1' |
|
415 |
ShortFloat readFrom:'0.1' |
|
416 |
ShortFloat readFrom:'0' |
|
417 |
||
418 |
LongFloat readFrom:'.1' |
|
419 |
LongFloat readFrom:'0.1' |
|
420 |
LongFloat readFrom:'0' |
|
21270 | 421 |
|
422 |
LimitedPrecisionReal readFrom:'bla' onError:nil |
|
423 |
Float readFrom:'bla' onError:nil |
|
424 |
ShortFloat readFrom:'bla' onError:nil |
|
16444 | 425 |
" |
426 |
||
427 |
"Created: / 07-01-1998 / 16:17:19 / cg" |
|
428 |
"Modified: / 23-11-2010 / 14:35:41 / cg" |
|
7447
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 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
431 |
!LimitedPrecisionReal class methodsFor:'class initialization'! |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
432 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
433 |
initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
434 |
"initialize ANSI compliant float globals" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
435 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
436 |
Smalltalk at:#FloatE put:ShortFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
437 |
Smalltalk at:#FloatD put:Float. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
438 |
Smalltalk at:#FloatQ put:LongFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
439 |
|
21896 | 440 |
"/ not defined in ANSI, but consistent in its naming |
441 |
Smalltalk at:#FloatQD put:QDouble. |
|
442 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
443 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
444 |
self initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
445 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
446 |
|
21896 | 447 |
"Created: / 07-09-2001 / 14:02:45 / cg" |
448 |
"Modified (comment): / 21-06-2017 / 13:59:08 / cg" |
|
5989
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 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
451 |
!LimitedPrecisionReal class methodsFor:'constants & defaults'! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
452 |
|
7390 | 453 |
NaN |
24264 | 454 |
"return the constant NaN (not a Number) in my representation. |
455 |
Here, based on the assumption that division of zero by zero generates a NaN |
|
456 |
(which is defined as sch in the IEEE standard). |
|
457 |
If a subclass does not, it needs to generate a NaN differently" |
|
7390 | 458 |
|
18281 | 459 |
|zero| |
460 |
||
461 |
zero := self zero. |
|
462 |
^ zero uncheckedDivide:zero. |
|
7390 | 463 |
|
464 |
" |
|
465 |
ShortFloat NaN |
|
466 |
Float NaN |
|
467 |
LongFloat NaN |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
468 |
LargeFloat NaN |
7390 | 469 |
" |
21825 | 470 |
|
471 |
"Modified (comment): / 16-06-2017 / 11:03:28 / cg" |
|
24264 | 472 |
"Modified (comment): / 06-06-2019 / 16:52:43 / Claus Gittinger" |
7390 | 473 |
! |
474 |
||
16663 | 475 |
computeEpsilon |
24437 | 476 |
"compute the maximum relative spacing of instances of mySelf |
477 |
(i.e. the value-delta of the least significant bit)" |
|
16663 | 478 |
|
24437 | 479 |
^ (self coerce:self radix) raisedToInteger:(self precision negated). |
16663 | 480 |
|
481 |
" |
|
21946 | 482 |
Float radix |
483 |
Float precision |
|
484 |
||
24437 | 485 |
Float computeEpsilon |
21946 | 486 |
ShortFloat computeEpsilon |
16663 | 487 |
LongFloat computeEpsilon |
21946 | 488 |
QDouble epsilon |
16663 | 489 |
" |
21946 | 490 |
|
491 |
"Modified (comment): / 03-07-2017 / 12:12:06 / cg" |
|
22852 | 492 |
"Modified: / 10-05-2018 / 01:19:25 / stefan" |
24437 | 493 |
"Modified (comment): / 19-07-2019 / 17:26:30 / Claus Gittinger" |
16663 | 494 |
! |
495 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
496 |
emax |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
497 |
"return the largest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
498 |
|
17655
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
499 |
^ (1 << (self numBitsInExponent-1)) - 1 |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
500 |
|
17655
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
501 |
" |
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
502 |
Float emax |
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
503 |
ShortFloat emax |
b8f27b84fda3
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
17380
diff
changeset
|
504 |
" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
505 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
506 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
507 |
emin |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
508 |
"return the smallest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
509 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
510 |
^ self subclassResponsibility |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
511 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
512 |
" |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
513 |
Float emin |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
514 |
ShortFloat emin |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
515 |
" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
516 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
517 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
518 |
epsilon |
21802 | 519 |
"return the maximum relative spacing of instances of mySelf |
520 |
(i.e. the value-delta of the least significant bit)" |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
521 |
|
9406 | 522 |
^ self radix asFloat raisedToInteger:(1 - self precision) |
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
523 |
|
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
524 |
" |
22851 | 525 |
Float epsilon -> 2.22044604925031E-16 |
526 |
ShortFloat epsilon -> 1.192093e-07 |
|
527 |
LongFloat epsilon -> 1.084202172485504434E-19 |
|
528 |
QDouble epsilon -> 1.21543267145725E-63 |
|
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
529 |
" |
21802 | 530 |
|
531 |
"Modified (comment): / 12-06-2017 / 18:52:31 / cg" |
|
22851 | 532 |
"Modified (comment): / 10-05-2018 / 01:08:48 / stefan" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
533 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
534 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
535 |
fmax |
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
536 |
"The largest value allowed by instances of this class." |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
537 |
|
22499 | 538 |
|radix radixReal| |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
539 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
540 |
radix := self radix. |
22499 | 541 |
radixReal := (self fromInteger: radix). |
21807 | 542 |
^ ((self fromInteger: 1) - |
22499 | 543 |
(radixReal timesTwoPower: self precision negated - 1)) * radix |
544 |
* (radixReal timesTwoPower: self emax - 1) |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
545 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
546 |
" |
20978 | 547 |
Float fmax |
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
548 |
ShortFloat fmax |
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
549 |
LongFloat fmax |
21807 | 550 |
QDouble fmax |
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
551 |
" |
20978 | 552 |
|
21807 | 553 |
"Modified (comment): / 14-06-2017 / 19:17:39 / cg" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
554 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
555 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
556 |
fmin |
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
557 |
"The smallest value allowed by instances of this class." |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
558 |
|
18135
6b108770c16d
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
18127
diff
changeset
|
559 |
^ self subclassResponsibility |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
560 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
561 |
|
7390 | 562 |
infinity |
24304 | 563 |
"return an instance of myself which represents positive infinity (for my instances). |
564 |
Warning: do not compare equal against infinities; |
|
565 |
instead, check using isFinite or isInfinite" |
|
7390 | 566 |
|
567 |
^ self unity uncheckedDivide:self zero |
|
568 |
||
569 |
" |
|
570 |
ShortFloat infinity |
|
571 |
Float infinity |
|
572 |
LongFloat infinity |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
573 |
LargeFloat infinity |
7390 | 574 |
" |
21861 | 575 |
|
576 |
"Modified (comment): / 20-06-2017 / 13:46:52 / cg" |
|
24304 | 577 |
"Modified (comment): / 09-06-2019 / 12:52:52 / Claus Gittinger" |
7390 | 578 |
! |
579 |
||
19359 | 580 |
maxSmallInteger |
22610 | 581 |
"answer the largest possible SmallInteger value as instance of myself" |
19359 | 582 |
|
583 |
^ self fromInteger:SmallInteger maxVal. |
|
584 |
||
585 |
" |
|
21861 | 586 |
Float maxSmallInteger. |
587 |
LongFloat maxSmallInteger. |
|
588 |
ShortFloat maxSmallInteger. |
|
589 |
QDouble maxSmallInteger. |
|
19359 | 590 |
" |
21861 | 591 |
|
592 |
"Modified (comment): / 20-06-2017 / 14:07:27 / cg" |
|
22610 | 593 |
"Modified (comment): / 14-03-2018 / 20:11:28 / mawalch" |
19359 | 594 |
! |
595 |
||
7390 | 596 |
negativeInfinity |
24304 | 597 |
"return an instance of myself which represents negative infinity (for my instances). |
598 |
Warning: do not compare equal against infinities; |
|
599 |
instead, check using isFinite or isInfinite" |
|
7390 | 600 |
|
601 |
^ self unity negated uncheckedDivide:self zero |
|
602 |
||
603 |
" |
|
604 |
ShortFloat negativeInfinity |
|
605 |
Float negativeInfinity |
|
606 |
LongFloat negativeInfinity |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
607 |
LargeFloat negativeInfinity |
7390 | 608 |
" |
21861 | 609 |
|
610 |
"Modified (comment): / 20-06-2017 / 13:46:42 / cg" |
|
24304 | 611 |
"Modified (comment): / 09-06-2019 / 12:55:35 / Claus Gittinger" |
7356 | 612 |
! ! |
613 |
||
614 |
!LimitedPrecisionReal class methodsFor:'queries'! |
|
615 |
||
616 |
decimalPrecision |
|
617 |
"return the number of valid decimal digits" |
|
618 |
||
24264 | 619 |
^ ((self precision + 1) / (10.0 log:self radix)) |
620 |
"/ ceiling "use ceiling, since the last decimal digit contains some precision" |
|
621 |
floor "use floor, since the last decimal digit is usually wrong" |
|
7356 | 622 |
|
623 |
" |
|
24264 | 624 |
ShortFloat decimalPrecision -> 7 |
625 |
Float decimalPrecision -> 16 |
|
626 |
LongFloat decimalPrecision -> 19 |
|
7356 | 627 |
" |
24264 | 628 |
|
629 |
"Modified (comment): / 06-06-2019 / 13:21:04 / Claus Gittinger" |
|
7356 | 630 |
! |
631 |
||
21825 | 632 |
defaultPrintPrecision |
633 |
"return the number of decimal digits printed by default" |
|
634 |
||
635 |
^ 6 |
|
636 |
||
637 |
" |
|
638 |
ShortFloat defaultPrintPrecision |
|
639 |
Float defaultPrintPrecision |
|
640 |
LongFloat defaultPrintPrecision |
|
641 |
" |
|
642 |
||
643 |
"Created: / 17-06-2017 / 02:56:43 / cg" |
|
644 |
! |
|
645 |
||
12120 | 646 |
denormalized |
647 |
"Return whether the instances of this class can |
|
648 |
represent values in denormalized format." |
|
649 |
||
650 |
^ true |
|
651 |
! |
|
652 |
||
24264 | 653 |
exactDecimalPrecision |
654 |
"return the exact number of decimal digits" |
|
655 |
||
656 |
^ ((self precision) / (10.0 log:self radix)) |
|
657 |
||
658 |
" |
|
659 |
ShortFloat exactDecimalPrecision -> 7.224719895935548004 |
|
660 |
Float exactDecimalPrecision -> 15.95458977019100184 |
|
661 |
LongFloat exactDecimalPrecision -> 19.26591972249479468 |
|
662 |
" |
|
663 |
||
664 |
"Created: / 06-06-2019 / 13:19:17 / Claus Gittinger" |
|
665 |
! |
|
666 |
||
20058 | 667 |
hasSharedInstances |
668 |
"return true if this class has shared instances, that is, instances |
|
669 |
with the same value are identical. |
|
670 |
Although not really shared, floats should be treated |
|
671 |
so, to be independent of the implementation of the arithmetic methods." |
|
672 |
||
673 |
^ true |
|
674 |
||
675 |
||
676 |
! |
|
677 |
||
8892 | 678 |
isAbstract |
11225 | 679 |
"Return if this class is an abstract class. |
680 |
True is returned for LimitedPrecisionReal here; false for subclasses." |
|
681 |
||
8892 | 682 |
^ self == LimitedPrecisionReal |
683 |
||
684 |
" |
|
685 |
1.0 class isAbstract |
|
686 |
" |
|
687 |
! |
|
688 |
||
17221 | 689 |
isIEEEFormat |
690 |
"return true, if this machine represents floats in IEEE format. |
|
691 |
Currently, no support is provided for non-ieee machines |
|
692 |
to convert their floats into this (which is only relevant, |
|
693 |
if such a machine wants to send floats as binary to some other |
|
694 |
machine). |
|
695 |
Machines with non-IEEE format are VAXed and IBM370-type systems |
|
696 |
(among others). Today, most systems use IEEE format floats." |
|
697 |
||
698 |
%{ |
|
699 |
#ifdef __s390__ |
|
700 |
RETURN(false); |
|
701 |
#endif |
|
702 |
%}. |
|
703 |
||
704 |
^ true "/ this may be a lie |
|
705 |
! |
|
706 |
||
7356 | 707 |
numBitsInExponent |
708 |
"return the number of bits in the exponent" |
|
709 |
||
710 |
^ self subclassResponsibility |
|
711 |
! |
|
712 |
||
713 |
numBitsInIntegerPart |
|
16781
4619d7da013a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16731
diff
changeset
|
714 |
"answer the number of bits in the integer part of the mantissa. |
24203 | 715 |
Most floating point formats are normalized to get rid of the extra bit." |
16781
4619d7da013a
class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
16731
diff
changeset
|
716 |
|
7356 | 717 |
^ 0 |
24203 | 718 |
|
719 |
"Modified (comment): / 28-05-2019 / 08:58:11 / Claus Gittinger" |
|
7356 | 720 |
! |
721 |
||
722 |
numBitsInMantissa |
|
723 |
"return the number of bits in the mantissa |
|
724 |
(typically 1 less than the precision due to the hidden bit)" |
|
725 |
||
726 |
^ self subclassResponsibility |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
727 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
728 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
729 |
precision |
7356 | 730 |
"return the number of valid mantissa bits" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
731 |
|
7356 | 732 |
^ self numBitsInMantissa + 1 - self numBitsInIntegerPart |
21802 | 733 |
|
734 |
" |
|
24264 | 735 |
HalfFloatArray precision |
21802 | 736 |
ShortFloat precision |
737 |
Float precision |
|
738 |
LongFloat precision |
|
739 |
QDouble precision |
|
740 |
" |
|
741 |
||
742 |
"Modified (comment): / 12-06-2017 / 18:47:54 / cg" |
|
24264 | 743 |
"Modified (comment): / 06-06-2019 / 13:25:00 / Claus Gittinger" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
744 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
745 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
746 |
radix |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
747 |
"return the radix (base)" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
748 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
749 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
750 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
751 |
"Created: / 7.9.2001 / 14:08:20 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
752 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
753 |
|
55 | 754 |
!LimitedPrecisionReal methodsFor:'accessing'! |
755 |
||
756 |
at:index |
|
757 |
"redefined to prevent access to individual bytes in a real." |
|
758 |
||
759 |
self error:'not allowed for floats/doubles' |
|
760 |
! |
|
761 |
||
762 |
at:index put:aValue |
|
763 |
"redefined to prevent access to individual bytes in a real" |
|
764 |
||
765 |
self error:'not allowed for floats/doubles' |
|
766 |
! ! |
|
767 |
||
768 |
!LimitedPrecisionReal methodsFor:'arithmetic'! |
|
769 |
||
701 | 770 |
* aNumber |
11727 | 771 |
"return the product of the receiver and the argument." |
701 | 772 |
|
1200 | 773 |
"/ as soon as Float are float & Double are doubles, |
774 |
"/ use: |
|
775 |
"/ ^ aNumber productFromDouble:self asDouble |
|
776 |
||
777 |
^ aNumber productFromFloat:self asFloat |
|
778 |
||
779 |
"Modified: 17.4.1996 / 12:35:36 / cg" |
|
701 | 780 |
! |
781 |
||
55 | 782 |
+ aNumber |
783 |
"return the sum of the receiver and the argument, aNumber" |
|
784 |
||
1200 | 785 |
"/ as soon as Float are float & Double are doubles, |
786 |
"/ use: |
|
787 |
"/ ^ aNumber sumFromDouble:self asDouble |
|
788 |
||
789 |
^ aNumber sumFromFloat:self asFloat |
|
790 |
||
791 |
"Modified: 17.4.1996 / 12:35:55 / cg" |
|
55 | 792 |
! |
793 |
||
794 |
- aNumber |
|
795 |
"return the difference of the receiver and the argument, aNumber" |
|
796 |
||
1200 | 797 |
"/ as soon as Float are float & Double are doubles, |
798 |
"/ use: |
|
799 |
"/ ^ aNumber differenceFromDouble:self asDouble |
|
800 |
||
801 |
^ aNumber differenceFromFloat:self asFloat |
|
802 |
||
803 |
"Modified: 17.4.1996 / 12:36:07 / cg" |
|
55 | 804 |
! |
805 |
||
806 |
/ aNumber |
|
807 |
"return the quotient of the receiver and the argument, aNumber" |
|
808 |
||
809 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
7141
033f2c26d8e6
replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents:
6890
diff
changeset
|
810 |
^ ZeroDivide raiseRequestWith:thisContext. |
55 | 811 |
]. |
1200 | 812 |
"/ as soon as Float are float & Double are doubles, |
813 |
"/ use: |
|
814 |
"/ ^ aNumber quotientFromDouble:self asDouble |
|
815 |
||
816 |
^ aNumber quotientFromFloat:self asFloat |
|
817 |
||
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
3898
diff
changeset
|
818 |
"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
|
819 |
"Modified: / 26.7.1999 / 10:46:11 / stefan" |
55 | 820 |
! |
821 |
||
822 |
// aNumber |
|
823 |
"return the integer quotient of dividing the receiver by aNumber with |
|
1882 | 824 |
truncation towards negative infinity." |
55 | 825 |
|
826 |
^ (self / aNumber) floor asInteger |
|
1882 | 827 |
|
828 |
"Modified: 5.11.1996 / 11:45:37 / cg" |
|
7356 | 829 |
! |
830 |
||
831 |
ceiling |
|
7398 | 832 |
^ self asTrueFraction ceiling |
7356 | 833 |
! |
834 |
||
835 |
floor |
|
7398 | 836 |
^ self asTrueFraction floor |
9406 | 837 |
! |
838 |
||
839 |
timesTwoPower:anInteger |
|
840 |
"multiply self by a power of two. |
|
841 |
Implementation takes care of preserving class and avoiding overflow/underflow |
|
842 |
Thanks to Nicolas Cellier for this code" |
|
843 |
||
12761 | 844 |
|half two h1 h2| |
9406 | 845 |
|
16444 | 846 |
two := self coerce:2.0. |
9406 | 847 |
|
24203 | 848 |
anInteger abs highBit >= (self numBitsInExponent - 2) ifTrue:[ |
9406 | 849 |
half := anInteger // 2. |
12761 | 850 |
|
851 |
anInteger even ifTrue:[ |
|
852 |
"/ halves are equal |
|
853 |
h2 := h1 := (two raisedToInteger: half). |
|
854 |
] ifFalse:[ |
|
855 |
h1 := (two raisedToInteger: half). |
|
856 |
anInteger > 0 ifTrue:[ |
|
857 |
h2 := h1 * two. |
|
858 |
] ifFalse:[ |
|
859 |
h2 := (two raisedToInteger: anInteger - half). |
|
860 |
]. |
|
861 |
]. |
|
862 |
^ (self * h1) * h2 |
|
9406 | 863 |
] ifFalse: [ |
864 |
^ self * (two raisedToInteger: anInteger). |
|
865 |
] |
|
866 |
||
12758 | 867 |
" |
868 |
(1 asShortFloat timesTwoPower: 3) class = ShortFloat. |
|
869 |
(1 asLongFloat timesTwoPower: 1024). |
|
870 |
(1 asFloat timesTwoPower: -1024) timesTwoPower: 1024. |
|
24203 | 871 |
(1 asLongFloat timesTwoPower: -1024) timesTwoPower: 1024. |
12761 | 872 |
|
873 |
(2.0 asShortFloat timesTwoPower: -150) timesTwoPower: 150 |
|
874 |
(2.0 asLongFloat timesTwoPower: -150) timesTwoPower: 150 |
|
875 |
(2.0 asFloat timesTwoPower: -150) timesTwoPower: 150 |
|
876 |
||
877 |
(2.0 asShortFloat timesTwoPower: -149) timesTwoPower: 149 |
|
878 |
(2.0 asLongFloat timesTwoPower: -149) timesTwoPower: 149 |
|
879 |
(2.0 asFloat timesTwoPower: -149) timesTwoPower: 149 |
|
880 |
||
881 |
Time millisecondsToRun:[ |
|
882 |
1000000 timesRepeat:[ |
|
883 |
(2.0 timesTwoPower: 150) |
|
884 |
] |
|
885 |
] |
|
12758 | 886 |
" |
24203 | 887 |
|
888 |
"Modified (comment): / 28-05-2019 / 09:08:25 / Claus Gittinger" |
|
55 | 889 |
! ! |
890 |
||
17221 | 891 |
!LimitedPrecisionReal methodsFor:'bytes access'! |
892 |
||
893 |
digitBytes |
|
894 |
"answer the float's digit bytes im IEEE format. |
|
895 |
Use the native machine byte ordering." |
|
896 |
||
897 |
|sz "{ Class:SmallInteger }" |
|
898 |
bytes| |
|
899 |
||
900 |
sz := self basicSize. |
|
901 |
bytes := ByteArray new:sz. |
|
902 |
1 to:sz do:[:i| |
|
903 |
bytes at:i put:(self basicAt:i). |
|
904 |
]. |
|
905 |
^ bytes |
|
906 |
||
907 |
" |
|
908 |
Float pi digitBytes |
|
909 |
ShortFloat pi digitBytes |
|
910 |
" |
|
911 |
! |
|
912 |
||
913 |
digitBytesMSB:msb |
|
914 |
"answer the float's digit bytes im IEEE format. |
|
915 |
If msb == true, use MSB byte order, otherwise LSB byte order." |
|
916 |
||
917 |
|sz "{ Class:SmallInteger }" |
|
918 |
bytes| |
|
919 |
||
920 |
self class isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
921 |
||
922 |
sz := self basicSize. |
|
923 |
bytes := ByteArray new:sz. |
|
924 |
||
925 |
(UninterpretedBytes isBigEndian == msb) ifTrue:[ |
|
926 |
1 to:sz do:[:i| |
|
927 |
bytes at:i put:(self basicAt:i). |
|
928 |
]. |
|
929 |
] ifFalse:[ |
|
930 |
"reverse the bytes" |
|
931 |
1 to:sz do:[:i| |
|
932 |
bytes at:sz-i+1 put:(self basicAt:i). |
|
933 |
]. |
|
934 |
]. |
|
935 |
^ bytes |
|
936 |
||
937 |
" |
|
938 |
Float pi digitBytesMSB:false |
|
939 |
Float pi digitBytesMSB:true |
|
940 |
ShortFloat pi digitBytesMSB:false |
|
941 |
ShortFloat pi digitBytesMSB:true |
|
942 |
" |
|
943 |
! ! |
|
944 |
||
5238 | 945 |
!LimitedPrecisionReal methodsFor:'coercing & converting'! |
55 | 946 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
947 |
asFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
948 |
^ Float fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
949 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
950 |
|
55 | 951 |
asFraction |
5883 | 952 |
"Answer a rational number (Integer or Fraction) representing the receiver. |
953 |
This conversion uses the continued fraction method to approximate |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
954 |
a floating point number. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
955 |
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
|
956 |
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
|
957 |
" |
5883 | 958 |
|
959 |
|num1 denom1 num2 denom2 int frac newD temp limit| |
|
55 | 960 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
961 |
self isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
962 |
"/ ^ self asMetaNumber. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
963 |
^ self class |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
964 |
raise:#domainErrorSignal |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
965 |
receiver:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
966 |
selector:#asFraction |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
967 |
arguments:#() |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
968 |
errorString:'Cannot represent non-finite number as fraction' |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
969 |
]. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
970 |
|
7394 | 971 |
limit := 10 raisedToInteger:self defaultNumberOfDigits. |
5883 | 972 |
num1 := self truncated. |
973 |
denom1 := 1. "The first of two alternating denominators" |
|
974 |
num2 := 1. "The second numerator" |
|
975 |
denom2 := 0. "The second denominator--will update" |
|
976 |
int := num1. "The integer part of self" |
|
977 |
frac := self fractionPart. |
|
978 |
[frac = 0] whileFalse:[ |
|
979 |
newD := 1.0 / frac. |
|
980 |
int := newD truncated. |
|
981 |
frac := newD fractionPart. "save the fractional part for next time" |
|
982 |
temp := num2. "old numerator and save it" |
|
983 |
num2 := num1. |
|
984 |
num1 := num1 * int + temp. "Update first numerator" |
|
985 |
temp := denom2. "old denominator and save it" |
|
986 |
denom2 := denom1. |
|
987 |
denom1 := int * denom1 + temp. "Update first denominator" |
|
988 |
limit < denom1 ifTrue:[ |
|
989 |
"Is ratio past float precision? If so, pick which of the two ratios to use" |
|
990 |
num2 = 0.0 ifTrue:[ |
|
991 |
"Is second denominator 0?" |
|
992 |
^ Fraction numerator:num1 denominator:denom1 |
|
993 |
]. |
|
994 |
^ Fraction numerator:num2 denominator:denom2 |
|
995 |
] |
|
3060 | 996 |
]. |
997 |
||
5883 | 998 |
"If fractional part is zero, return the first ratio" |
999 |
denom1 = 1 ifTrue:[ |
|
1000 |
"Am i really an Integer?" |
|
1001 |
^ num1 "Yes, return Integer result" |
|
3060 | 1002 |
]. |
5883 | 1003 |
"Otherwise return Fraction result" |
1004 |
^ Fraction numerator:num1 denominator:denom1 |
|
55 | 1005 |
|
3060 | 1006 |
" |
5883 | 1007 |
1.1 asFraction |
1008 |
1.2 asFraction |
|
1009 |
0.3 asFraction |
|
7394 | 1010 |
0.5 asFraction |
1011 |
(1/5) asFloat asFraction |
|
5883 | 1012 |
(1/8) asFloat asFraction |
1013 |
(1/13) asFloat asFraction |
|
16663 | 1014 |
(1/10) asFloat asFraction |
1015 |
(1/10) asFloat asTrueFraction asFixedPoint scale:20 |
|
1016 |
3.14159 asFixedPoint scale:20 |
|
1017 |
3.14159 storeString |
|
1018 |
3.14159 asFraction asFloat storeString |
|
7394 | 1019 |
1.3 asFraction |
1020 |
1.0 asFraction |
|
1021 |
1E6 asFraction |
|
1022 |
1E-6 asFraction |
|
3060 | 1023 |
" |
2793 | 1024 |
|
3060 | 1025 |
"Modified: / 25.10.1997 / 16:41:19 / cg" |
55 | 1026 |
! |
1027 |
||
1028 |
asInteger |
|
1029 |
"return an integer with same value - might truncate" |
|
1030 |
||
3185 | 1031 |
|max maxF| |
55 | 1032 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1033 |
self isFinite ifFalse:[ |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1034 |
"/ ^ self asMetaNumber. |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
1035 |
^ self class |
7418
521174de046d
Nan asInteger raises DomainError
Claus Gittinger <cg@exept.de>
parents:
7405
diff
changeset
|
1036 |
raise:#domainErrorSignal |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
1037 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
1038 |
selector:#asInteger |
6195 | 1039 |
arguments:#() |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1040 |
errorString:'Cannot represent non-finite number as integer' |
2388 | 1041 |
]. |
1042 |
||
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1043 |
self abs < 2e16 ifTrue:[ |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1044 |
"/ NOTICE: this must be redefined in float |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1045 |
"/ subclasses to handle the smallinteger range; |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1046 |
"/ i.e. this may only be invoked for reals |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1047 |
"/ which are NOT within the smallInt range. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1048 |
"/ otherwise, endless recursion is the consequence. |
3426 | 1049 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1050 |
max := SmallInteger maxVal // 2 + 1. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1051 |
maxF := max asFloat. |
3426 | 1052 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1053 |
^ (self quo:maxF) * max + (self rem:maxF) truncated |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1054 |
]. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1055 |
^ self asTrueFraction |
55 | 1056 |
|
3137 | 1057 |
" |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1058 |
12345.0 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1059 |
1e15 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1060 |
1e33 asInteger asFloat |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
1061 |
1e303 asInteger asFloat |
3137 | 1062 |
" |
2388 | 1063 |
|
6195 | 1064 |
"Modified: / 16.11.2001 / 14:15:33 / cg" |
701 | 1065 |
! |
1066 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1067 |
asLargeFloat |
23054 | 1068 |
"return a large float with (approximately) my value. |
1069 |
If the largeFloat class is not present, a regular float is returned" |
|
21802 | 1070 |
|
23056 | 1071 |
^ (LargeFloat ? LongFloat ? Float) fromLimitedPrecisionReal:self |
21802 | 1072 |
|
1073 |
"Modified (comment): / 12-06-2017 / 20:57:05 / cg" |
|
23056 | 1074 |
"Modified: / 07-06-2018 / 16:28:03 / Claus Gittinger" |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1075 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1076 |
|
24155 | 1077 |
asLargeFloatPrecision:n |
1078 |
"return a large float with (approximately) my value. |
|
1079 |
If the largeFloat class is not present, a regular float is returned" |
|
1080 |
||
1081 |
LargeFloat notNil ifTrue:[ |
|
24156 | 1082 |
^ (LargeFloat fromLimitedPrecisionReal:self) precision:n |
24155 | 1083 |
]. |
24156 | 1084 |
^ (LongFloat ? Float) fromLimitedPrecisionReal:self |
1085 |
||
1086 |
" |
|
1087 |
1.0 asLargeFloatPrecision:10 |
|
1088 |
" |
|
24155 | 1089 |
|
1090 |
"Created: / 26-05-2019 / 04:01:22 / Claus Gittinger" |
|
24156 | 1091 |
"Modified (format): / 26-05-2019 / 11:02:53 / Claus Gittinger" |
24155 | 1092 |
! |
1093 |
||
7378 | 1094 |
asLimitedPrecisionReal |
1095 |
"return a float of any precision with same value" |
|
1096 |
||
1097 |
^ self |
|
1098 |
! |
|
1099 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1100 |
asLongFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1101 |
^ LongFloat fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1102 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1103 |
|
24283 | 1104 |
asQuadFloat |
1105 |
^ QuadFloat fromLimitedPrecisionReal:self |
|
1106 |
||
1107 |
"Created: / 07-06-2019 / 02:30:07 / Claus Gittinger" |
|
1108 |
! |
|
1109 |
||
5883 | 1110 |
asRational |
1111 |
"Answer a Rational number--Integer or Fraction--representing the receiver. |
|
1112 |
Same as asFraction fro st-80 compatibility." |
|
1113 |
||
1114 |
^ self asFraction |
|
1115 |
||
1116 |
" |
|
1117 |
1.1 asRational |
|
1118 |
1.2 asRational |
|
1119 |
0.3 asRational |
|
1120 |
0.5 asRational |
|
1121 |
(1/5) asFloat asRational |
|
1122 |
(1/8) asFloat asRational |
|
1123 |
(1/13) asFloat asRational |
|
1124 |
3.14159 asRational |
|
1125 |
3.14159 asRational asFloat |
|
1126 |
1.3 asRational |
|
1127 |
1.0 asRational |
|
1128 |
" |
|
1129 |
! |
|
1130 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1131 |
asShortFloat |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1132 |
^ ShortFloat fromLimitedPrecisionReal:self |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1133 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1134 |
|
7356 | 1135 |
asTrueFraction |
1136 |
"Answer a fraction or integer that EXACTLY represents self, |
|
1137 |
an any-precision IEEE floating point number, consisting of: |
|
1138 |
numMantissaBits bits of normalized mantissa (i.e. with hidden leading 1-bit) |
|
1139 |
optional numExtraBits between mantissa and exponent (normalized flag for ext-real) |
|
1140 |
numExponentBits bits of 2s complement exponent |
|
1141 |
1 sign bit. |
|
24291 | 1142 |
Taken from Float's asTrueFraction" |
7356 | 1143 |
|
1144 |
|shifty sign expPart exp fraction fractionPart result zeroBitsCount |
|
1145 |
numBytes numBits numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
1146 |
biasExponent numIntegerBits| |
|
1147 |
||
1148 |
self isFinite ifFalse:[ |
|
18281 | 1149 |
^ self asMetaNumber |
1150 |
"/ ^ self class |
|
1151 |
"/ raise:#domainErrorSignal |
|
1152 |
"/ receiver:self |
|
1153 |
"/ selector:#asTrueFraction |
|
1154 |
"/ arguments:#() |
|
1155 |
"/ errorString:'Cannot represent non-finite float as a fraction'. |
|
7356 | 1156 |
]. |
1157 |
||
1158 |
"Extract the bits of an IEEE anySize float " |
|
1159 |
numBytes := self basicSize. |
|
24203 | 1160 |
numIntegerBits := self numBitsInIntegerPart. |
1161 |
numBitsInMantissa := self numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
1162 |
numBitsInExponent := self numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
7356 | 1163 |
numBits := numBitsInMantissa + numBitsInExponent. "not (numBytes * 8 - 1) -- there could be 80 float bits in 96 overall bits" |
1164 |
biasExponent := maskExponent bitShift:-1. |
|
1165 |
||
1166 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
1167 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
1168 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
1169 |
] ifFalse:[ |
|
1170 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
1171 |
]. |
|
1172 |
||
1173 |
" Extract the sign and the biased exponent " |
|
1174 |
sign := (shifty bitAt:numBits+1) == 0 ifTrue: [1] ifFalse: [-1]. |
|
22212 | 1175 |
expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent. |
7356 | 1176 |
|
1177 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
1178 |
fractionPart := shifty bitAnd:maskMantissa. |
|
1179 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
701 | 1180 |
|
7356 | 1181 |
numIntegerBits == 0 ifTrue:[ |
1182 |
" Replace omitted leading 1 in fraction (Notice: quadIEEE format does not do this)" |
|
1183 |
fraction := fractionPart bitOr: (maskMantissa + 1). |
|
1184 |
] ifFalse:[ |
|
1185 |
fraction := fractionPart. |
|
1186 |
]. |
|
1187 |
||
7390 | 1188 |
"Unbias exponent" |
7356 | 1189 |
exp := biasExponent - expPart + (numBitsInMantissa - numIntegerBits). |
1200 | 1190 |
|
7390 | 1191 |
" Form the result. When exp>fractionalPrecision, the exponent is adjusted by |
7356 | 1192 |
the number of trailing zero bits in the fraction to minimize |
7390 | 1193 |
the (huge) time otherwise spent in #gcd: of fraction handling code." |
7356 | 1194 |
exp negative ifTrue: [ |
22212 | 1195 |
result := sign * (fraction rightShift: exp) |
7356 | 1196 |
] ifFalse:[ |
1197 |
zeroBitsCount := fraction lowBit - 1. |
|
1198 |
exp := exp - zeroBitsCount. |
|
1199 |
exp <= 0 ifTrue: [ |
|
1200 |
zeroBitsCount := zeroBitsCount + exp. |
|
22212 | 1201 |
result := sign * (fraction rightShift:zeroBitsCount) |
7356 | 1202 |
] ifFalse: [ |
1203 |
result := Fraction |
|
22212 | 1204 |
numerator: (sign * (fraction rightShift: zeroBitsCount)) |
7356 | 1205 |
denominator: (1 bitShift:exp) |
1206 |
] |
|
1207 |
]. |
|
1200 | 1208 |
|
7356 | 1209 |
"Low cost validation omitted after extensive testing" |
1210 |
"(result asFloat = self) ifFalse: [self error: 'asTrueFraction validation failed']." |
|
1211 |
||
1212 |
^ result |
|
1213 |
||
1214 |
" |
|
7390 | 1215 |
0.3 asFloat asTrueFraction |
7356 | 1216 |
0.3 asShortFloat asTrueFraction |
7390 | 1217 |
0.3 asLongFloat asTrueFraction |
1218 |
||
1219 |
1.25 asTrueFraction |
|
7480 | 1220 |
1.25 asShortFloat asTrueFraction |
7390 | 1221 |
0.25 asTrueFraction |
1222 |
-0.25 asTrueFraction |
|
1223 |
3e37 asTrueFraction |
|
1224 |
||
18281 | 1225 |
LongFloat NaN asTrueFraction |
1226 |
LongFloat infinity asTrueFraction |
|
1227 |
LongFloat negativeInfinity asTrueFraction |
|
7390 | 1228 |
" |
22212 | 1229 |
|
1230 |
"Modified: / 25-08-2017 / 12:34:08 / cg" |
|
24203 | 1231 |
"Modified: / 28-05-2019 / 09:04:52 / Claus Gittinger" |
24291 | 1232 |
"Modified (comment): / 08-06-2019 / 13:59:40 / Claus Gittinger" |
701 | 1233 |
! ! |
1234 |
||
1235 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
1236 |
||
1237 |
< aNumber |
|
1238 |
"return true, if the argument is greater" |
|
1239 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1240 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1241 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1242 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 1243 |
! ! |
1244 |
||
55 | 1245 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
1246 |
||
1247 |
differenceFromFraction:aFraction |
|
24277 | 1248 |
"sent when a fraction does not know how to subtract the receiver" |
55 | 1249 |
|
1250 |
|d| |
|
1251 |
||
1252 |
d := aFraction denominator. |
|
1253 |
^ (aFraction numerator - (self * d)) / d |
|
24277 | 1254 |
|
1255 |
"Modified (comment): / 07-06-2019 / 02:19:52 / Claus Gittinger" |
|
55 | 1256 |
! |
1257 |
||
1258 |
productFromFraction:aFraction |
|
24277 | 1259 |
"sent when a fraction does not know how to multiply the receiver" |
55 | 1260 |
|
1261 |
^ self * aFraction numerator / aFraction denominator |
|
24277 | 1262 |
|
1263 |
"Modified (comment): / 07-06-2019 / 02:19:47 / Claus Gittinger" |
|
55 | 1264 |
! |
1265 |
||
1266 |
quotientFromFraction:aFraction |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1267 |
"Return the quotient of the argument, aFraction and the receiver. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1268 |
Sent when aFraction does not know how to divide by the receiver." |
55 | 1269 |
|
1270 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 1271 |
! |
1272 |
||
1273 |
sumFromFraction:aFraction |
|
24277 | 1274 |
"sent when a fraction does not know how to add the receiver" |
701 | 1275 |
|
1276 |
|d| |
|
1277 |
||
1278 |
d := aFraction denominator. |
|
1279 |
^ (self * d + aFraction numerator) / d |
|
24277 | 1280 |
|
1281 |
"Modified (comment): / 07-06-2019 / 02:19:35 / Claus Gittinger" |
|
10680 | 1282 |
! |
1283 |
||
1284 |
sumFromTimestamp:aTimestamp |
|
1285 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
1286 |
after aTimestamp" |
|
1287 |
||
1288 |
^ aTimestamp addMilliseconds:(self * 1000) truncated. |
|
1289 |
||
1290 |
" |
|
1291 |
Timestamp now sumFromTimestamp:aTimestamp |
|
1292 |
100.0 sumFromTimestamp:Timestamp now |
|
1293 |
||
1294 |
|t1 t2| |
|
1295 |
t1 := Timestamp now. |
|
1296 |
t2 := 1.5 sumFromTimestamp:t1. |
|
1297 |
t1 inspect. t2 inspect. |
|
1298 |
" |
|
701 | 1299 |
! ! |
1300 |
||
16731 | 1301 |
|
23402 | 1302 |
!LimitedPrecisionReal methodsFor:'printing'! |
1303 |
||
1304 |
printStringScientific |
|
1305 |
"return a 'user friendly' scientific printString. |
|
1306 |
Notice: this returns a Text object with emphasis. |
|
1307 |
Also: the returned string is not meant to be read back - purely for GUIs" |
|
1308 |
||
1309 |
|s idx| |
|
1310 |
||
1311 |
s := PrintfScanf printf:'%e' argument:self. |
|
1312 |
idx := s indexOfAny:'eE'. |
|
1313 |
idx == 0 ifTrue:[^ s]. |
|
1314 |
^ (s copyTo:idx-1) , '×10' , ((s copyFrom:idx+1) asText emphasisAllAdd:#superscript) |
|
1315 |
||
1316 |
" |
|
1317 |
1.23e14 printStringScientific |
|
24643 | 1318 |
1.23456 printStringScientific |
1319 |
PrintfScanf printf:'%e' argument:1.23456 |
|
23402 | 1320 |
" |
1321 |
||
1322 |
"Created: / 05-10-2018 / 13:38:44 / Claus Gittinger" |
|
1323 |
! ! |
|
22851 | 1324 |
|
22305 | 1325 |
!LimitedPrecisionReal methodsFor:'queries'! |
701 | 1326 |
|
22305 | 1327 |
decimalPrecision |
1328 |
"Answer how many digits of accuracy this class supports" |
|
1200 | 1329 |
|
22305 | 1330 |
^ self class decimalPrecision |
1200 | 1331 |
|
22305 | 1332 |
" |
1333 |
1.0 asFloat decimalPrecision |
|
1334 |
1.0 asLongFloat decimalPrecision |
|
1335 |
1.0 asShortFloat decimalPrecision |
|
1336 |
1.0 asQDouble decimalPrecision |
|
1337 |
1.0 asLargeFloat decimalPrecision |
|
1338 |
" |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1339 |
|
22305 | 1340 |
"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
|
1341 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1342 |
|
5883 | 1343 |
defaultNumberOfDigits |
7356 | 1344 |
"Answer how many digits of accuracy this class supports" |
1345 |
||
16663 | 1346 |
^ self class decimalPrecision |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1347 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1348 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1349 |
Float new defaultNumberOfDigits |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1350 |
LongFloat new defaultNumberOfDigits |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1351 |
ShortFloat new defaultNumberOfDigits |
21853 | 1352 |
QDouble new defaultNumberOfDigits |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
1353 |
" |
21853 | 1354 |
|
1355 |
"Modified (comment): / 20-06-2017 / 12:58:12 / cg" |
|
1356 |
! |
|
1357 |
||
1358 |
exponent |
|
1359 |
"extract a normalized float's exponent. |
|
1360 |
This is a fallback for systems which do not provide frexp in their math lib, |
|
1361 |
als also for error reporting (NaN or Inf). |
|
1362 |
The returned value depends on the float-representation of |
|
1363 |
the underlying machine and is therefore highly unportable. |
|
1364 |
This is not for general use. |
|
1365 |
This assumes that the mantissa is normalized to |
|
1366 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
|
1367 |
||
1368 |
|shifty expPart exp numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
1369 |
biasExponent numIntegerBits fractionPart| |
|
1370 |
||
1371 |
(self isNaN or:[self isInfinite]) ifTrue:[ |
|
1372 |
^ self class |
|
1373 |
raise:#domainErrorSignal |
|
1374 |
receiver:self |
|
1375 |
selector:#exponent |
|
1376 |
arguments:#() |
|
1377 |
errorString:'bad receiver (Inf or NaN) in exponent' |
|
1378 |
]. |
|
1379 |
||
1380 |
"Extract the bits of an IEEE anySize float " |
|
1381 |
numBytes := self basicSize. |
|
24203 | 1382 |
numBitsInMantissa := self numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
1383 |
numBitsInExponent := self numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
1384 |
numIntegerBits := self numBitsInIntegerPart. |
|
21853 | 1385 |
biasExponent := maskExponent bitShift:-1. |
1386 |
||
1387 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
1388 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
1389 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
1390 |
] ifFalse:[ |
|
1391 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
1392 |
]. |
|
1393 |
||
1394 |
" Extract the sign and the biased exponent " |
|
22212 | 1395 |
expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent. |
21853 | 1396 |
|
1397 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
1398 |
fractionPart := shifty bitAnd:maskMantissa. |
|
1399 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
1400 |
||
1401 |
exp := expPart - biasExponent + 1. |
|
1402 |
^ exp |
|
1403 |
||
1404 |
" |
|
1405 |
0.3 asFloat exponent |
|
1406 |
0.3 asShortFloat exponent |
|
1407 |
0.3 asLongFloat exponent |
|
1408 |
||
1409 |
0.0 exponent2 0 |
|
1410 |
1.0 exponent2 1 |
|
1411 |
2.0 exponent2 2 |
|
1412 |
3.0 exponent2 2 |
|
1413 |
4.0 exponent2 3 |
|
1414 |
0.5 exponent2 0 |
|
1415 |
0.4 exponent2 -1 |
|
1416 |
0.25 exponent2 -1 |
|
1417 |
0.00000011111 exponent2 -23 |
|
1418 |
" |
|
1419 |
||
22212 | 1420 |
"Modified: / 25-08-2017 / 12:34:23 / cg" |
24203 | 1421 |
"Modified: / 28-05-2019 / 09:05:02 / Claus Gittinger" |
21853 | 1422 |
! |
1423 |
||
1424 |
fractionalPart |
|
1425 |
"This has been renamed to #fractionPart for ST80 compatibility. |
|
1426 |
||
1427 |
extract the after-decimal fraction part. |
|
1428 |
the floats value is |
|
1429 |
float truncated + float fractionalPart" |
|
1430 |
||
1431 |
<resource:#obsolete> |
|
1432 |
||
1433 |
self obsoleteMethodWarning:'please use #fractionPart'. |
|
1434 |
^ self fractionPart |
|
1435 |
||
1436 |
"Modified: / 28.10.1998 / 17:10:12 / cg" |
|
1437 |
"Created: / 28.10.1998 / 17:10:32 / cg" |
|
1438 |
! |
|
1439 |
||
1440 |
mantissa |
|
1441 |
"extract a normalized float's mantissa. |
|
1442 |
This is a fallback for systems which do not provide frexp in their math lib, |
|
1443 |
als also for error reporting (NaN or Inf)." |
|
1444 |
||
1445 |
|shifty expPart numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
1446 |
biasExponent numIntegerBits fractionPart| |
|
1447 |
||
1448 |
(self isNaN or:[self isInfinite]) ifTrue:[ |
|
1449 |
^ self class |
|
1450 |
raise:#domainErrorSignal |
|
1451 |
receiver:self |
|
1452 |
selector:#mantissa |
|
1453 |
arguments:#() |
|
1454 |
errorString:'bad receiver (Inf or NaN) in mantissa' |
|
1455 |
]. |
|
1456 |
||
1457 |
"Extract the bits of an IEEE anySize float " |
|
1458 |
numBytes := self basicSize. |
|
24203 | 1459 |
numBitsInMantissa := self numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
1460 |
numBitsInExponent := self numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
1461 |
numIntegerBits := self numBitsInIntegerPart. |
|
21853 | 1462 |
biasExponent := maskExponent bitShift:-1. |
1463 |
||
1464 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
1465 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
1466 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
1467 |
] ifFalse:[ |
|
1468 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
1469 |
]. |
|
1470 |
||
1471 |
" Extract the sign and the biased exponent " |
|
22212 | 1472 |
expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent. |
21853 | 1473 |
|
1474 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
1475 |
fractionPart := shifty bitAnd:maskMantissa. |
|
1476 |
^ fractionPart. |
|
1477 |
||
1478 |
" |
|
1479 |
0.3 asFloat mantissa |
|
1480 |
0.3 asShortFloat mantissa |
|
1481 |
0.3 asLongFloat mantissa |
|
1482 |
0.3 asQDouble mantissa |
|
1483 |
" |
|
1484 |
||
1485 |
"Created: / 20-06-2017 / 11:36:51 / cg" |
|
22212 | 1486 |
"Modified: / 25-08-2017 / 12:34:34 / cg" |
24203 | 1487 |
"Modified: / 28-05-2019 / 09:05:10 / Claus Gittinger" |
1488 |
! |
|
1489 |
||
1490 |
numBitsInExponent |
|
1491 |
"answer the number of bits in the exponent |
|
1492 |
11 for double precision: |
|
1493 |
seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm |
|
1494 |
8 for single precision: |
|
1495 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
|
1496 |
15 for long floats (x86): |
|
1497 |
00000000 00000000 seeeeeee eeeeeeee immmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm |
|
1498 |
15 for long floats (sparc): |
|
1499 |
seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm... |
|
1500 |
other for LargeFloats" |
|
1501 |
||
1502 |
^ self class numBitsInExponent |
|
1503 |
||
1504 |
"Created: / 28-05-2019 / 08:52:44 / Claus Gittinger" |
|
1505 |
! |
|
1506 |
||
1507 |
numBitsInIntegerPart |
|
1508 |
"answer the number of bits in the integer part of the mantissa. |
|
1509 |
Most floating point formats are normalized to get rid of the extra bit. |
|
1510 |
(i.e. except for LongFloats and LargeFloats, |
|
1511 |
instances are normalized to exclude any integer bit" |
|
1512 |
||
1513 |
^ self class numBitsInIntegerPart |
|
1514 |
||
1515 |
"Created: / 28-05-2019 / 08:52:52 / Claus Gittinger" |
|
1516 |
! |
|
1517 |
||
1518 |
numBitsInMantissa |
|
1519 |
"answer the number of bits in the mantissa (any hidden bits are not counted). |
|
1520 |
52 for double precision: |
|
1521 |
seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm |
|
1522 |
23 for single precision: |
|
1523 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
|
1524 |
64 for longfloat precision (x86): |
|
1525 |
00000000 00000000 seeeeeee eeeeeeee immmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm |
|
1526 |
112 for longfloat precision (sparc): |
|
1527 |
seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm... |
|
1528 |
" |
|
1529 |
||
1530 |
^ self class numBitsInMantissa |
|
1531 |
||
1532 |
" |
|
1533 |
1.0 numBitsInMantissa |
|
1534 |
1.0 asShortFloat numBitsInMantissa |
|
1535 |
1.0 asLongFloat numBitsInMantissa |
|
1536 |
" |
|
1537 |
||
1538 |
"Created: / 28-05-2019 / 08:52:34 / Claus Gittinger" |
|
5883 | 1539 |
! |
1540 |
||
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1541 |
precision |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1542 |
"return the number of valid mantissa bits. |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1543 |
Should be redefined in classes which allow per-instance precision specification" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1544 |
|
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1545 |
^ self class precision |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1546 |
! |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1547 |
|
5557 | 1548 |
size |
1549 |
"redefined since reals are kludgy (ByteArry)" |
|
1550 |
||
1551 |
^ 0 |
|
1552 |
! ! |
|
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
1553 |
|
24327 | 1554 |
|
24155 | 1555 |
!LimitedPrecisionReal methodsFor:'special access'! |
1556 |
||
1557 |
partValues:aBlock |
|
1558 |
"invoke aBlock with sign, exponent and abs(mantissa)" |
|
1559 |
||
1560 |
^ aBlock |
|
1561 |
value:self sign |
|
1562 |
value:self exponent |
|
1563 |
value:self mantissa abs |
|
1564 |
||
1565 |
" |
|
1566 |
1.0 partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa]. |
|
1567 |
2.0 partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa]. |
|
1568 |
-1.0 partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa]. |
|
1569 |
-2.0 partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa]. |
|
24229 | 1570 |
|
1571 |
1.0 asShortFloat partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa]. |
|
1572 |
1.0 asLongFloat partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa]. |
|
1573 |
1.0 asLargeFloat partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa]. |
|
24155 | 1574 |
" |
1575 |
||
1576 |
"Created: / 26-05-2019 / 03:11:28 / Claus Gittinger" |
|
24229 | 1577 |
"Modified (comment): / 29-05-2019 / 03:49:20 / Claus Gittinger" |
24155 | 1578 |
! ! |
1579 |
||
701 | 1580 |
!LimitedPrecisionReal methodsFor:'testing'! |
1581 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1582 |
isFinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1583 |
^ self subclassResponsibility |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1584 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1585 |
"Created: / 7.1.1998 / 12:02:06 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1586 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1587 |
|
6185 | 1588 |
isFloat |
1589 |
"return true, if the receiver is some kind of floating point number; |
|
1590 |
false is returned here. |
|
1591 |
Same as #isLimitedPrecisionReal, but a better name ;-)" |
|
1592 |
||
1593 |
^ true |
|
1594 |
||
1595 |
"Created: / 14.11.2001 / 14:57:55 / cg" |
|
1596 |
! |
|
1597 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1598 |
isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1599 |
"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
|
1600 |
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
|
1601 |
however, inline C-code could produce them ..." |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1602 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1603 |
^ (self isFinite or:[self isNaN]) not. |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1604 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1605 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1606 |
1.0 isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1607 |
(0.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1608 |
(1.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1609 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1610 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1611 |
"Modified: / 7.1.1998 / 12:01:30 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1612 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
1613 |
|
6086
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1614 |
isLimitedPrecisionReal |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1615 |
"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
|
1616 |
true is returned here - the method is redefined from Object." |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1617 |
|
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1618 |
^ true |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1619 |
! |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
1620 |
|
2388 | 1621 |
isNaN |
1622 |
^ self subclassResponsibility |
|
1623 |
||
1624 |
"Modified: 12.2.1997 / 16:45:27 / cg" |
|
1625 |
! |
|
1626 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1627 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1628 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1629 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1630 |
^ (self isZero) and:[self printString first == $-] |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1631 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1632 |
" |
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1633 |
0.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1634 |
-0.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1635 |
-1.0 asLongFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1636 |
1.0 asLongFloat isNegativeZero |
7356 | 1637 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1638 |
0.0 asLargeFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1639 |
-0.0 asLargeFloat isNegativeZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1640 |
" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1641 |
" |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1642 |
0.0 asLongFloat isZero |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1643 |
-0.0 asLongFloat isZero |
701 | 1644 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1645 |
0.0 > 0 |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1646 |
-0.0 < 0 |
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1647 |
" |
701 | 1648 |
! |
1649 |
||
24327 | 1650 |
isZero |
1651 |
"return true, if the receiver is zero" |
|
1652 |
||
1653 |
^ self = self class zero |
|
1654 |
||
1655 |
"Created: / 10-06-2019 / 21:58:45 / Claus Gittinger" |
|
1656 |
! |
|
1657 |
||
5357 | 1658 |
numberOfBits |
1659 |
"return the size (in bits) of the real; |
|
1660 |
typically, this is 64 for Floats and 32 for ShortFloats, |
|
1661 |
but who knows ..." |
|
1662 |
||
1663 |
self subclassResponsibility |
|
1664 |
! |
|
1665 |
||
701 | 1666 |
positive |
18854 | 1667 |
"return true if the receiver is greater or equal to zero (not negative)" |
701 | 1668 |
|
7447
64fab8614c76
double dispatching, converting
Claus Gittinger <cg@exept.de>
parents:
7418
diff
changeset
|
1669 |
^ self sign >= 0 |
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1670 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
1671 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
17380 | 1672 |
! |
1673 |
||
1674 |
sign |
|
1675 |
"return the sign of the receiver (-1, 0 or 1)" |
|
1676 |
||
1677 |
self strictlyPositive ifTrue:[^ 1]. |
|
1678 |
self negative ifTrue:[^ -1]. |
|
1679 |
^ 0 |
|
1680 |
||
1681 |
" |
|
1682 |
-1.0 sign |
|
1683 |
-0.0 sign |
|
1684 |
1.0 sign |
|
1685 |
0.0 sign |
|
1686 |
" |
|
55 | 1687 |
! ! |
1688 |
||
7356 | 1689 |
!LimitedPrecisionReal methodsFor:'truncation & rounding'! |
1690 |
||
1691 |
ceilingAsFloat |
|
1692 |
^ self coerce:(self ceiling). |
|
1693 |
||
1694 |
" |
|
1695 |
0.4 asLongFloat ceilingAsFloat |
|
1696 |
" |
|
1697 |
! |
|
1698 |
||
1699 |
floorAsFloat |
|
1700 |
^ self coerce:(self floor). |
|
1701 |
||
1702 |
" |
|
1703 |
0.4 asLongFloat floorAsFloat |
|
1704 |
" |
|
1705 |
! |
|
1706 |
||
1707 |
roundedAsFloat |
|
1708 |
self negative ifTrue:[ ^ (self - 0.5) ceilingAsFloat ]. |
|
1709 |
^ (self + 0.5) floorAsFloat |
|
7398 | 1710 |
! |
1711 |
||
1712 |
truncatedAsFloat |
|
1713 |
^ self coerce:(self truncated). |
|
1714 |
||
1715 |
" |
|
1716 |
0.4 asLongFloat truncatedAsFloat |
|
1717 |
" |
|
22786 | 1718 |
! |
1719 |
||
1720 |
truncatedToPrecision |
|
1721 |
"truncates to the precision of the float. |
|
1722 |
This is slightly different from truncated. |
|
1723 |
Taking for example 1e32, |
|
1724 |
the printed representation will be 1e32, |
|
1725 |
but the actual value, when truncating to an integer |
|
1726 |
would be 100000003318135351409612647563264. |
|
1727 |
||
1728 |
This is due to the inaccuracy in the least significant bits, |
|
1729 |
and the way the print-converter compensates for this. |
|
1730 |
This method tries to generate an integer value which corresponds |
|
1731 |
to what is seen in the float's printString. |
|
1732 |
||
1733 |
Here, a slow fallback (generating and rescanning the printString) |
|
1734 |
is provided, which should work on any float number. |
|
1735 |
Specialized versions in subclasses may be added for more performance |
|
1736 |
(however, this is probably only used rarely)" |
|
1737 |
||
1738 |
|pow10 printString intVal s sign nFract ch expSign exp pI| |
|
1739 |
||
1740 |
pow10 := #(10 100 1000 10000 100000 1000000 10000000 100000000 1000000000). |
|
1741 |
||
1742 |
printString := self printString. |
|
1743 |
intVal := 0. |
|
1744 |
sign := 1. |
|
1745 |
nFract := 0. |
|
1746 |
exp := 0. |
|
1747 |
||
1748 |
"/ fetch the mantissa |
|
1749 |
s := printString readStream. |
|
1750 |
s peek == $- ifTrue:[ |
|
1751 |
sign := -1. |
|
1752 |
s next. |
|
1753 |
]. |
|
1754 |
intVal := s nextDecimalInteger. |
|
1755 |
s peek == $. ifTrue:[ |
|
1756 |
s next. |
|
1757 |
[(ch := s peek) notNil and:[ch isDigit]] whileTrue:[ |
|
1758 |
intVal := intVal * 10 + (s next digitValue). |
|
1759 |
nFract := nFract + 1. |
|
1760 |
]. |
|
1761 |
]. |
|
1762 |
('eEdDqQ' includes:s peek) ifTrue:[ |
|
1763 |
expSign := 1. |
|
1764 |
s next. |
|
1765 |
(ch := s peek) == $- ifTrue:[ |
|
1766 |
expSign := -1. |
|
1767 |
s next. |
|
1768 |
] ifFalse:[ |
|
1769 |
ch == $+ ifTrue:[ |
|
1770 |
s next. |
|
1771 |
]. |
|
1772 |
]. |
|
1773 |
exp := s nextDecimalInteger. |
|
1774 |
exp := exp * expSign. |
|
1775 |
]. |
|
1776 |
exp := exp - nFract. |
|
1777 |
exp < 0 ifTrue:[ |
|
1778 |
[exp < 0] whileTrue:[ |
|
1779 |
pI := (exp negated) min:pow10 size. |
|
1780 |
intVal := intVal / (pow10 at:pI). |
|
1781 |
exp := exp + pI. |
|
1782 |
]. |
|
1783 |
intVal := intVal asInteger. |
|
1784 |
] ifFalse:[ |
|
1785 |
[exp > 0] whileTrue:[ |
|
1786 |
pI := exp min:pow10 size. |
|
1787 |
intVal := intVal * (pow10 at:pI). |
|
1788 |
exp := exp - pI. |
|
1789 |
]. |
|
1790 |
]. |
|
1791 |
^ intVal |
|
1792 |
||
1793 |
" |
|
1794 |
1e32 asShortFloat truncated |
|
1795 |
1e32 asShortFloat truncatedToPrecision |
|
1796 |
1.234e10 asShortFloat truncatedToPrecision |
|
1797 |
1234e-1 asShortFloat truncatedToPrecision |
|
1798 |
||
1799 |
1e32 truncated |
|
1800 |
1e32 truncatedToPrecision |
|
1801 |
1.234e10 truncatedToPrecision |
|
1802 |
1234e-1 truncatedToPrecision |
|
1803 |
||
1804 |
1e32 asLongFloat truncated |
|
1805 |
1e32 asLongFloat truncatedToPrecision |
|
1806 |
1.234e10 asLongFloat truncatedToPrecision |
|
1807 |
1234e-1 asLongFloat truncatedToPrecision |
|
1808 |
" |
|
7356 | 1809 |
! ! |
1810 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1811 |
!LimitedPrecisionReal methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1812 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1813 |
acceptVisitor:aVisitor with:aParameter |
16731 | 1814 |
"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
|
1815 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1816 |
^ aVisitor visitFloat:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1817 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8202
diff
changeset
|
1818 |
|
701 | 1819 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 1820 |
|
701 | 1821 |
version |
18834 | 1822 |
^ '$Header$' |
12120 | 1823 |
! |
1824 |
||
1825 |
version_CVS |
|
18834 | 1826 |
^ '$Header$' |
55 | 1827 |
! ! |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
1828 |
|
16405 | 1829 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
1830 |
LimitedPrecisionReal initialize! |