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