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