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