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