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