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