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