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