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