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